C# 无法从重定向标准输入获取输入

C# 无法从重定向标准输入获取输入,c#,process,fortran,redirectstandardoutput,C#,Process,Fortran,Redirectstandardoutput,我需要为我的C#进程获取一个enter命令 我的C#代码适用于测试批处理文件,而不适用于fortran控制台应用程序 我读了这篇文章: 但这对我不起作用 谁能给我一些建议 我的示例批处理文件(test.bat): 注意:此批处理文件模拟我的另一个应用程序 @echo off cls dir echo "please input enter key" pause tree 我的C#代码: 私有进程_进程=null; 私有bool_bEnterCR=false; 私有无效开始\单击(对象发送者,路

我需要为我的C#进程获取一个enter命令

我的C#代码适用于测试批处理文件,而不适用于fortran控制台应用程序

我读了这篇文章:

但这对我不起作用

谁能给我一些建议

我的示例批处理文件(test.bat):

注意:此批处理文件模拟我的另一个应用程序

@echo off
cls
dir
echo "please input enter key"
pause
tree
我的C#代码:

私有进程_进程=null;
私有bool_bEnterCR=false;
私有无效开始\单击(对象发送者,路由目标)
{
this.tbOutput.Text=“”;
_bEnterCR=假;
如果(空!=\u进程)
{
_process.Dispose();
}
字符串strPathName=System.IO.Path.GetDirectoryName(Assembly.getExecutionGassembly().Location);
//使用ProcessStartInfo类
_流程=新流程();
_process.StartInfo.CreateNoWindow=true;
_process.StartInfo.UseShellExecute=false;
_process.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
_process.StartInfo.FileName=strPathName+“\\test.bat”;
_process.StartInfo.WorkingDirectory=strPathName+“\\Output\\”;
_process.StartInfo.RedirectStandardError=true;
_process.StartInfo.RedirectStandardInput=true;
_process.StartInfo.RedirectStandardOutput=true;
//_process.EnableRaisingEvents=true;
_process.OutputDataReceived+=新的DataReceivedEventHandler(OnOutputDataReceived);
_process.ErrorDataReceived+=新的DataReceivedEventHandler(OnOutputDataReceived);
//_process.Exited+=新的EventHandler(OnProcessExited);
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
}
接收到OutputDataReceived(对象发送方,DataReceivedEventArgs e)的私有无效
{
if(String.IsNullOrEmpty(e.Data)==false)
{
如果(例如,数据包含(“请输入输入键”)&&&&u bEnterCR==false)
{
WriteLine(“找到暂停,输入命令”);
//适用于批处理文件,不适用于控制台应用程序
_process.StandardInput.Write(@“\r\n”);
_bEnterCR=真;
}
新线程(()=>
{
this.Dispatcher.Invoke(新操作(()=>
{
tbOutput.AppendText(e.Data+Environment.NewLine);
}));
}).Start();
}
}

我只需要向fortran应用程序中的fortran pause发送一个enter命令。会发生什么情况,您会进入“pause found”行吗?您能修改fortran吗?Fortran的pause语句是一个依赖于实现的行为不良的语句(例如,有时您必须键入“go”)。如果可以的话,可以将暂停转换为一个字符串。是的,我可以得到“pause found”行。但它显示了两次“Fortran暂停”。最后3行文本:第1行(Fortran Pause)、第2行(系统找不到指定的路径)、第3行(Fortran Pause)。然后它停止了。我没有fortran应用程序的源代码。
   private Process _process = null;
   private bool _bEnterCR = false;
   private void Begin_Click(object sender, RoutedEventArgs e)
    {
        this.tbOutput.Text = "";
        _bEnterCR = false;

        if (null != _process)
        {
            _process.Dispose();
        }

        string strPathName = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);            
        // Use ProcessStartInfo class
        _process = new Process();
        _process.StartInfo.CreateNoWindow = true;
        _process.StartInfo.UseShellExecute = false;
        _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        _process.StartInfo.FileName = strPathName + "\\test.bat";
        _process.StartInfo.WorkingDirectory = strPathName + "\\Output\\";
        _process.StartInfo.RedirectStandardError = true;
        _process.StartInfo.RedirectStandardInput = true;
        _process.StartInfo.RedirectStandardOutput = true;
        //_process.EnableRaisingEvents = true;

        _process.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
        _process.ErrorDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);            

        //_process.Exited += new EventHandler(OnProcessExited);

        _process.Start();

        _process.BeginOutputReadLine();
        _process.BeginErrorReadLine();
    }

    private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (String.IsNullOrEmpty(e.Data) == false)
        {
        if (e.Data.Contains("please input enter key") && _bEnterCR == false)
            {
                Debug.WriteLine("Pause Found, Entering <CR> command");
                // work for batch file, not for console application
                _process.StandardInput.Write(@"\r\n");
                _bEnterCR = true;
            }
            new Thread(() =>
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    tbOutput.AppendText(e.Data + Environment.NewLine);
                }));
            }).Start();
        }
    }