ConnectUNCWithCredentials.UNCAccessWithCredentials.NetUseWithCredentials C# (CSharp) Method

NetUseWithCredentials() private method

private NetUseWithCredentials ( ) : bool
return bool
        private bool NetUseWithCredentials()
        {
            uint returncode;
            try
            {
                USE_INFO_2 useinfo = new USE_INFO_2();

                useinfo.ui2_remote = sUNCPath;
                useinfo.ui2_username = sUser;
                useinfo.ui2_domainname = sDomain;
                useinfo.ui2_password = sPassword;
                useinfo.ui2_asg_type = 0;
                useinfo.ui2_usecount = 1;
                uint paramErrorIndex;
                returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
                iLastError = (int)returncode;
                return returncode == 0;
            }
            catch
            {
                iLastError = Marshal.GetLastWin32Error();
                return false;
            }
        }

Same methods

UNCAccessWithCredentials::NetUseWithCredentials ( string UNCPath, string User, string Domain, string Password ) : bool

Usage Example

Esempio n. 1
0
 private void btnConnect_Click(object sender, EventArgs e)
 {
     this.Cursor = Cursors.WaitCursor;
     Application.DoEvents();
     string[] dirs;
     using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
     {
         if (unc.NetUseWithCredentials(tbUNCPath.Text,
                                       tbUserName.Text,
                                       tbDomain.Text,
                                       tbPassword.Text))
         {
             dirs = Directory.GetDirectories(tbUNCPath.Text);
             foreach (string d in dirs)
             {
                 tbDirList.Text += d + "\r\n";
             }
         }
         else
         {
             this.Cursor = Cursors.Default;
             MessageBox.Show("Failed to connect to " + tbUNCPath.Text + "\r\nLastError = " + unc.LastError.ToString(),
                             "Failed to connect",
                             MessageBoxButtons.OK,
                             MessageBoxIcon.Error);
         }
     }
     this.Cursor = Cursors.Default;
 }
All Usage Examples Of ConnectUNCWithCredentials.UNCAccessWithCredentials::NetUseWithCredentials