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的路径!我想 ...
随机推荐
- Hazelcast
Hazelcast是一个高度可扩展的数据分发和集群平台.特性包括: 提供java.util.{Queue, Set, List, Map}分布式实现. 提供java.util.concurrency. ...
- 数据库sharding(scale up to scale out)
sharding是将一个大数据库按照一定规则拆分成多个小数据库的一门技术. 当我们的应用数据量越来越多,访问量越来越大的时候,我们会作何选择?继续提升数据库服务器的性能还是采用一项技术让数据库平滑扩展 ...
- Asp.net日期字符串格式化显示
我们经常会遇到对时间进行转换,达到不同的显示效果,默认格式为:2006-6-6 14:33:34 如果要换成成200606,06-2006,2006-6-6或更多的格式该怎么办呢?这里将要用到:Dat ...
- HTML表单元素中disabled的元素的值不会提交到服务器
一.在HTMl页面的form表单中对disabled的元素的属性和值不会提交到服务器 实例1: <form action="#"> <input type=&qu ...
- JDBC——架构层、驱动
JDBC(java Datebase Connector) jdbc驱动程序 四种类型: jdbc-odbc桥接驱动程序 Native-API JDBC-Net Native-Protocol (常见 ...
- 配置nginx的负载均衡
1.1 什么是负载均衡 负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性. 负载均衡,英文名称 ...
- HDU 2809 God of War(DP + 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 题目大意:给出战神吕布的初始攻击力ATI.防御力DEF.生命值HP.每升一级增加的攻击力In_A ...
- Python问题记录:如何处理中文网页中的多余空格
在制作Epub电子书的时候,因为有从网络上下载的格式比较混乱的电子书,现在打算自己用Pythonc处理一下. 1.如何删除掉网页(html)中的多余空额.尤其是包含在tag(标签:span.p)当中的 ...
- 九度OJ 1042 Coincidence -- 动态规划(最长公共子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1042 题目描述: Find a longest common subsequence of two strings ...
- DataTable操作(建表,建行,建列,添加数据)
public DataTable GetNewTable() { DataTable dt2 = new DataTable("NewDataSet"); //创建一个新Table ...
注意