C# 调用命令行命令 net use

bool flag = false;
Process pro = new Process();
try
{
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.CreateNoWindow = true;
pro.Start();
string docline = @"net use " + "" + path + " " + "/user:" + username + " " + pwd;
// MessageBox.Show(docline);
pro.StandardInput.WriteLine(docline);
pro.StandardInput.WriteLine("exit");
while (!pro.HasExited)
{
pro.WaitForExit(1000);
}
string errormsg = pro.StandardError.ReadToEnd();
pro.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
flag = true;
}
else
{
throw new Exception(errormsg);

}
;
}
catch (System.Exception ex)
{
throw ex;

}
finally
{
pro.Close();
pro.Dispose();
}

return flag;
}
}

原文地址:https://www.cnblogs.com/lionmxs/p/12115020.html