2016-05-26 125 views
0

我正在通過命令行運行一個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(); 

回答

0

我不知道很多關於ProcessStartInfo但我用Process前後辦法脫身標準輸出的信息,如下所示,我假設僅僅通過訪問StandardOutput

Process cmd = new Process(); 

cmd.StartInfo.FileName = "cmd.exe"; 
cmd.StartInfo.RedirectStandardInput = true; 
cmd.StartInfo.RedirectStandardOutput = true; 
cmd.StartInfo.CreateNoWindow = false; 
cmd.StartInfo.UseShellExecute = false; 
cmd.Start(); 

cmd.StandardInput.WriteLine(command); 
cmd.StandardInput.Flush(); 
cmd.StandardInput.Close(); 

var output = cmd.StandardOutput.ReadToEnd(); 

cmd.WaitForExit(); 
0

此代碼工作對我來說應該是類似的方式:

const int MAX_EXIT_WAIT_TIME = 3000; 
    // Fill needed data 
    string username = ""; 
    string password = ""; 
    string domain = ""; 
    string appName = ""; 

    var dir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); 
    var appFullPath = Path.Combine(dir, appName); 
    ProcessStartInfo psi = new ProcessStartInfo(appFullPath); 
    psi.UserName = username; 
    var securePass = new System.Security.SecureString(); 
    foreach (var c in password) 
     securePass.AppendChar(c); 
    psi.Password = securePass; 
    psi.Domain = domain; 
    psi.LoadUserProfile = false; 
    psi.WorkingDirectory = dir; 
    psi.Arguments = ""; 
    psi.RedirectStandardOutput = true; 
    // Create Process object, but not start it! 
    var proc = new Process(); 
    proc.StartInfo = psi; 
    StringCollection values = new StringCollection(); 
    DataReceivedEventHandler outputDataReceived = (o, e) => 
    { 
     lock (values) 
      values.Add(e.Data); 
    }; 
    try 
    { 
     proc.OutputDataReceived += outputDataReceived; 
     // Only here we start process 
     if (!proc.Start()) 
      throw new InvalidOperationException("Couldn't start app"); 
     proc.BeginOutputReadLine(); 
     proc.WaitForExit(MAX_EXIT_WAIT_TIME); 
    } 
    finally { proc.OutputDataReceived -= outputDataReceived; } 
    Console.WriteLine("Read {0} ", values.Count); 
    foreach (var item in values) 
     Console.WriteLine(" {0}", item); 
+0

PatchInstaller.ex e它將靜默執行,然後它將調用PatchInstaller.exe中的其他進程,並且將獲得C:\ Users \ sysadmin>開始設置 UI模式:無提示 錯誤:正在運行的進程衝突。 退出代碼:19所以我需要閱讀這一行。我在這裏被卡住了。 –

+0

我不明白你說的是什麼。 –

+0

感謝您的回覆。但我不是在尋找你在這裏完成的一樣。當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;(需要讀取這條線。) –