如何使用C#轻松运行Shell命令?
我如何使用C#运行命令提示符命令?
可以说我想按顺序运行这些命令:
cd F:/File/File2/...FileX/
ipconfig
ping google.com
或类似的东西…有人可以使用此方法:
void runCommands(String[] commands)
{
//method guts...
}
这样您的输入是一系列字符串命令(例如[“ ipconfig”,“ ping 192.168.192.168”,“ ping google.com”,“ nslookup facebook.com”),应在特定命令的单个命令提示符下执行将它们放入数组的顺序.谢谢.
解决方法:
that should be executed on a single command prompt in the specific
sequence in which they are put in the array
您可以在bat文件中编写命令序列,然后如下运行.
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Reference