private string RunCommand(string cmd, string args)
{
Process proc = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo(cmd);
startInfo.Arguments = args;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
proc.StartInfo = startInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
proc.Close();
return output;
}
本文介绍了一个使用C#来执行外部命令并获取其输出的例子。通过Process类及其相关属性,可以实现对外部程序的调用及结果读取。

被折叠的 条评论
为什么被折叠?



