使用ProcessStartInof重定向标准输出时出现问题 [英] Problem Redirecting Standard output with ProcessStartInof

查看:68
本文介绍了使用ProcessStartInof重定向标准输出时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,在这种情况下,我需要捕获perl脚本发出的控制台输出.
我已使用下面的代码块来完成此操作

I have scenario where in i Need to capture the output from console emitted by perl scripts.
I have used the below code block to accomplish this

myProcessStartInfo = new ProcessStartInfo("C:\\Win16App\\Perl\\bin\\perl.exe");
                myProcessStartInfo.Arguments = scriptPath;
                myProcessStartInfo.UseShellExecute = false;
                // myProcessStartInfo.RedirectStandardOutput = true;
                myProcessStartInfo.RedirectStandardOutput = true;
                myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                myProcessStartInfo.CreateNoWindow = true;
                myProcess.StartInfo = myProcessStartInfo;
                myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
                                               
 myProcess.Start();
                //  m_progBar.Value = 100;
                // myProcess.StandardOutput.ReadLine();
                myProcess.BeginOutputReadLine();

推荐答案

我在这里看到了两个问题.首先,您可能不仅应该使用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;


这篇关于使用ProcessStartInof重定向标准输出时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆