获取用于写入应用程序输入的流。

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

语法

 
 
 
public StreamWriter StandardInput { get; }
 
/** @property */
public StreamWriter get_StandardInput ()
 
public function get StandardInput () : StreamWriter

属性值

StreamWriter,可用于写入应用程序的标准输入流。

异常

 
 
异常类型 条件

InvalidOperationException

由于 ProcessStartInfo.RedirectStandardInput 设置为 false,因此尚未定义 StandardInput 流。

备注

 
 

Process 可以读取来自它的标准输入流(一般是键盘)的输入文本。通过重定向 StandardInput 流,可以采用编程方式指定输入。例如,可以不使用键盘输入,而从指定文件的内容或另一个应用程序的输出提供文本。

注意

若要使用 StandardInput,您必须将 ProcessStartInfo.UseShellExecute 设置为 false,并且将 ProcessStartInfo.RedirectStandardInput设置为 true。否则,写入 StandardInput 流时将引发异常。

示例

 
 

下面的示例阐释了如何重定向进程的 StandardInput 流。该示例通过重定向的输入启动 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.StandardInput属性的更多相关文章

  1. android.app.Service-android:process=":remote"属性解说

    在学习Android Service组件的过程中碰到了一个问题,就是在Android应用的声明文件Manifest.xml中有时候会对相关的服务标签设置一个android:process=”:remo ...

  2. [C#]使用Process的StandardInput与StandardOutput写入读取控制台数据

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以 ...

  3. Process.RedirectStandardInput

    获取或设置一个值,该值指示应用程序的输入是否从 Process.StandardInput 流中读取. 命名空间:System.Diagnostics程序集:System(在 system.dll 中 ...

  4. C#启动进程之Process

    在程序设计中,我们经常会遇到要从当前的程序跳到另一个程序的设计需求.也就是当前进程创建另一个进程.C#提供了Process使得我们很方便的实现. 1.Process基本属性和方法 Id //进程的Id ...

  5. 关于.Net中Process的使用方法和各种用途汇总(二):用Process启动cmd.exe完成将cs编译成dll

    上一章博客我为大家介绍了Process类的所有基本使用方法,这一章博客我来为大家做一个小扩展,来熟悉一下Process类的实际使用,废话不多说我们开始演示. 先看看我们的软件要设计成的布局吧. 首先我 ...

  6. nodejs之process进程

    虽然node对操作系统做了很多抽象的工作,但是你还是可以直接和他交互,比如和系统中已经存在的进程进行交互,创建工作子进程.node是一个用于事件循环的线程,但是你可以在这个事件循环之外创建其他的进程( ...

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

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

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

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

  9. apk,task,android:process与android:sharedUserId的区别

    apk一般占一个dalvik,一个进程,一个task.通过设置也可以多个进程,占多个task. task是一个activity的栈,其中"可能"含有来自多个App的activity ...

随机推荐

  1. php注册登录时生成的验证码

    http://blog.sina.com.cn/s/blog_8173443e01012l82.html 记得我学php时第一件事就是研究登陆注册.当然,登陆少不了验证码.两年过去了,昨天突然想用个验 ...

  2. kettle中通过 时间戳(timestamp)方式 来实现数据库的增量同步操作(一)

    这个实验主要思想是在创建数据库表的时候, 通过增加一个额外的字段,也就是时间戳字段, 例如在同步表 tt1 和表 tt2 的时候, 通过检查那个表是最新更新的,那个表就作为新表,而另外的表最为旧表被新 ...

  3. #ifndef#define#endif的用法

    在网上看到了感觉作者总结得很好,作者辛苦了! #ifndef#define#endif的用法 文件中的#ifndef 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都 ...

  4. mysql相关重要问题解决

    root密码修改 MySQL 的管理员密码: sudo mysqladmin -u root password newpassword: mysql无法安装:删除/etc/mysql,   /var/ ...

  5. IOS动态修改按钮响应时间

    在项目开发中我们可能会遇到这样子的情况,比如在我们登陆的时候需要把数据发送给服务器进行比对,通常我们的做法是当用户点击按钮后,使用一个加载效果的view遮挡住当前界面,直到服务器返回数据或者超时.如果 ...

  6. 关于MDCSwipeToChooseView的应用

    本人因为项目中某个页面的功能需要,用到了MDCSwipeToChooseView,就在网上查阅了相关的资料,资源有很多,但应该都是同一个人上传的,code4还有git上都有,但下载demo下来后运行不 ...

  7. iOS控件——UIView与UIImageView播放动画的实现方法

    1.UIView //初始状态 [UIView animateWithDuration:(int) animations:^{ //最终状态 }completion:^(BOOL finished){ ...

  8. [C#][转][string 字符串截取

    C#几个经常用到的字符串截取 一. 1.取字符串的前i个字符 (1)string str1=str.Substring(0,i); (2)string str1=str.Remove(i,str.Le ...

  9. 暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  10. ubuntu 安装flash插件

    参考文献: http://wiki.debian.org.hk/w/Install_Flash_Player_with_APTapt-get install adobe-flashplugin