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;
}
}

上一篇:jsp&servlet&jdbc&mysql实现简单的增删查改(一)


下一篇:MapReduce工作流程