在win form工程中怎麽樣調用xcopy系統命令
2008-10-25 19:26:53 来源:WEB开发网private string RunCommand(string cmd)
{
string res = String.Empty;
string error = String.Empty;
Process p = new Process();
StreamWriter sw;
StreamReader sr;
//StreamReader err;
ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
try
{
p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
//err = p.StandardError;
sw.AutoFlush = true;
sw.WriteLine(cmd);
sw.Close();
//error = err.ReadToEnd();
res = sr.ReadToEnd();
//err.Close();
sr.Close();
p.Close();
}
catch(Exception e)
{
MessageBox.Show("Error when run command: " + cmd + "n" + e.Message + "n");
return "";
}
if(error.Length != 0)
{
MessageBox.Show("Error when run command: " + cmd + "n" + error + "n");
return "";
}
return res;
}
更多精彩
赞助商链接