软件编程
位置:首页>> 软件编程>> C#编程>> C#程序调用cmd.exe执行命令

C#程序调用cmd.exe执行命令

作者:.NET开发菜鸟  发布时间:2022-11-14 04:04:50 

标签:C#,调用,cmd,执行,命令

在windows环境下,命令行程序为cmd.exe,是一个32位的命令行程序,微软Windows系统基于Windows上的命令解释程序,类似于微软的DOS操作系统。输入一些命令,cmd.exe可以执行,比如输入shutdown -s就会在30秒后关机。总之,它非常有用。打开方法:开始-所有程序-附件 或 开始-寻找-输入:cmd/cmd.exe 回车。它也可以执行BAT文件。

下面介绍使用C#程序调用cmd执行命令:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace CmdDemo
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("请输入要执行的命令:");
           string strInput = Console.ReadLine();
           Process p = new Process();
           //设置要启动的应用程序
           p.StartInfo.FileName = "cmd.exe";
           //是否使用操作系统shell启动
           p.StartInfo.UseShellExecute = false;
           // 接受来自调用程序的输入信息
           p.StartInfo.RedirectStandardInput = true;
           //输出信息
           p.StartInfo.RedirectStandardOutput = true;
           // 输出错误
           p.StartInfo.RedirectStandardError = true;
           //不显示程序窗口
           p.StartInfo.CreateNoWindow = true;
           //启动程序
           p.Start();

//向cmd窗口发送输入信息
           p.StandardInput.WriteLine(strInput+"&exit");

p.StandardInput.AutoFlush=true;

//获取输出信息
           string strOuput = p.StandardOutput.ReadToEnd();
           //等待程序执行完退出进程
           p.WaitForExit();
           p.Close();

Console.WriteLine(strOuput);

Console.ReadKey();
       }
   }
}

运行效果:

C#程序调用cmd.exe执行命令

应用:使用C#程序调用cmd命令生成WCF服务的客户端调用文件

设计界面:

C#程序调用cmd.exe执行命令

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace ExecuteCMD
{
   public partial class FrmMain : Form
   {
       public FrmMain()
       {
           InitializeComponent();
       }

private void btn_Create_Click(object sender, EventArgs e)
       {
           try
           {
               //创建一个进程
               Process p = new Process();
               p.StartInfo.FileName = "cmd.exe";
               p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
               p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
               p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
               p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
               p.StartInfo.CreateNoWindow = true;//不显示程序窗口
               p.Start();//启动程序

string strCMD = "\"" + @"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\SvcUtil.exe" + "\"  " + this.txt_URL.Text.ToString().Trim()
                   + " /r:"+"\""+@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll" +"\""+ " /syncOnly";
               //向cmd窗口发送输入信息
               p.StandardInput.WriteLine(strCMD + "&exit");

p.StandardInput.AutoFlush = true;

//获取cmd窗口的输出信息
               string output = p.StandardOutput.ReadToEnd();
               //等待程序执行完退出进程
               p.WaitForExit();
               p.Close();

MessageBox.Show(output);
               Console.WriteLine(output);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message + "\r\n跟踪;" + ex.StackTrace);
           }
       }
   }
}

点击创建按钮,会在bin\Debug目录下面生成对于的cs文件

来源:https://www.cnblogs.com/dotnet261010/p/7087290.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com