我在C#应用程序中使用以下代码通过Mono运行Linux控制台命令:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c ls");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
String result = proc.StandardOutput.ReadToEnd();
这个命令按预期工作。但是,如果我输入命令
"-c ls -l"
或"-c ls /path"
,输出仍然会忽略-l
和path
。在使用多个开关的命令时,应该使用什么语法?
ProcessStartInfo.Arguments
来看看替代方法是否可行?另外,你需要/bin/bash吗?你不能直接运行'ls'吗? - cjb110