<i id='1Dmgt'><tr id='1Dmgt'><dt id='1Dmgt'><q id='1Dmgt'><span id='1Dmgt'><b id='1Dmgt'><form id='1Dmgt'><ins id='1Dmgt'></ins><ul id='1Dmgt'></ul><sub id='1Dmgt'></sub></form><legend id='1Dmgt'></legend><bdo id='1Dmgt'><pre id='1Dmgt'><center id='1Dmgt'></center></pre></bdo></b><th id='1Dmgt'></th></span></q></dt></tr></i><div id='1Dmgt'><tfoot id='1Dmgt'></tfoot><dl id='1Dmgt'><fieldset id='1Dmgt'></fieldset></dl></div>
    1. <tfoot id='1Dmgt'></tfoot>
    2. <legend id='1Dmgt'><style id='1Dmgt'><dir id='1Dmgt'><q id='1Dmgt'></q></dir></style></legend>

      <small id='1Dmgt'></small><noframes id='1Dmgt'>

        <bdo id='1Dmgt'></bdo><ul id='1Dmgt'></ul>

        从 Winforms 控制 cmd.exe

        时间:2023-08-27
            <bdo id='km3T7'></bdo><ul id='km3T7'></ul>

                <tbody id='km3T7'></tbody>
              <tfoot id='km3T7'></tfoot>
              1. <legend id='km3T7'><style id='km3T7'><dir id='km3T7'><q id='km3T7'></q></dir></style></legend>
              2. <i id='km3T7'><tr id='km3T7'><dt id='km3T7'><q id='km3T7'><span id='km3T7'><b id='km3T7'><form id='km3T7'><ins id='km3T7'></ins><ul id='km3T7'></ul><sub id='km3T7'></sub></form><legend id='km3T7'></legend><bdo id='km3T7'><pre id='km3T7'><center id='km3T7'></center></pre></bdo></b><th id='km3T7'></th></span></q></dt></tr></i><div id='km3T7'><tfoot id='km3T7'></tfoot><dl id='km3T7'><fieldset id='km3T7'></fieldset></dl></div>

                <small id='km3T7'></small><noframes id='km3T7'>

                  本文介绍了从 Winforms 控制 cmd.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  问题:我想从winforms控制cmd.exe.

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

                  我并不是指单个进程中的每个命令,带有启动信息,然后停止.

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

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

                  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.

                  基本上我想在任何控制台应用程序之上编写一个 GUI.

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

                  我希望将 cmd.exe 的输出重定向到一个文本字段,而输入来自另一个文本字段(按下 enter/OK 按钮).

                  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?

                  推荐答案

                  代码项目

                  祝你好运!

                  -我认为这更像是它,我创建了一个简单的表单,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 控制 cmd.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从单元测试中获取发送到 Console.Out 的输出? 下一篇:控制台应用程序在鼠标单击时冻结

                  相关文章

                  最新文章

                • <tfoot id='YLOoS'></tfoot>

                    • <bdo id='YLOoS'></bdo><ul id='YLOoS'></ul>
                    <legend id='YLOoS'><style id='YLOoS'><dir id='YLOoS'><q id='YLOoS'></q></dir></style></legend>

                    <small id='YLOoS'></small><noframes id='YLOoS'>

                    1. <i id='YLOoS'><tr id='YLOoS'><dt id='YLOoS'><q id='YLOoS'><span id='YLOoS'><b id='YLOoS'><form id='YLOoS'><ins id='YLOoS'></ins><ul id='YLOoS'></ul><sub id='YLOoS'></sub></form><legend id='YLOoS'></legend><bdo id='YLOoS'><pre id='YLOoS'><center id='YLOoS'></center></pre></bdo></b><th id='YLOoS'></th></span></q></dt></tr></i><div id='YLOoS'><tfoot id='YLOoS'></tfoot><dl id='YLOoS'><fieldset id='YLOoS'></fieldset></dl></div>