【问题标题】:c# start process arguments pass in datac# 启动进程参数传入数据
【发布时间】:2011-05-24 19:35:50
【问题描述】:

尝试将文件中的数据作为 C# 中的 cmd 行参数传递并遇到问题

ProcessStartInfo startInfo1 = new ProcessStartInfo();
startInfo1.FileName = @"myexe.exe";
startInfo1.UseShellExecute = false;
startInfo1.RedirectStandardOutput = true;
startInfo1.WindowStyle = ProcessWindowStyle.Hidden;
startInfo1.WorkingDirectory = @"C:\myfolder\";
startInfo1.Arguments = "-cmd1 x -cmd2 y  < c:\\yesfile.txt";

问题在于

当我调试并获取 .Arguments 并从 cmd 行执行时,工作正常。从代码运行,我得到 ​​p>

Invalid command line parameters: <

四处搜索,我找不到从代码中执行此操作(传递数据)的方法。我正在调用的 exe 不会将“y”作为 cmd 行 arg,因此我必须将其从文件中传入才能像这样自动运行。

更新:如何获取标准输入并传入 y(基于答案)- 确保你也 RedirectStandardInput = true;

       StreamWriter inputWriter = myProcess.StandardInput;
                    inputWriter.Write("y");
                    inputWriter.Flush();
                    inputWriter.Close();

【问题讨论】:

  • 不应该是startInfo1.UseShellExecute = true;&lt;, &gt;, | 由 shell (cmd) 处理。
  • 试过了 - Process 对象必须将 UseShellExecute 属性设置为 false 才能重定向 IO 流。我也打开了重定向输入/输出并尝试过,它似乎也不起作用。

标签: c# command-line batch-file


【解决方案1】:

这是因为重定向 &lt;&gt; 等是由 shell 而不是由 Windows 处理的 - 您不能在 Process.Start 中使用它们。

您可以改为使用包含“y”和标志 startInfo1.RedirectStandardInput = true; 的流为您的 Process.StandardInput 播种

【讨论】:

  • 你有这方面的例子吗?
猜你喜欢
  • 1970-01-01
  • 2018-12-17
  • 2011-08-11
  • 2020-04-23
  • 1970-01-01
  • 2017-06-12
  • 2013-02-27
  • 1970-01-01
相关资源
最近更新 更多