在c#中重定向过程StandardInput,StandardOutput和StandardError [英] Redirecting Process StandardInput, StandardOutput and StandardError in c#

查看:346
本文介绍了在c#中重定向过程StandardInput,StandardOutput和StandardError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用批处理文件编译java程序。如果程序不需要任何输入但在需要输入时创建死锁条件,则代码有效以下代码:



I want to compile a java program using batch file. Code works if the program does not need any input but creates a deadlock condition when it needs input Here is the code:

public void compilefile(ref System.Windows.Controls.RichTextBox rtb,ref TabItem tabitem)
   {
       var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

       FileInfo filetocompile = new FileInfo(Path.GetTempPath() + tabitem.Header.ToString());
       using (FileStream fs = filetocompile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
       {
           range.Save(fs, System.Windows.DataFormats.Text);
           fs.Close();
       }

    try{

           pf = new ProcessStartInfo(@"E:\com.bat");
           pf.FileName = @"E:\com.bat";
           pf.Arguments = string.Format("{0} {1} {2} {3} {4}", "\"C:\\Program Files (x86)\\Java\\jdk1.7.0_13\\bin\\\"", "javac", tabitem.Header.ToString(), "java", tabitem.Header.ToString().Substring(0, tabitem.Header.ToString().LastIndexOf('.')));
           pf.CreateNoWindow = true;
           pf.ErrorDialog = false;
           pf.UseShellExecute = false;
           pf.RedirectStandardInput = true;
           pf.RedirectStandardOutput = true;
           pf.RedirectStandardError = true;
           pf.WorkingDirectory = Path.GetTempPath();
           p = new Process();
           p.StartInfo = pf;
           p.Start();
           input = p.StandardInput;
           string inputstring = "";
           input.AutoFlush = true;

           do
           {
               if (p.StandardError.ReadToEnd() != "")
               {
                   consoleTextBlock.IsDocumentEnabled = false;
                   consoleTextBlock.AppendText(p.StandardOutput.ReadToEnd());
                   consoleTextBlock.AppendText(p.StandardError.ReadToEnd());
                }
               else
               {
                   consoleTextBlock.AppendText(p.StandardOutput.ReadToEnd());
                   consoleTextBlock.AppendText(p.StandardError.ReadToEnd());

               }

               consoleTextInput.Focus();
               consoleTextBlock.ScrollToEnd();
               consoleTextBlock.IsReadOnly = true;
               System.Windows.MessageBox.Show(p.ExitCode.ToString());
           } while (p.HasExited != true);
       }
       catch (Exception ex)
       {
           System.Windows.MessageBox.Show(ex.ToString());

       }
   }

推荐答案

这篇关于在c#中重定向过程StandardInput,StandardOutput和StandardError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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