使用带有重定向输出流的 .NET Process.Start 执行时,FIND shell 命令不起作用
我在 bat 文件中使用 Windows shell find 命令时遇到问题。 find 命令的输出始终为空。 Bat 文件是在 C# 中使用 .NET 的 Process.Start 方法执行的。我使用输出流重定向。我想要做什么:
ProcessStartInfo processInfo = new ProcessStartInfo("c:\test.bat")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process testProcess = new Process();
testProcess.EnableRaisingEvents = true;
testProcess.OutputDataReceived += new DataReceivedEventHandler(testProcess_OutputDataReceived);
testProcess.ErrorDataReceived += new DataReceivedEventHandler(testProcess_ErrorDataReceived);
testProcess.StartInfo = processInfo;
testProcess.Start();
批处理文件(c:\ test.bat)包含带有重定向到输出文件的find命令:
find /I "TestString" "c:\TestInput.xml" > output.txt
outputStream的重定向工作正常,但output.txt的内容为空(文件大小为0B)。当我执行相同的批处理命令时,output.txt 包含找到的字符串出现次数。是否可以在批处理文件中使用 Process.Start
并重定向输出流来获取 find 命令?
感谢您的帮助。
I have problem with windows shell find command in bat file. Output of the find command is always empty. Bat file is executed using .NET's Process.Start
method in C#. I use output stream redirection. What I want to do:
ProcessStartInfo processInfo = new ProcessStartInfo("c:\test.bat")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process testProcess = new Process();
testProcess.EnableRaisingEvents = true;
testProcess.OutputDataReceived += new DataReceivedEventHandler(testProcess_OutputDataReceived);
testProcess.ErrorDataReceived += new DataReceivedEventHandler(testProcess_ErrorDataReceived);
testProcess.StartInfo = processInfo;
testProcess.Start();
Batch file (c:\test.bat) contains find command with redirection to the output file:
find /I "TestString" "c:\TestInput.xml" > output.txt
Redirection of the outputStream works fine, but the content of the output.txt is empty (File size is 0B). When I execute same batch command, output.txt contains found string occurences. Is it possible to get find command in batch file work with Process.Start
and output streams redirected?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当禁用 ShellExecute 时,您无法直接通过 Process 类启动批处理文件(并且无法在启用 ShellExecute 的情况下重定向)。这是因为批处理文件在某种意义上并不是真正可执行的,它是资源管理器中的人为构造。
无论如何,您可以做的就是直接使用 cmd.exe 来修复它,例如将 ProcessStartInfo 更改为类似以下内容:
并且还要确保等待命令退出。
You can't start a batch file directly through the Process class when ShellExecute is disabled (and you can't redirect with ShellExecute enabled). This is because batch files are not really executable in a sense, it is an artificial construct in explorer.
Anyway what you can do to fix it is to use cmd.exe directly, e.g. change your ProcessStartInfo to something like:
And also make sure you wait for the command to exit.
如果没有更多信息,就不可能说出您遇到的问题。但是,以下方法有效:
针对我的示例文件运行它所得到的结果与我使用相同参数从命令行运行
find
时得到的结果相同。这里可能会让您感到困惑的是 find 命令需要引用搜索字符串参数。
Without more info, it's impossible to say what problem you're having. However, the following works:
Running that against my sample file gives the same results as I get when I run
find
from the command line using the same arguments.The thing that might trip you up here is that the
find
command requires the search string argument to be quoted.