通过C#输入输出重定向与OutputDataReceived ,将cmd的运行程序,隐藏到后台执行

问题:

  有个exe程序,运行后会显示cmd界面,将需要输入两次"K"进行确认。才会全部执行完毕。现需要将其cmd界面及输入过程全部隐藏到后台执行。

解法:通过C#输入输出重定向,并在OutputDataReceived ()中实现输入C的动作来实现,可详见具体代码如下:

                  private Process _p = null;

    ...

  if (_p == null)
                {
                    _p = new Process();
                    _p.StartInfo.UseShellExecute = false;
                    _p.StartInfo.CreateNoWindow = true;
                    _p.StartInfo.RedirectStandardInput = true;
                    _p.StartInfo.RedirectStandardOutput = true;
                    _p.StartInfo.RedirectStandardError = true;
                }
                Environment.CurrentDirectory = Application.StartupPath;
                _p.StartInfo.FileName = Application.StartupPath+ "\\test.exe";
                _p.OutputDataReceived += new DataReceivedEventHandler((sender1, e1) =>
                {
                    if (!string.IsNullOrEmpty(e1.Data))
                    {
                        string sData= e1.Data;
                        Debug.WriteLine(sData);
                        if (sData.IndexOf("output 1") > 1)
                        {
                            _p.StandardInput.AutoFlush = true;           //写入命令     
                            _p.StandardInput.WriteLine("K");
                        }
                        else if (sData.IndexOf("output 2) > 1)
                        {
                            _p.StandardInput.AutoFlush = true;           //写入命令     
                            _p.StandardInput.WriteLine("K");
                        }
                    }
                });
                _p.ErrorDataReceived += new DataReceivedEventHandler((sender1, e1) =>
                {
                    if (!string.IsNullOrEmpty(e1.Data))
                    {
                        Debug.WriteLine( e1.Data);
                    }
                });
                _p.Start();
                _p.BeginOutputReadLine();
                _p.BeginErrorReadLine();
                _p.WaitForExit();
                _p.Close();
                _p = null;
                    


     
          
         

好的,针对你的问题,以下是关于 C# 输入输出重定向的方法一:同步 Process 的解答。 在 C# 中,可以使用 Process 类来执行外部程序,并且可以通过重定向输入和输出流来实现与外部程序的交互。具体步骤如下: 1. 创建 ProcessStartInfo 对象,设置 FileName 属性为要执行的外部程序的路径,设置 RedirectStandardInput 和 RedirectStandardOutput 属性为 true,表示重定向输入和输出流。 2. 创建 Process 对象,设置 StartInfo 属性为上述 ProcessStartInfo 对象,然后调用 Start 方法启动进程。 3. 使用 Process 对象的 StandardInput 属性获取重定向的输入流,使用 WriteLine 方法向外部程序写入数据。 4. 使用 Process 对象的 StandardOutput 属性获取重定向的输出流,使用 ReadLine 方法从外部程序读取数据。 以下是一个简单的示例代码: ``` using System; using System.Diagnostics; class Program { static void Main() { var startInfo = new ProcessStartInfo { FileName = "外部程序路径", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false }; var process = new Process { StartInfo = startInfo }; process.Start(); var standardInput = process.StandardInput; var standardOutput = process.StandardOutput; standardInput.WriteLine("要写入的数据"); var output = standardOutput.ReadLine(); Console.WriteLine(output); process.WaitForExit(); } } ``` 以上就是 C# 输入输出重定向方法一:同步 Process 的解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

diaya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值