国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

.NET程序頁(yè)面中,操作并輸入cmd命令的小例子

2020-01-24 03:34:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

WinFormsApp_OperateAndInputCMD:

新建Form1,拖入TextBox,并設(shè)為允許多行,Dock設(shè)為Fill,然后綁定KeyUp事件即可

執(zhí)行代碼如下:

復(fù)制代碼 代碼如下:

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();                                  //設(shè)置自動(dòng)刷新緩沖并更新   

          p.StandardInput.AutoFlush = true;           //寫入命令     

          p.StandardInput.WriteLine(cmd);

          p.StandardInput.WriteLine("exit");          //等待結(jié)束  

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

          p.WaitForExit();

          p.Close();

      }


執(zhí)行效果圖:

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 屏东市| 界首市| 建德市| 内乡县| 理塘县| 黔东| 神农架林区| 英吉沙县| 鄂温| 南京市| 当阳市| 玉林市| 新建县| 新乐市| 桐庐县| 读书| 平武县| 丰原市| 额敏县| 昌平区| 嘉义市| 名山县| 炉霍县| 沐川县| 获嘉县| 石棉县| 灯塔市| 焦作市| 青阳县| 托里县| 桦川县| 永城市| 大连市| 通州区| 萨迦县| 柘城县| 长寿区| 常熟市| 邳州市| 鹿泉市| 皋兰县|