擷取或設定一個值,該值訓示應用程式的輸入是否從 Process.StandardInput 流中讀取。
命名空間:System.Diagnostics
程式集:System(在 system.dll 中)
文法
C#
C++
VB
public bool RedirectStandardInput { get; set; }
J#
/** @property */
public boolean get_RedirectStandardInput ()
/** @property */
public void set_RedirectStandardInput (boolean value)
JScript
public function get RedirectStandardInput () : boolean
public function set RedirectStandardInput (value : boolean)
屬性值
若要從 Process.StandardInput 中讀取輸入,則為 true;否則為 false。
備注
Process 可以讀取來自它的标準輸入流(一般是鍵盤)的輸入文本。通過重定向 StandardInput 流,可以通過程式設計方式指定程序的輸入。例如,可以不使用鍵盤輸入,而從指定檔案的内容或另一個應用程式的輸出提供文本。
注意 |
如果要将 RedirectStandardInput 設定為 true,必須先将 UseShellExecute 設定為 false。否則,寫入 StandardInput 流時将引發異常。 |
示例
下面的示例闡釋了如何重定向程序的 StandardInput 流。sort 指令是讀取文字輸入并對其進行排序的一種控制台應用程式。
該示例通過重定向的輸入啟動 sort 指令。然後它提示使用者輸入文本,并通過重定向的 StandardInput 流,将使用者輸入的文本傳遞給 sort 程序。sort 結果會在控制台上顯示給使用者。
using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
namespace Process_StandardInput_Sample
{
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.
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(" {0} sorted text line(s) ", numLines);
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();
myProcess.Close();
}
}
}
龍騰一族至尊龍騎