且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用ProcessStartInof重定向标准输出时出现问题

更新时间:2023-09-12 10:18:10

我在这里看到了两个问题.首先,您可能不仅应该使用System.Diagnostics.Process.StandardError重定向stdout,而且还要重定向stderr.如果您使用管道从控制台重定向输出,那些多平台工具总是很难看到它,但是Process使它正确.

另外,在您实际捕获StandardInputStandardError的地方也看不到任何代码.

查看此代码示例: http://msdn.microsoft.com/zh-我们/library/system.diagnostics.process.standardoutput.aspx [ ^ ].

在使用BeginOutputReadLine时,请使用以下代码示例: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx [ http://msdn.microsoft.com/en-us/library/system. diagnostics.process.aspx [ ^ ].

—SA
I see a couple of problems here. First, you should probably redirect not only stdout but also stderr using System.Diagnostics.Process.StandardError. Those multiplatform tool always make it difficult to see it if you redirect the output from console using pipe, but Process makes it right.

Also, I don''t see any code where you actually capture StandardInput or StandardError.

Look at this code sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

As you''re using BeginOutputReadLine, use this code sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx[^].

I don''t think you really need asynchronous read operations though. It''s much more straightforward to use a separate thread for creation process and waiting for its end. I think asynchronous operations were only good before threads were introduced. If you use my advice and use thread (which I highly recommend), you can use the pattern shown in a first code sample. In this thread, you can safely use the blocking call to System.Diagnostics.Process.WaitForExit().

See also http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

—SA


尝试:
using System;
using System.Diagnostics;
 
namespace RedirectExample
{
    class Program
    {
        static void Main()
        {
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo( "C:\\Win16App\\Perl\\bin\\perl.exe", scriptPath )
                {
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                };
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            Console.WriteLine(output);
            Console.Read();
        }
    }
}



还请记住, 64位Windows不支持16位应用程序 [



Also remember that 64 bit Windows does not support 16 bit applications[^].

Best regards
Espen Harlinn


尝试添加以下内容:

Try adding this:

myProcessStartInfo.EnableRaisingEvents = true;