【问题标题】:Using New Process() in C#, how can I copy the command line text to a text file?在 C# 中使用 New Process(),如何将命令行文本复制到文本文件中?
【发布时间】:2012-01-04 18:21:30
【问题描述】:

我想使用参数 -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

【问题讨论】:

  • 3400@tkad>Report.txt 之间的空格怎么样?
  • 34000@tkad 中的 > 是什么
  • 文件名 = "cmd.exe", 参数 = "/c c:\flex..yadayada > Report.txt"
  • 空格没有用,>表示应该将输出复制到文本文件中

标签: c# processstartinfo


【解决方案1】:

要获得Process 输出,您可以使用记录在here 中的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();

【讨论】:

  • 好的,然后从参数中删除 `>Report.txt'。
  • 这是有效的。我原以为它会把它放在lmutil.exe的目录中。相反,它位于我正在运行的 c# 程序的 bin 文件夹中。谢谢
【解决方案2】:

你不能使用>通过进程重定向,你必须使用StandardOutput。另请注意,StartInfo.RedirectStandardOutput 必须设置为 true。

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2019-10-07
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    相关资源
    最近更新 更多