C#启动进程参数传递数据
尝试将文件中的数据作为 C# 中的命令行参数传递并遇到问题,
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";
问题在于 <; c:\yesfile.txt ...
当我调试并获取 .Arguments 并从命令行执行时,工作正常。从代码运行,我到处
Invalid command line parameters: <
搜索,我找不到从代码执行此操作(传入数据)的方法。我调用的 exe 不会将“y”作为命令行参数,因此我必须从文件中传递它才能像这样自动运行它。
更新:如何获取std输入并传入ay(基于答案) - 确保您RedirectStandardInput = true;
StreamWriter inputWriter = myProcess.StandardInput;
inputWriter.Write("y");
inputWriter.Flush();
inputWriter.Close();
trying to pass in data from a file as a cmd line argument in c# and running into issues
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";
the issue is with the < c:\yesfile.txt ...
when I debug and grab the .Arguments and execute from cmd line, works fine. running from code, i get
Invalid command line parameters: <
searching around, I cant find the way to do this (pass in data) from code. The exe I am calling doesn't take in the "y" as a cmd line arg so I have to pass it in from a file to run it automatically like this.
Update: how to get the std input and pass in a y (based on answer) - make sure you RedirectStandardInput = true;
as well
StreamWriter inputWriter = myProcess.StandardInput;
inputWriter.Write("y");
inputWriter.Flush();
inputWriter.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为重定向
<
、>
等是由 shell 而不是 Windows 处理的 - 您不能从 Process.Start 使用它们。您可以改为播种
Process.StandardInput
,流中包含“y”和标志
startInfo1.RedirectStandardInput = true;
That's because the redirections
<
,>
etc. are handled by the shell and not by Windows - you can't use them from Process.Start.You could instead seed your
Process.StandardInput
with a stream containing a 'y' and flagstartInfo1.RedirectStandardInput = true;