1101 VS.NET PROCESS 输入 输出 模拟 CMD 很久以前的老文章

vs.net c# 一个调用外部程序的例子 http://www.yippeesoft.com/blog/p/vsnetStartInfoProcess.php Process.StandardOutput 属性 http://www.yippeesoft.com/blog/p/ProcessStandardOutput.php

今天看到有人回复 www.yippeesoft.com 有关程序中调用执行控制台应用程序并交互疑问!和楼主的文章最后一个问题相似。输入一个命令,获得输出,判断该输出内容并输入相应命令,再获得输出值…重复以上动作。 www.yippeesoft.com

试验了一下 要使用 StandardInput,必须已为 StartInfo 属性的 RedirectStandardInput 属性指定了 true。否则,读取 StandardInput 属性将引发异常。
注意   如果要将 StandardInput 设置为 true,则 StartInfo 属性上的 UseShellExecute 必须为 false。
www.yippeesoft.com

ProcessStartInfo.RedirectStandardInput 属性请参见
获取或设置一个值,该值指示是否从 Process 实例的 StandardInput 成员读取进程命令输入,从而使您能够从标准输入流(通常为键盘)之外的源进行读取。例如,用于从文件读取数据。

private void DisplayOutput() www.yippeesoft.com
&leftsign;
 while ( proc != null && !proc.HasExited )
  &leftsign;
    string strLine = null;
    while (  ( strLine = proc.StandardOutput.ReadLine() ) != null)
    &leftsign;
   Trace.WriteLine(strLine);
     m_txtOutput.AppendText( strLine + "\\r\\n" );
    &rightsign; www.yippeesoft.com

  &rightsign;
&rightsign; www.yippeesoft.com

  Thread m_threadOutput; www.yippeesoft.com
  Process proc; www.yippeesoft.com
  
  private void menuItem1_Click(object sender, System.EventArgs e)
  &leftsign;
   proc = new Process(); www.yippeesoft.com
   proc .StartInfo.FileName = "cmd";
   proc .StartInfo.WorkingDirectory = "c:\\\\";
   proc .StartInfo.CreateNoWindow = true;
   proc .StartInfo.UseShellExecute = false;
   proc .StartInfo.RedirectStandardOutput = true;
   proc .StartInfo.RedirectStandardInput = true;
   proc .Start();  www.yippeesoft.com
   [hide]m_threadOutput = new Thread( new ThreadStart( DisplayOutput ) );
   m_threadOutput.Start(); [/hide]
  &rightsign; www.yippeesoft.com

private void menuItem3_Click(object sender, System.EventArgs e)
  &leftsign; www.yippeesoft.com
   proc.StandardInput.WriteLine("dir");
  &rightsign;

如何在图形界面中实时捕获控制台程序的标准输出
http://www.contextfree.net/wangyg/b/tech/myide.html

ShellControl – A console emulation control
http://www.codeproject.com/cs/miscctrl/shellcontrol.asp

1.判断命令是否执行结束并获得这个命令执行后返回的结果?(连续执行多个命令)
DOTNET程序获得输出不全.
当直接执行控制台程序,在控制台中输入命令,执行完成后为出现 "XXX>"提示符等待继续输入.
由于程序采用多线程执行,本想用 "XXX>"提示符号, 来判断命令是否执行结束.
如果程序执行后,获得该命令执行得到的返内容,在执行下一命令…
但DOTNET程序却无法获得这一符号.而java写的程序却都能得到.

历史博文

标签:, , ,
十一月 10, 2005 at 6:28 上午 by yippee 1,046 次
Category: Dev
Tags: , , ,