简体   繁体   English

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

[英]Using New Process() in C#, how can I copy the command line text to a text file?

I want to run lmutil.exe with the arguments -a, -c, and 3400@takd, then put everything that command line prompt generates into a text file. 我想使用参数-a,-c和3400 @ takd运行lmutil.exe,然后将命令行提示符生成的所有内容都放入文本文件中。 What I have below isn't working. 我下面的内容不起作用。

If I step through the process, I get errors like "threw an exception of type System.InvalidOperationException" 如果我单步执行该过程,我会收到类似“抛出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();

All I want is for the command line output to be written to Report.txt 我想要的是将命令行输出写入Report.txt

To get the Process output you can use the StandardOutput property documented here . 要获取Process输出,您可以使用此处记录的StandardOutput属性。

Then you can write it to a file: 然后,您可以将其写入文件:

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();

You can't use > to redirect via Process, you have to use StandardOutput . 您不能使用>来通过Process进行重定向,而必须使用StandardOutput Also note that for it to work StartInfo.RedirectStandardOutput has to be set to true. 另请注意,要使其工作, StartInfo.RedirectStandardOutput必须设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM