在C#编程中,有时我们需要在应用程序中执行命令行(CMD)命令,但不希望显示命令提示符窗口。本文将详细介绍如何使用C#隐式运行CMD命令并隐藏命令窗口,特别是在WinForm应用中。 要实现这个功能,我们需要使用`System.Diagnostics.Process`类。这个类提供了启动、控制和与外部进程交互的能力。以下是一个简单的C# WinForm应用示例,它创建一个文本框让用户输入DOS命令,然后点击按钮执行命令并显示结果: ```csharp using System; using System.Diagnostics; using System.IO; using System.Windows.Forms; namespace RunDosCommandForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ExcuteDosCommand(textBox1.Text); } private void ExcuteDosCommand(string cmd) { try { Process p = new Process(); p.StartInfo.FileName = "cmd"; p.StartInfo.UseShellExecute = false; // 关闭外壳执行,防止弹出命令窗口 p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; // 重定向错误输出 p.StartInfo.CreateNoWindow = true; // 隐藏命令窗口 p.OutputDataReceived += new DataReceivedEventHandler(sortProcess_OutputDataReceived); p.Start(); // 写入命令到命令行 StreamWriter cmdWriter = p.StandardInput; if (!String.IsNullOrEmpty(cmd)) { cmdWriter.WriteLine(cmd); } cmdWriter.Close(); // 开始监听输出数据 p.BeginOutputReadLine(); p.WaitForExit(); // 等待命令执行完成 p.Close(); } catch (Exception ex) { MessageBox.Show("执行命令失败,请检查输入的命令是否正确!"); } } // 事件处理程序,处理命令输出 private void sortProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (!String.IsNullOrEmpty(e.Data)) { this.BeginInvoke(new Action(() => { this.listBox1.Items.Add(e.Data); })); } } } } ``` 在这个示例中,`ExcuteDosCommand`方法是执行CMD命令的核心。它创建了一个`Process`对象,设置其属性以启动CMD进程,关闭命令窗口,重定向输入、输出和错误流。`StartInfo.FileName`设为"cmd",表示我们要启动CMD。`UseShellExecute`设为`false`,`RedirectStandardInput`和`RedirectStandardOutput`设为`true`,允许我们向命令行写入命令并读取输出。 `sortProcess_OutputDataReceived`方法是事件处理程序,用于接收命令的输出数据并将其添加到WinForm中的列表框(listBox1)中。使用`BeginOutputReadLine()`方法异步监听输出数据,避免阻塞UI线程。 此外,如果需要执行的命令已经保存为批处理文件(.bat),我们可以直接指定批处理文件的路径作为`FileName`,而不需要通过CMD来调用。例如: ```csharp Process p = new Process(); p.StartInfo.FileName = "cmd.bat"; p.StartInfo.UseShellExecute = false; // ...其他设置 p.Start(); ``` 通过使用`Process`类,我们可以方便地在C#应用中隐式运行CMD命令并捕获输出,无需显示命令窗口,从而提供更流畅的用户体验。在实际开发中,这种技术常用于执行系统管理任务、自动化过程或者集成其他命令行工具。
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/release/download_crawler_static/12805091/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 12
- 资源: 935
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)