从的WinForms Controling CMD.EXE [英] Controling cmd.exe from Winforms

查看:160
本文介绍了从的WinForms Controling CMD.EXE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:我想从的WinForms控制CMD.EXE。

我并不是在一个单一的过程中的每一个命令,用STARTUPINFO,然后停止。

我的意思是,例如启动(我的)SQL或GDB命令提示符,发出命令,接收答案,发送下一个命令,接收下一个答案,停止SQL命令提示符
退出的过程。

基本上我想写的任何控制台应用程序之上的图形用户界面。

我想有从CMD.EXE重定向到一个文本框的输出和输入来自另一个文本框来了(在preSS输入/ OK键)。

Question: I want to control cmd.exe from winforms.

I DO NOT mean every command in a single process, with startupinfo, and then stop.

I mean for example start the (My) SQL or GDB command prompt, send command, receive answer, send next command, receive next answer, stop SQL command prompt
exit process.

Basically I want to write a GUI on top of any console application.

I want to have the output from cmd.exe redirected to a textfield, and the input coming from another textfield (on press enter/OK button).

我没有发现任何样本这一点。 有什么办法?

I don't find any samples for this. Is there a way ?

推荐答案

有在 $ C $的CProject

祝你好运!

- 编辑: 我觉得这是更喜欢它,我创建了一个简单的形式,2文本框和三个按钮。 第一个文本框是命令项,第二(多),显示结果。

- I think this is more like it, I created a simple form, 2 textboxes and three buttons. First textbox is for command entry, the second (multiline), displays the result.

第一个按钮执行该命令时,第二按钮更新的结果(因为结果读异步)

The first button executes the command, the second button updates the result (because results are read async)

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private static StringBuilder cmdOutput = null;
        Process cmdProcess;
        StreamWriter cmdStreamWriter;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmdOutput = new StringBuilder("");
            cmdProcess = new Process();

            cmdProcess.StartInfo.FileName = "cmd.exe";
            cmdProcess.StartInfo.UseShellExecute = false;
            cmdProcess.StartInfo.CreateNoWindow = true;
            cmdProcess.StartInfo.RedirectStandardOutput = true;

            cmdProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
            cmdProcess.StartInfo.RedirectStandardInput = true;
            cmdProcess.Start();

            cmdStreamWriter = cmdProcess.StandardInput;
            cmdProcess.BeginOutputReadLine();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            cmdStreamWriter.WriteLine(textBox2.Text);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            cmdStreamWriter.Close();
            cmdProcess.WaitForExit();
            cmdProcess.Close();
        }

        private void btnShowOutput_Click(object sender, EventArgs e)
        {
            textBox1.Text = cmdOutput.ToString();
        }

        private static void SortOutputHandler(object sendingProcess,
            DataReceivedEventArgs outLine)
        {
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                cmdOutput.Append(Environment.NewLine + outLine.Data);
            }
        }
    }
}

在截图中,你可以看到,我进入了CD \命令更改目录,这个目录(DIR)在执行下一个命令。

In the screenshot you can see that I entered the cd\ command to change directory and the next command executed in this directory (dir).

这篇关于从的WinForms Controling CMD.EXE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆