1.登錄代碼 2.執行登錄 ...
1.登錄代碼
/// <summary>
/// 登錄許可權
/// </summary>
/// <param name="path"></param>
/// <param name="userName"></param>
/// <param name="passWord"></param>
/// <returns></returns>
public bool ConnectState(string path, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
throw new Exception(errormsg);
}
}
catch (Exception ex)
{
DisplaylistboxMsg(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
2.執行登錄
private void 登錄_Click(object sender, EventArgs e)
{
bool userbool = ConnectState(@"\\**.**.*.*",User.Text,Pwd.Text);
if (userbool)
{
DisplaylistboxMsg("登錄成功:"+User.Text);
}
else
{
DisplaylistboxMsg("登錄失敗");
}
}