使用c#运行shell命令并将信息输入字符串

我想从c#运行shell命令并使用我程序中的返回信息。 所以我已经知道要从终端运行一些东西我需要做那样的事情

string strCmdText; strCmdText= "p4.exe jobs -e"; System.Diagnostics.Process.Start("CMD.exe",strCmdText); 

所以现在命令执行了,并从这个命令返回一些信息…我的问题是如何在我的程序中使用这些信息,可能与命令行参数有关,但不确定…

我知道使用python这样的脚本语言更容易,但是真的需要使用c#

您可以使用ProcessStartInfo重定向输出。 有关MSDN和SO的例子。

例如

 Process proc = new Process { StartInfo = new ProcessStartInfo { FileName = "program.exe", Arguments = "command line arguments to your executable", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; 

然后开始这个过程并从中读取:

 proc.Start(); while (!proc.StandardOutput.EndOfStream) { string line = proc.StandardOutput.ReadLine(); // do something with line } 

根据您要完成的任务,您可以实现更多目标。 我编写的应用程序异步地将数据传递到命令行并从中读取数据。 这样的例子不容易在论坛上发布。