C#隐式运行CMD命令(隐藏命令窗口)

所属分类: 软件编程 / C#教程 阅读数: 68
收藏 0 赞 0 分享

本文实现了C#隐式运行CMD命令的功能。下图是实例程序的主画面。在命令文本框输入DOS命令,点击“Run”按钮,在下面的文本框中输出运行结果。

下面是程序的完整代码。本程序没有使用p.StandardOutput.ReadtoEnd()和p.StandardOutput.ReadLine()方法来获得输出,因为这些方法执行后画面容易卡死。而是通过调用异步方法BeginOutputReadLine来获取输出,并在事件p.OutputDataReceived的事件处理方法中来处理结果。

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

 
namespace RunDosCommandForm
{
  publicpartialclassForm1 : Form
  {
    publicForm1()
    {
      InitializeComponent();
    }
 
    privatevoidbutton1_Click(object sender, EventArgse)
    {
      ExcuteDosCommand(textBox1.Text);
    }
 
    privatevoidExcuteDosCommand(string cmd)
    {
      try
      {
        Process p = newProcess();
        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 += newDataReceivedEventHandler(sortProcess_OutputDataReceived);
        p.Start();
        StreamWriter cmdWriter = p.StandardInput;
        p.BeginOutputReadLine();
        if (!String.IsNullOrEmpty(cmd))
        {
          cmdWriter.WriteLine(cmd);
        }
        cmdWriter.Close();
        p.WaitForExit();
        p.Close(); 
      }
      catch(Exception ex)
      {
        MessageBox.Show("执行命令失败,请检查输入的命令是否正确!");
      }
    }
 
    privatevoidsortProcess_OutputDataReceived(object sender,DataReceivedEventArgs e)
    {
      if(!String.IsNullOrEmpty(e.Data))
      {
        this.BeginInvoke(newAction(() => { this.listBox1.Items.Add(e.Data);}));         
      }
    }
  }
}

我们还可以将需要运行的CMD命令保存为BAT文件,再使用Process类来执行。

Process p = new Process();//设定调用的程序名,不是系统目录的需要完整路径 
p.StartInfo.FileName = "cmd.bat";//传入执行参数 
p.StartInfo.Arguments = "";
p.StartInfo.UseShellExecute = false;//是否重定向标准输入 
p.StartInfo.RedirectStandardInput = false;//是否重定向标准转出 
p.StartInfo.RedirectStandardOutput = false;//是否重定向错误 
p.StartInfo.RedirectStandardError = false;//执行时是不是显示窗口 
p.StartInfo.CreateNoWindow = true;//启动 
p.Start();
p.WaitForExit();
p.Close(); 

更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多