如何使用C#从现有命令进程读取命令输出 [英] How read command output from existing command process using C#

查看:144
本文介绍了如何使用C#从现有命令进程读取命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我需要输出命令窗口,它已经存在的进程,从这个过程我需要读取输出(命令行输出)使用c#。



注意:我没有从这里开始任何过程。进程已在差异处执行,需要从命令行验证字符串。



Regard's

Suri



我尝试了什么:



通过使用这个我得到输出,我需要没有进程启动已经运行进程需要获取输出数据:



Dear all,

I need to get output of command window , its already existing process , from this process i need to read output(command line output) by using c#.

Note: I am not starting any process from here. Process already executed in diff place and need to verify string from command line.

Regard's
Suri

What I have tried:

By using this i am getting output , i need without process start already running process need to get output data :

string command = "/c " + richTextBox2.Text;

           ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);

           Process proc = new Process();
           proc.StartInfo = procStartInfo;
          // proc.Start();
           procStartInfo.RedirectStandardOutput = true;
           procStartInfo.UseShellExecute = false;
           proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
           proc.Start();
           proc.BeginOutputReadLine();
           proc.WaitForExit();










void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
       {
           if (e.Data != null)
           {
               string newLine = e.Data.Trim() + Environment.NewLine;
             //  MethodInvoker append = () => richTextBox1.Text += newLine;
               MethodInvoker append = () => richTextBox1.AppendText(newLine);
               richTextBox1.BeginInvoke(append);
           }
       }

推荐答案

你做不到。 Youo只能在流程运行时获得流程输出。您可以从StandardOut变量获得。请阅读此处:



Process.StandardOutput属性(System.Diagnostics) [ ^ ]
You can't do that. Youo can only get process output as it's happening from the process as it's running. You get that from the StandardOut variable. Read up on that here:

Process.StandardOutput Property (System.Diagnostics)[^]


这篇关于如何使用C#从现有命令进程读取命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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