我正在通过命令行运行一个exe文件并获得跟随输出。如何读取进程输出进程中的进程消息?
C:\ Users \用户系统管理员> C:\ Users \用户系统管理员\桌面\ New_folder \设置\ PatchInstaller.exe --mode =无声
C:\ Users \用户系统管理员开始设置UI模式:无提示错误: 正在运行另一个实例,一次只能运行一个实例。 退出代码:11
我公司通过System.daignostics.process运行此。
我的问题是PatchInstaller.exe调用另一个进程,并且该嵌套进程的输出是使用cmd可见的。但相同的结果和退出代码,我无法通过PatchInstaller.exe的进程对象。 有没有什么办法可以让程序在进程内运行? 以下是代码,我累了...
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();
Process[] pname = Process.GetProcessesByName("PatchInstaller");
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}
proc.WaitForExit();
PatchInstaller.ex e它将静默执行,然后它将调用PatchInstaller.exe中的其他进程,并且将获得C:\ Users \ sysadmin>开始设置 UI模式:无提示 错误:正在运行的进程冲突。 退出代码:19所以我需要阅读这一行。我在这里被卡住了。 –
我不明白你说的是什么。 –
感谢您的回复。但我不是在寻找你在这里完成的一样。当exe直接执行并在命令行下面给出输出时,这是一个可用的代码。 这里我的exe文件正在运行,它将首先执行下面的行, C:\ Users \ sysadmin \ Desktop \ New_folder \ Setup \ PatchInstaller.exe --mode = silent 现在PatchInstaller.exe在内部执行其他进程,我是(EX:C:\ Users \ New_folder \ Setup \ PatchInstaller.exe --mode = silent(回车)C:\ Windows \ system32>开始安装程序退出代码:19;(需要读取这条线。) –