因原程序是用cmd執行的,先打算使用WinForm界面來運行該程序(cmd還是在後台隱藏運行)
現有如下問題:
1.如何在程序運行期間一直隱藏開啟cmd?
2.如何能夠間端的發命令給cmd。並讀取cmd的顯示結果?
3.如何通過WinForm發送回車鍵給cmd?謝謝!

解决方案 »

  1.   

    调用cmd多数可以使用Process.Start( "cmd.exe", args );执行你提到想一直运行一个cmd,cmd的运行是另外一个进程,进程与进程之间要交互的话会比较麻烦。你的需要应该往其他方向考虑,请详细描述下你要做什么
      

  2.   

    说明:经常有朋友问如何在C#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在Java中实现过,由于在C#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。程序运行情况:
      

  3.   

    private void ExecuteCmd(string command)
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";            p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.CreateNoWindow = true;            p.Start();
                p.StandardInput.WriteLine(command);            p.StandardInput.WriteLine("exit");
                p.WaitForExit();
                this.textBox1.Text=textBox1.Text+ p.StandardOutput.ReadToEnd();
                p.Close();
            }這是我找到的代碼,可是好像只能運行一條命令。
    我的需求是要隔一段時間運行一條命令,並根據cmd返回的成功信息後,再執行第2個命令。
      

  4.   

    详细描述地址:C#中运行命令行截取输出流的例子部分关键代码:private void btnExecute_Click(object sender, EventArgs e)
            {
                tbResult.Text = "";
                ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
                //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
                start.Arguments = txtCommand.Text;//设置命令参数
                start.CreateNoWindow = true;//不显示dos命令行窗口
                start.RedirectStandardOutput = true;//
                start.RedirectStandardInput = true;//
                start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
                Process p=Process.Start(start);
                StreamReader reader = p.StandardOutput;//截取输出流
                string line = reader.ReadLine();//每次读取一行
                while (!reader.EndOfStream)
                {
                    tbResult.AppendText(line+" ");
                    line = reader.ReadLine();
                }
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();//关闭进程
                reader.Close();//关闭流
            }
      

  5.   

    /*
     * Created by SharpDevelop.
     * User: Administrator
     * Date: 2008-5-10
     * Time: 12:30
     * 
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;namespace WindowsApplication8
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
    public MainForm()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    }

    delegate void dReadLine(string strLine);
            private void executeCommand(string strFile, string args, dReadLine onReadLine)
               {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = new System.Diagnostics.ProcessStartInfo();
                p.StartInfo.FileName = strFile;
                p.StartInfo.Arguments = args;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                System.IO.StreamReader reader = p.StandardOutput;//截取输出流
                string line = reader.ReadLine();//每次读取一行
                while (!reader.EndOfStream)
                   {
                    onReadLine(line);
                    line = reader.ReadLine();
                }
                p.WaitForExit();
            }
            
            private void PrintMessage(string strLine)
            {
                this.textBox1.Text += "\r\n";
                this.textBox1.Text +=  strLine ;
            }
            
    void Button1Click(object sender, EventArgs e)
    {
    executeCommand("ipconfig", "/all", new dReadLine(PrintMessage));
    }

    void MainFormLoad(object sender, EventArgs e)
    {
    this.textBox1.Multiline = true;
    this.textBox1.WordWrap = false;
    }
    }
    }
      

  6.   

    如果是打命令行,接收返回形式,用shell
      

  7.   

    1
    ProcessStartInfo.WindowStyle 属性 
    http://msdn.microsoft.com/zh-cn/vstudio/system.diagnostics.processstartinfo.windowstyle.aspx2,3
    ProcessStartInfo.RedirectStandardInput 属性 
    ProcessStartInfo.RedirectStandardOutput 属性 
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  8.   

    1.如何在程序運行期間一直隱藏開啟cmd? 
    CreateProcess有参数可以设置,查MSDN就知道了2.如何能夠間端的發命令給cmd。並讀取cmd的顯示結果? 
    3.如何通過WinForm發送回車鍵給cmd? 
    使用管道,详情查MSDN
      

  9.   

    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = true;p.Start();
    p.StandardInput.WriteLine("ping 192.168.1.1");
    p.StandardInput.WriteLine("exit");
    string strRst = p.StandardOutput.ReadToEnd(); //读取返回的信息
      

  10.   


    其實我的cmd是調用另外一個軟件。該軟件運行後返回的結果也是顯示在cmd界面
      

  11.   

    1.Process.StartInfo.CreateNoWindow = true;
    2,3.需要用API, GetStdHandle,ReadConsoleOutput,WriteConsoleInput,GetConsoleScreenBufferInfo等来直接处理控制台缓冲区
      

  12.   


    謝謝周公!你的方法很好用。但相對我的程序有一點還需要改進,請幫忙看看怎麼改!我在發送一個命令給cmd後,它會處於等待狀態,此時我們需要按回車鍵才能繼續執行。
    現在的問題有2個
    1.在處於等待狀態時,WinForm界面就不能動,處於假死狀態,這應該如何處理?
    2.如何發送回車鍵確認?
    謝謝!
      

  13.   

    这个发布一个月了没有人解决
    大家看看
    如何用窗体的Load执行如ngen C:\mRelease\myinfoapp.exe的Vs2008的Dos命令呢?注意不是cmd.exe的
    否则会出现ngen不是内部命令 !!!
      

  14.   

    謝謝周公!你的方法很好用。但相對我的程序有一點還需要改進,請幫忙看看怎麼改!我在發送一個命令給cmd後,它會處於等待狀態,此時我們需要按回車鍵才能繼續執行。
    現在的問題有2個
    1.在處於等待狀態時,WinForm界面就不能動,處於假死狀態,這應該如何處理?
    2.如何發送回車鍵確認?
    謝謝!
      

  15.   

    again
    使用windows API, GetStdHandle,ReadConsoleOutput,WriteConsoleInput,GetConsoleScreenBufferInfo等来直接处理控制台缓冲区,不会出现所谓的卡死问题
      

  16.   

    如何用winAPI函数屏蔽系统热键Ctrl + Alt + Delete,还有个shift(除了用注册表外)
      

  17.   

    如何用钩子函数蔽系统热键Ctrl + Alt + Delete,还有个shift(除了用注册表外)
      

  18.   

    难道没有人试过用钩子函数屏蔽系统热键Ctrl + Alt + Delete,还有个shift(除了用注册表外)???
      

  19.   

     ; hen bu cuo de dongxi  .
      

  20.   

    针对上面PING的命令 如何后面跟一个IP 或 WEB地址是可以实现 但是如果我后面再加入了参数 比如 ping 162.135.10.120 -t 这样的话又该如何处理呢?因为执行这个命令后是一只有返回信息了 那该怎么判断它结束 并把信息反馈回WINFORM中呢?