获取或设置一个值,该值指示应用程序的输入是否从 Process.StandardInput 流中读取。

命名空间:System.Diagnostics
程序集:System(在 system.dll 中)

语法

 
 
 
public bool RedirectStandardInput { get; set; }
 
/** @property */
public boolean get_RedirectStandardInput () /** @property */
public void set_RedirectStandardInput (boolean value)
 
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(); }
}
}

Process.RedirectStandardInput的更多相关文章

  1. 使用Process类重定向输出与错误时遇到的问题 (转)

    程序中要调用外部程序cmd.exe执行一些命令行,并取得屏幕输出,使用了Process类,基本代码如下: Process process = new Process(); process.StartI ...

  2. C# Process运行cmd命令的异步回显

    以下的代码为new Process() 调用cmd命令,并将结果异步回显到Form的例子: 以下的代码为new Process() 调用cmd命令,并将结果异步回显到Form的例子: [csharp] ...

  3. Process启动.exe,当.exe内部抛出异常时,总会弹出一个错误提示框,阻止Process进入结束

    public class TaskProcess { [DllImport("kernel32.dll", SetLastError = true)] public static ...

  4. C#的Process类的一些用法

    c#之process类相关整理 一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using Sys ...

  5. c# process 输入输出

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. Process.StandardInput属性

    获取用于写入应用程序输入的流. 命名空间:System.Diagnostics程序集:System(在 system.dll 中) 语法     C# C++ VB   public StreamWr ...

  7. c#之process类相关整理

    一.根据进程名获取进程的用户名?   需要添加对 System.Management.dll 的引用   using System.Diagnostics; using System.Manageme ...

  8. C# process 使用方法

    public static string ExecuteAaptCommand(string appName, string command) { string result = string.Emp ...

  9. C#Process执行批处理后如何获取返回值?

    代码如下   p.StartInfo = new System.Diagnostics.ProcessStartInfo(path, pwd); p.Start();其中path是个BAT的路径!我想 ...

随机推荐

  1. weewwe

    http://blog.csdn.net/u013073524/article/details/25912891

  2. java新手笔记18 类比较

    1.Shap类 package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 // ...

  3. == 和 equals()的区别

    package com.liaojianya.chapter1; /** * This program demonstrates the difference between == and equal ...

  4. sublime_2014-11-19

    http://xionggang163.blog.163.com/blog/static/376538322013930104310297/ 直接输入注册码就可以了 ----- BEGIN LICEN ...

  5. (四)跟我一起玩Linux网络服务:DHCP服务配置之中继代理

    继第三部分的DHCP服务器的设置成功,我们来做一个中继代理服务器的配置吧. 我们的虚拟机结构如图: 具体参考: (一)跟我一起玩Linux网络服务:DNS服务——BIND(/etc/named.con ...

  6. JQuery 分页实现

     JQuery分页实现  css: .liList0 { padding-left:5px;}.liList0 li { width:160px; float:left; display:inline ...

  7. JDK重要包和Java学习方法论

    以下内容摘自:万能的林萧说:一篇文章教会你,如何做到简历中要求的“要有扎实的Java基础”    第一级别:精读源码 该级别包含的包如下: java.io java.lang java.util 第二 ...

  8. IE6背景图片闪动问题

    在IE6中,当JS触发事件时或者hover的时候,如果网速过慢 IE6背景图片重新加载会闪一下. 好的一个解决方案是 <!--[if IE 6]><script> try{do ...

  9. 在Linux下不使用密码远程登陆其他Linux

    有时需要再一台Linux上登陆其他Linux服务器,通常可以直接使用SSH命令,加入两台服务器一台服务器A,IP地址192.168.1.2,另一台服务器B,IP地址192.168.1.3,如果想从A服 ...

  10. TDirectory.IsRelativePath是否相对路径

    使用函数: System.IOUtils.TDirectory.IsRelativePath class function IsRelativePath(const Path: string): Bo ...