Process p = new Process();
p.StartInfo.FileName = @"\run.bat";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
txtOutput.Text = p.StandardOutput.ReadToEnd();  
p.Close(); 问题在于,run.bat将会运行很长时间,其间输出的信息也很多,有没有办法输出一条信息就显示一条,而不是等都运行完了再输出全部阿

解决方案 »

  1.   

    p.StandardOutput.ReadToEnd();  //别用这个!  你在p.StandardOutput.@#$@#$;
      

  2.   

    采用这种方式,可以边运行边输出结果
     Process p = new Process();
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.FileName = _sysPath;
                    p.StartInfo.Arguments = _cmdStr;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.CreateNoWindow = true;
                    //string temp = null;
                    string[] strOutput = new string[2];
                    p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                    p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
                    p.Start();
                    // Start the asynchronous read of the standard output stream.
                    p.BeginOutputReadLine();                p.BeginErrorReadLine();
                    // Let the net command run, collecting the output.
                    p.WaitForExit();private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
            {            ErrString += e.Data;
            }        private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
            {
                OutString += e.Data;
            }
      

  3.   

    Process p = new Process();
    p.StartInfo.FileName = @"\run.bat";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = true;
    p.Start();
    string text = p.StandardOutput.ReadLine();//改这句
                               txtOutput.Text+=text;//加这句
    p.Close();