ProcessStartInfo.RedirectStandardInput 屬性

定義

取得或設定值,表示應用程式的輸入是否從 StandardInput 資料流讀取。

C#
public bool RedirectStandardInput { get; set; }

屬性值

如果應該從 StandardInput 讀取輸入,則為 true,否則為false。 預設為 false

範例

下列範例說明如何重新導向 StandardInput 進程的數據流。 sort命令是可讀取及排序文字輸入的主控台應用程式。

此範例會 sort 以重新導向的輸入啟動命令。 然後,它會提示使用者輸入文字,並透過重新導向StandardInput的數據流將文字傳遞至sort進程。 結果 sort 會顯示給控制臺上的使用者。

C#
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

namespace ProcessStandardInputSample
{
    class StandardInputTest
    {
        static void Main()
        {
            Console.WriteLine("Ready to sort one or more text lines...");

            // Start the Sort.exe process with redirected input.
            // Use the sort command to sort the input text.
            using (Process myProcess = new Process())
            {
                myProcess.StartInfo.FileName = "Sort.exe";
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.RedirectStandardInput = true;

                myProcess.Start();

                StreamWriter myStreamWriter = myProcess.StandardInput;

                // Prompt the user for input text lines to sort.
                // Write each line to the StandardInput stream of
                // the sort command.
                String inputText;
                int numLines = 0;
                do
                {
                    Console.WriteLine("Enter a line of text (or press the Enter key to stop):");

                    inputText = Console.ReadLine();
                    if (inputText.Length > 0)
                    {
                        numLines++;
                        myStreamWriter.WriteLine(inputText);
                    }
                } while (inputText.Length > 0);

                // Write a report header to the console.
                if (numLines > 0)
                {
                    Console.WriteLine($" {numLines} sorted text line(s) ");
                    Console.WriteLine("------------------------");
                }
                else
                {
                    Console.WriteLine(" No input was sorted");
                }

                // End the input stream to the sort command.
                // When the stream closes, the sort command
                // writes the sorted text lines to the
                // console.
                myStreamWriter.Close();

                // Wait for the sort process to write the sorted text lines.
                myProcess.WaitForExit();
            }
        }
    }
}

備註

Process可以從其標準輸入數據流讀取輸入文字,通常是鍵盤。 藉由重新導向 StandardInput 數據流,您可以透過程序設計方式指定進程的輸入。 例如,您可以提供指定檔案內容的文字,或從另一個應用程式輸出,而不是使用鍵盤輸入。

注意

如果您想要設定為 ,則必須將 設定UseShellExecutefalseRedirectStandardInput為。true 否則,寫入 StandardInput 數據流會擲回例外狀況。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱