如何从流程中的流程中读取流程输出消息? [英] How to read process Output Message from process within the process ??

查看:91
本文介绍了如何从流程中的流程中读取流程输出消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am running an exe through commandline and getting following output.

C:\ Users \ sysadmin> C:\ Users \ sysadmin \ Desktop \ New_folder \ Setup \ PatchInstaller.exe --mode = silent

C:\ Users \ sysadmin开始设置UI模式:静默错误:另一个实例正在运行,一次只能运行一个实例.退出代码:11

我正在通过System.daignostics.process运行此程序.

i am running this through System.daignostics.process.

我的问题是 PatchInstaller.exe ,调用另一个进程,该嵌套进程的输出在cmd中可见.但是相同的结果和退出代码我无法通过 PatchInstaller.exe 的Process对象获得. 有什么方法可以使流程中运行的流程输出?

My issue is PatchInstaller.exe calling another process and the output of that nested process is what is visible with cmd. but the same result and exit code i am not able to get through Process object ofPatchInstaller.exe. Is there any way of getting output of process running within process?

以下是我讨厌的代码...

Following is the code i have tired... 

string command = @"C:\Users\sysadmin\Desktop\Setup\PatchInstaller.exe";
            string result = string.Empty;
            System.Diagnostics.ProcessStartInfo procStartInfo = new ProcessStartInfo();
            procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command + " --mode=silent);
            System.Diagnostics.Process proc = new Process();
            procStartInfo.ErrorDialog = false;
            procStartInfo.UseShellExecute = false;
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.RedirectStandardError = true;
            procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;

            if (!string.IsNullOrEmpty(domain) && !string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pwd))
            {
                procStartInfo.Domain = domain;
                procStartInfo.UserName = user;

                System.Security.SecureString ss = new System.Security.SecureString();
                foreach (char c in pwd) { ss.AppendChar(c); }
                procStartInfo.Password = ss;
            }

            proc = System.Diagnostics.Process.Start(procStartInfo);

            proc.ErrorDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs errorLine)
            {
                if (errorLine.Data != null) result += "error:" + errorLine.Data +;

            };
            proc.OutputDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs outputLine)
            {
                if (outputLine.Data != null) result += outputLine.Data +;

            };
            proc.BeginErrorReadLine();
            proc.BeginOutputReadLine();
            proc.WaitForExit();

推荐答案

嗨ArunCSharp,

Hi ArunCSharp,

由于我不知道如何编写 PatchInstaller.exe .我尝试使用Process.Start()方法调用另一个进程(一个exe),在我这方面工作正常,没有错误.

Since I don't know how to write PatchInstaller.exe on your side. I tried to use Process.Start() method to call another process(An exe), It works fine on my side, there is no error.

>>是否有任何方法可以获取进程中正在运行的进程的输出?

>>Is there any way of getting output of process running within process?

据我所知,根据上述消息,我们可以使用

From above message, as far as I know, we could use Process.StandardOutput Property to get a stream used to read the textual output of the application.

例如,以下C#代码显示如何从重定向的流中读取并等待子进程退出.

The following C# code, for example, shows how to read from a redirected stream and wait for the child process to exit.

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();

// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

最诚挚的问候,

克里斯汀


这篇关于如何从流程中的流程中读取流程输出消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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