#region 根据用户名,密码连接服务共享
/// <summary>
/// 根据用户名,密码连接服务共享
/// </summary>
/// <param name="path"></param>
/// <param name="userName"></param>
/// <param name="passWord"></param>
/// <param name="ip"></param>
/// <returns></returns>
public static bool connectState(string path, string userName, string passWord, string ip)
{
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)
{
Flag = false;
return Flag;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
#endregion
快乐而轻松的写代码