欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

.NET程序页面中,操作并输入cmd命令的小例子

时间:2021-05-14 10:36:46|栏目:.NET代码|点击:

WinFormsApp_OperateAndInputCMD:

新建Form1,拖入TextBox,并设为允许多行,Dock设为Fill,然后绑定KeyUp事件即可

执行代码如下:

复制代码 代码如下:

private void txtCmdInput_KeyUp(object sender, KeyEventArgs e)

      {

          if (e.KeyCode == Keys.Enter)

          {

              int count = txtCmdInput.Lines.Length;

              if (count == 0) return;

              while (count > 0 && (string.IsNullOrEmpty(txtCmdInput.Lines[count - 1])))

              {

                  count--;

              }

              if (count > 0)// && !string.IsNullOrEmpty(txtCmdInput.Lines[count - 1]))

                  ExecuteCmd(txtCmdInput.Lines[count - 1]);

          }

      }

      public void ExecuteCmd(string cmd)

      {

          System.Diagnostics.Process p = new System.Diagnostics.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.AutoFlush = true;           //写入命令     

          p.StandardInput.WriteLine(cmd);

          p.StandardInput.WriteLine("exit");          //等待结束  

          txtCmdInput.AppendText(p.StandardOutput.ReadToEnd());

          p.WaitForExit();

          p.Close();

      }


执行效果图:

上一篇:WPF实现图片合成或加水印的方法【2种方法】

栏    目:.NET代码

下一篇:详解在.net中读写config文件的各种方法

本文标题:.NET程序页面中,操作并输入cmd命令的小例子

本文地址:http://www.codeinn.net/misctech/121079.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有