Process.StandardOutput Свойство

Определение

Получает поток, используемый для чтения текстовых выходных данных приложения.

public:
 property System::IO::StreamReader ^ StandardOutput { System::IO::StreamReader ^ get(); };
public System.IO.StreamReader StandardOutput { get; }
[System.ComponentModel.Browsable(false)]
public System.IO.StreamReader StandardOutput { get; }
member this.StandardOutput : System.IO.StreamReader
[<System.ComponentModel.Browsable(false)>]
member this.StandardOutput : System.IO.StreamReader
Public ReadOnly Property StandardOutput As StreamReader

Значение свойства

StreamReader

Объект StreamReader, используемый для чтения стандартного потока вывода приложения.

Атрибуты

Исключения

Поток StandardOutput не был определен для переадресации. Убедитесь, что для RedirectStandardOutput задано true, а для UseShellExecutefalse.

-или-

Поток StandardOutput был открыт для асинхронных операций чтения с BeginOutputReadLine().

Примеры

В следующем примере выполняется команда ipconfig.exe и перенаправляет стандартные выходные данные в окно консоли примера.

using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;

int main()
{
    Process^ process = gcnew Process();
    process->StartInfo->FileName = "ipconfig.exe";
    process->StartInfo->UseShellExecute = false;
    process->StartInfo->RedirectStandardOutput = true;
    process->Start();

    // Synchronously read the standard output of the spawned process-> 
    StreamReader^ reader = process->StandardOutput;
    String^ output = reader->ReadToEnd();

    // Write the redirected output to this application's window.
    Console::WriteLine(output);

    process->WaitForExit();
    process->Close();

    Console::WriteLine("\n\nPress any key to exit");
    Console::ReadLine();
    return 0;
}
using System;
using System.IO;
using System.Diagnostics;

class StandardOutputExample
{
    public static void Main()
    {
        using (Process process = new Process())
        {
            process.StartInfo.FileName = "ipconfig.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();

            // Synchronously read the standard output of the spawned process.
            StreamReader reader = process.StandardOutput;
            string output = reader.ReadToEnd();

            // Write the redirected output to this application's window.
            Console.WriteLine(output);

            process.WaitForExit();
        }

        Console.WriteLine("\n\nPress any key to exit.");
        Console.ReadLine();
    }
}
Imports System.IO
Imports System.Diagnostics

Module Module1
    Sub Main()
        Using process As New Process()
            process.StartInfo.FileName = "ipconfig.exe"
            process.StartInfo.UseShellExecute = False
            process.StartInfo.RedirectStandardOutput = True
            process.Start()

            ' Synchronously read the standard output of the spawned process. 
            Dim reader As StreamReader = process.StandardOutput
            Dim output As String = reader.ReadToEnd()
            Console.WriteLine(output)

            process.WaitForExit()
        End Using

        Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
        Console.ReadLine()
    End Sub
End Module

Комментарии

Когда текст записывается в стандартный Process поток, этот текст обычно отображается в консоли. Перенаправляя StandardOutput поток, вы можете управлять выходными данными процесса или подавлять их. Например, можно отфильтровать текст, отформатировать его по-разному или записать выходные данные как в консоль, так и в указанный файл журнала.

Примечание

Чтобы использоватьStandardOutput, необходимо задать значение ProcessStartInfo.UseShellExecute false, а для него необходимо задать значение trueProcessStartInfo.RedirectStandardOutput . В противном случае при чтении StandardOutput из потока возникает исключение.

Перенаправленный StandardOutput поток можно считывать синхронно или асинхронно. Такие методы, как Read, ReadLineи ReadToEnd выполняют синхронные операции чтения в выходном потоке процесса. Эти синхронные операции чтения не завершаются до тех пор, пока связанные Process операции записи в поток StandardOutput не закроются или не закроют поток.

Напротив, BeginOutputReadLine запускает асинхронные операции чтения в потоке StandardOutput . Этот метод включает назначенный обработчик событий для вывода потока и немедленно возвращается вызывающей объекту, который может выполнять другую работу, пока поток направляется в обработчик событий.

Синхронные операции чтения представляют зависимость между вызывающим чтением StandardOutput из потока и дочерним процессом записи в этот поток. Эти зависимости могут привести к взаимоблокировке. Когда вызывающий объект считывает из перенаправленного потока дочернего процесса, он зависит от дочернего процесса. Вызывающий объект ожидает операции чтения, пока дочерний объект не запишет в поток или не закроет поток. Когда дочерний процесс записывает достаточно данных для заполнения перенаправленного потока, он зависит от родительского элемента. Дочерний процесс ожидает следующей операции записи, пока родитель не считывает из полного потока или не закроет поток. Условие взаимоблокировки приводит к тому, что вызывающий и дочерний процесс ожидают завершения операции и не может продолжаться. Чтобы избежать взаимоблокировок, оцените зависимости между вызывающим и дочерним процессом.

В последних двух примерах в этом разделе используется Start метод запуска исполняемого файла с именем Write500Lines.exe. В следующем примере содержится исходный код.

using System;
using System.IO;

public class Example3
{
   public static void Main()
   {
      for (int ctr = 0; ctr < 500; ctr++)
         Console.WriteLine($"Line {ctr + 1} of 500 written: {ctr + 1/500.0:P2}");

      Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n");
   }
}
// The example displays the following output:
//      The last 50 characters in the output stream are:
//      ' 49,800.20%
//      Line 500 of 500 written: 49,900.20%
//'
//
//      Error stream: Successfully wrote 500 lines.
Imports System.IO

Public Module Example
   Public Sub Main()
      For ctr As Integer = 0 To 499
         Console.WriteLine($"Line {ctr + 1} of 500 written: {ctr + 1/500.0:P2}")
      Next

      Console.Error.WriteLine($"{vbCrLf}Successfully wrote 500 lines.{vbCrLf}")
   End Sub
End Module
' The example displays the following output:
'      The last 50 characters in the output stream are:
'      ' 49,800.20%
'      Line 500 of 500 written: 49,900.20%
'
'
'      Error stream: Successfully wrote 500 lines.

В следующем примере показано, как считывать из перенаправленного потока и ожидать завершения дочернего процесса. В этом примере не создается условие взаимоблокировки путем вызова до p.WaitForExitвызоваp.StandardOutput.ReadToEnd. Условие взаимоблокировки может привести к тому, что родительский процесс вызывается p.WaitForExit до p.StandardOutput.ReadToEnd и дочерний процесс записывает достаточно текста для заполнения перенаправленного потока. Родительский процесс будет ждать бесконечного завершения дочернего процесса. Дочерний процесс будет ждать неограниченное время, пока родитель будет считывать из полного StandardOutput потока.

using System;
using System.Diagnostics;

public class Example2
{
   public static void Main()
   {
      var 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();

      Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
   }
}
// The example displays the following output:
//      Successfully wrote 500 lines.
//
//      The last 50 characters in the output stream are:
//      ' 49,800.20%
//      Line 500 of 500 written: 49,900.20%
//      '
Imports System.Diagnostics'

Public Module Example
   Public Sub Main()
      Dim p As 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.  
      Dim output As String = p.StandardOutput.ReadToEnd()  
      p.WaitForExit()

      Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'")
   End Sub
End Module
' The example displays the following output:
'      Successfully wrote 500 lines.
'
'      The last 50 characters in the output stream are:
'      ' 49,800.20%
'      Line 500 of 500 written: 49,900.20%
'      '

При чтении всего текста из стандартных выходных и стандартных потоков ошибок возникает аналогичная проблема. В следующем примере выполняется операция чтения в обоих потоках. Это позволяет избежать взаимоблокировки, выполняя асинхронные операции чтения в потоке StandardError . Условие взаимоблокировки приводит к тому, что родительский процесс вызывается p.StandardOutput.ReadToEnd , p.StandardError.ReadToEnd а дочерний процесс записывает достаточно текста для заполнения потока ошибок. Родительский процесс будет ждать неограниченное время, пока дочерний процесс закроет поток StandardOutput . Дочерний процесс будет ждать неограниченное время, пока родитель будет считывать из полного StandardError потока.

using System;
using System.Diagnostics;

public class Example
{
   public static void Main()
   {
      var p = new Process();  
      p.StartInfo.UseShellExecute = false;  
      p.StartInfo.RedirectStandardOutput = true;  
      string eOut = null;
      p.StartInfo.RedirectStandardError = true;
      p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => 
                                 { eOut += e.Data; });
      p.StartInfo.FileName = "Write500Lines.exe";  
      p.Start();  

      // To avoid deadlocks, use an asynchronous read operation on at least one of the streams.  
      p.BeginErrorReadLine();
      string output = p.StandardOutput.ReadToEnd();  
      p.WaitForExit();

      Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'");
      Console.WriteLine($"\nError stream: {eOut}");
   }
}
// The example displays the following output:
//      The last 50 characters in the output stream are:
//      ' 49,800.20%
//      Line 500 of 500 written: 49,900.20%
//      '
//
//      Error stream: Successfully wrote 500 lines.
Imports System.Diagnostics

Public Module Example
   Public Sub Main()
      Dim p As New Process()  
      p.StartInfo.UseShellExecute = False  
      p.StartInfo.RedirectStandardOutput = True  
      Dim eOut As String = Nothing
      p.StartInfo.RedirectStandardError = True
      AddHandler p.ErrorDataReceived, Sub(sender, e) eOut += e.Data 
      p.StartInfo.FileName = "Write500Lines.exe"  
      p.Start()  

      ' To avoid deadlocks, use an asynchronous read operation on at least one of the streams.  
      p.BeginErrorReadLine()
      Dim output As String = p.StandardOutput.ReadToEnd()  
      p.WaitForExit()

      Console.WriteLine($"The last 50 characters in the output stream are:{vbCrLf}'{output.Substring(output.Length - 50)}'")
      Console.WriteLine($"{vbCrLf}Error stream: {eOut}")
   End Sub
End Module
' The example displays the following output:
'      The last 50 characters in the output stream are:
'      ' 49,800.20%
'      Line 500 of 500 written: 49,900.20%
'      '
'
'      Error stream: Successfully wrote 500 lines.

Вы можете использовать асинхронные операции чтения, чтобы избежать этих зависимостей и их взаимоблокировки. Кроме того, можно избежать условия взаимоблокировки, создав два потока и считывая выходные данные каждого потока в отдельном потоке.

Примечание

Нельзя смешивать асинхронные и синхронные операции чтения в перенаправленном потоке. После открытия перенаправленного потока объекта Process в асинхронном или синхронном режиме все дальнейшие операции чтения в этом потоке должны находиться в одном и том же режиме. Например, не следует следовать BeginOutputReadLine вызову ReadLine StandardOutput в потоке или наоборот. Однако можно читать два разных потока в разных режимах. Например, можно вызвать BeginOutputReadLine и вызвать ReadLine StandardError поток.

Применяется к

См. также раздел