【发布时间】: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;?<, >, |由 shell (cmd) 处理。 -
试过了 - Process 对象必须将 UseShellExecute 属性设置为 false 才能重定向 IO 流。我也打开了重定向输入/输出并尝试过,它似乎也不起作用。
标签: c# command-line batch-file