在C#中使用New Process(),如何将命令行文本复制到文本文件?

时间:2012-01-04 18:21:31

标签: c# processstartinfo

我想使用参数-a,-c和3400 @ takd运行lmutil.exe,然后将命令行提示生成的所有内容放入文本文件中。我下面的内容不起作用。

如果我单步执行该过程,我会收到类似“抛出类型System.InvalidOperationException异常”的错误

        Process p = new Process();
        p.StartInfo.FileName = @"C:\FlexLM\lmutil.exe";
        p.StartInfo.Arguments = "lmstat -a -c 3400@tkad>Report.txt";
        p.Start();
        p.WaitForExit();

我想要的只是将命令行输出写入Report.txt

2 个答案:

答案 0 :(得分:2)

要获得Process输出,您可以使用StandardOutput记录的Process p = new Process(); p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = @"C:\FlexLM\lmutil.exe"; p.StartInfo.Arguments = "lmstat -a -c 3400@tkad"; p.Start(); System.IO.File.WriteAllText("Report.txt", p.StandardOutput.ReadToEnd()); p.WaitForExit(); p.Close(); 属性。

然后你可以把它写到文件中:

{{1}}

答案 1 :(得分:1)

您无法使用>通过Process重定向,您必须使用StandardOutput。另请注意,要使其工作StartInfo.RedirectStandardOutput必须设置为true。

相关问题