Process.RedirectStandardInput
获取或设置一个值,该值指示应用程序的输入是否从 Process.StandardInput 流中读取。
命名空间:System.Diagnostics
程序集:System(在 system.dll 中)
/** @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的更多相关文章
- 使用Process类重定向输出与错误时遇到的问题 (转)
程序中要调用外部程序cmd.exe执行一些命令行,并取得屏幕输出,使用了Process类,基本代码如下: Process process = new Process(); process.StartI ...
- C# Process运行cmd命令的异步回显
以下的代码为new Process() 调用cmd命令,并将结果异步回显到Form的例子: 以下的代码为new Process() 调用cmd命令,并将结果异步回显到Form的例子: [csharp] ...
- Process启动.exe,当.exe内部抛出异常时,总会弹出一个错误提示框,阻止Process进入结束
public class TaskProcess { [DllImport("kernel32.dll", SetLastError = true)] public static ...
- C#的Process类的一些用法
c#之process类相关整理 一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using Sys ...
- c# process 输入输出
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Process.StandardInput属性
获取用于写入应用程序输入的流. 命名空间:System.Diagnostics程序集:System(在 system.dll 中) 语法 C# C++ VB public StreamWr ...
- c#之process类相关整理
一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using System.Manageme ...
- C# process 使用方法
public static string ExecuteAaptCommand(string appName, string command) { string result = string.Emp ...
- C#Process执行批处理后如何获取返回值?
代码如下 p.StartInfo = new System.Diagnostics.ProcessStartInfo(path, pwd); p.Start();其中path是个BAT的路径!我想 ...
随机推荐
- groupBy
public List groupBy(List list,String flag,String... sortName) throws Exception{ Map<String,List&l ...
- ios 阻止GDB依附
GDB,IDE是大多数hackers的首选,阻止GDB依附到应用的常规办法是: . #import <sys/ptrace.h> . . int main(int argc, charch ...
- 经历:easyui的datagrid没有数据滚动条的显示
今天,一个用户提出一个这样的问题,"查询不到结果时,为什么我看不到后面的标题呢?" 最初,我听到这个问题时,第一反应是:查出来数据不就有滚动条了吗,干嘛非要较真呢? 不过,后来想想 ...
- O-C相关06:self和super关键字介绍——self关键字
self关键字介绍 1.self和super OC 版权声明:本文为博主原创文章,未经博主允许不得转载. posted @ 2015-08-04 12:46 王刚韧(wanghy_iOS) 阅读(.. ...
- ios专题 -block用法
what is block Blocks are a language-level feature added to C, Objective-C and C++, which allow you t ...
- MongoDB源码分析——mongod数据查询操作
源码版本为MongoDB 2.6分支 Edit mongod数据查询操作 在mongod的初始化过程中说过,服务端接收到客户端消息后调用MyMessageHandler::process函数处理消息. ...
- bootstrap实现手风琴功能(树形列表)
首先把架包拷进项目,然后在页面中引进css,js <script src="js/jquery/jquery-2.1.1.min.js"></script> ...
- (转)IOS 学习笔记 2015-03-23 如何获取IOS程序的系统信息
首页 > 程序开发 > 移动开发 > IOS > 正文 iOS 获取手机的型号,系统版本,软件名称,软件版本 -- 个评论 作者:vipa1888 收藏 我要投稿 网上搜索出来 ...
- java对象初始化顺序的简单验证
以下这段小程序对调用对象构造函数时,父类构造函数.成员变量初始化函数,以及非静态初始化块调用顺序进行验证,不考虑静态成员及静态初始化块. public class Derive extends Bas ...
- 为什么aspx这么“慢”
首先你要明白什么viewstate:由系统生成的一个隐藏域,用来进行页面状态保持的 里面存放着关于判断页面是否提交的Ispostback,和一些关于服务器控件的状态和数据: (说明下 ,ViewSta ...
注意