使用带有重定向输出流的 .NET Process.Start 执行时,FIND shell 命令不起作用

发布于 2024-12-11 15:25:32 字数 1008 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-12-18 15:25:32

当禁用 ShellExecute 时,您无法直接通过 Process 类启动批处理文件(并且无法在启用 ShellExecute 的情况下重定向)。这是因为批处理文件在某种意义上并不是真正可执行的,它是资源管理器中的人为构造。

无论如何,您可以做的就是直接使用 cmd.exe 来修复它,例如将 ProcessStartInfo 更改为类似以下内容:

new ProcessStartInfo(@"cmd.exe", @"/c C:\test.bat")

并且还要确保等待命令退出。

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:

new ProcessStartInfo(@"cmd.exe", @"/c C:\test.bat")

And also make sure you wait for the command to exit.

浮萍、无处依 2024-12-18 15:25:32

如果没有更多信息,就不可能说出您遇到的问题。但是,以下方法有效:

var find = new Process();
var psi = find.StartInfo;
psi.FileName = "find.exe";
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;

// remember to quote the search string argument
psi.Arguments = "\"quick\" xyzzy.txt";

find.Start();

string rslt = find.StandardOutput.ReadToEnd();

find.WaitForExit();

Console.WriteLine("Result = {0}", rslt);

Console.WriteLine();
Console.Write("Press Enter:");
Console.ReadLine();
return 0;

针对我的示例文件运行它所得到的结果与我使用相同参数从命令行运行 find 时得到的结果相同。

这里可能会让您感到困惑的是 find 命令需要引用搜索字符串参数。

Without more info, it's impossible to say what problem you're having. However, the following works:

var find = new Process();
var psi = find.StartInfo;
psi.FileName = "find.exe";
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;

// remember to quote the search string argument
psi.Arguments = "\"quick\" xyzzy.txt";

find.Start();

string rslt = find.StandardOutput.ReadToEnd();

find.WaitForExit();

Console.WriteLine("Result = {0}", rslt);

Console.WriteLine();
Console.Write("Press Enter:");
Console.ReadLine();
return 0;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文