如何利用其他流程' `stderr`和`stdout`?

时间:2018-04-30 21:18:06

标签: c# .net process .net-core stdout

这个问题与提出的问题不同:

  1. 我的问题非常具体到。即即使有经典的解决方案" .NET Framework可能不适用于.NET Core环境。
  2. 发布的问题之间存在重大差距(8年!)。
  3. 将其视为副本,因为它不是。

    我发现自己无法点击在我的应用程序之外创建stderrstdout个流程。

    targetProcessList的类型为List<System.Diagnostics.Process>

    以下代码

    foreach (var proc in targetProcessList)
    {
        proc.StandardError.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
        proc.StandardOutput.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
    }
    

    导致InvalidOperationException(可能是因为没有为此类用法配置进程):

    enter image description here

    [c:\           HRIS.Web] dotnet run :       User profile is available. Using '                 \AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
    [c:\           HRIS.Web] dotnet run :
    [c:\           HRIS.SPA] npm run start-prodish-for-e2e ||| [12044] cmd
    [c:\           HRIS.SPA] npm run start-prodish-for-e2e ||| [11080] conhost
    System.InvalidOperationException: StandardError has not been redirected.
       at System.Diagnostics.Process.get_StandardError()
       at RunE2E.Program.StartProcessViaCmd(String command, String arguments, String workingDirectory)
       at RunE2E.Program.Main(String[] args)
    System.NullReferenceException: Object reference not set to an instance of an object.
       at RunE2E.Program.Main(String[] args)
    

    所以,我试图像这样动态启用重定向:

    foreach (var proc in targetProcessList)
    {
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StandardError.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
        proc.StandardOutput.ReadToEndAsync().ContinueWith(t => Console.WriteLine(t.Result));
    }
    

    但在运行时仍然受到打击

    enter image description here

    [c:\           \HRIS.Web] dotnet run :       User profile is available. Using '                 \AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
    [c:\           \HRIS.Web] dotnet run :
    [c:\           \HRIS.SPA] npm run start-prodish-for-e2e ||| [14952] cmd
    [c:\           \HRIS.SPA] npm run start-prodish-for-e2e ||| [10784] conhost
    System.InvalidOperationException: Process was not started by this object, so requested information cannot be determined.
       at System.Diagnostics.Process.get_StartInfo()
       at RunE2E.Program.StartProcessViaCmd(String command, String arguments, String workingDirectory)
       at RunE2E.Program.Main(String[] args)
    System.NullReferenceException: Object reference not set to an instance of an object.
       at RunE2E.Program.Main(String[] args)
    

    有办法解决这个问题吗?

0 个答案:

没有答案