C#程序调用CMD执行命令

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

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

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Diagnostics;
 7
 8 namespace CmdDemo
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Console.WriteLine("请输入要执行的命令:");
15             string strInput = Console.ReadLine();
16             Process p = new Process();
17             //设置要启动的应用程序
18             p.StartInfo.FileName = "cmd.exe";
19             //是否使用操作系统shell启动
20             p.StartInfo.UseShellExecute = false;
21             // 接受来自调用程序的输入信息
22             p.StartInfo.RedirectStandardInput = true;
23             //输出信息
24             p.StartInfo.RedirectStandardOutput = true;
25             // 输出错误
26             p.StartInfo.RedirectStandardError = true;
27             //不显示程序窗口
28             p.StartInfo.CreateNoWindow = true;
29             //启动程序
30             p.Start();
31
32             //向cmd窗口发送输入信息
33             p.StandardInput.WriteLine(strInput+"&exit");
34
35             p.StandardInput.AutoFlush=true;
36
37              //获取输出信息
38             string strOuput = p.StandardOutput.ReadToEnd();
39             //等待程序执行完退出进程
40             p.WaitForExit();
41             p.Close();
42
43             Console.WriteLine(strOuput);
44
45             Console.ReadKey();
46         }
47     }
48 }

运行效果:

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

设计界面:

代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Diagnostics;
11
12 namespace ExecuteCMD
13 {
14     public partial class FrmMain : Form
15     {
16         public FrmMain()
17         {
18             InitializeComponent();
19         }
20
21         private void btn_Create_Click(object sender, EventArgs e)
22         {
23             try
24             {
25                 //创建一个进程
26                 Process p = new Process();
27                 p.StartInfo.FileName = "cmd.exe";
28                 p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
29                 p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
30                 p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
31                 p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
32                 p.StartInfo.CreateNoWindow = true;//不显示程序窗口
33                 p.Start();//启动程序
34
35                 string strCMD = "\"" + @"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\SvcUtil.exe" + "\"  " + this.txt_URL.Text.ToString().Trim()
36                     + " /r:"+"\""+@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll" +"\""+ " /syncOnly";
37                 //向cmd窗口发送输入信息
38                 p.StandardInput.WriteLine(strCMD + "&exit");
39
40                 p.StandardInput.AutoFlush = true;
41
42                 //获取cmd窗口的输出信息
43                 string output = p.StandardOutput.ReadToEnd();
44                 //等待程序执行完退出进程
45                 p.WaitForExit();
46                 p.Close();
47
48
49                 MessageBox.Show(output);
50                 Console.WriteLine(output);
51             }
52             catch (Exception ex)
53             {
54                 MessageBox.Show(ex.Message + "\r\n跟踪;" + ex.StackTrace);
55             }
56         }
57     }
58 }

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

时间: 2024-08-25 06:40:46

C#程序调用CMD执行命令的相关文章

C#程序调用cmd执行命令(转)

C#通过程序来调用cmd命令的操作 string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true

C#程序调用cmd执行命令-MySql备份还原

1.简单实例 //备份还原mysql public static void TestOne() { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.

C#程序调用CMD执行命令方法

先将adb.exe环境加入系统环境变量 { Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; //process.StartInfo.Arguments = "adb deviecs"; process.StartInfo.WorkingDirectory = "C:/Users/Administrator"; process.StartInfo.Us

C# 调用CMD执行命令行

这几天用c#做了一个项目,其中一个功能是要把生成的临时文件隐藏,同时,不能在屏幕上有调用CMD的痕迹,这里生成的临时文件的绝对路径为delfile为文件的绝对路径, 代码如下: private void HiddenFile() { System.Diagnostics.Process proRestart = new System.Diagnostics.Process();    //创新Process proRestart.StartInfo.WindowStyle = System.Di

linux 程序调用system执行命令

正确使用system方法,判断返回值 int exeCmd(const char *cmd) { pid_t status; status = system(cmd); if (-1 == status) { WriteLog("system error!"); } else { WriteLog("exit status value = [0x%x]\n", status); if (WIFEXITED(status)) { if (0 == WEXITSTATU

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

代码部分 using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Windows\System32\cmd.exe"; /// <summary> /// 执行cmd命令 /// 多命令请使用批处理命令连接符: /// <![CDATA[ /// &:同时执行两个命令 /// |:将上一个命令的输出,作为下一个命令的输入 /// &&

java调用cmd执行maven命令

一.原理介绍 Java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后封闭命令窗口. cmd /k dir 是执行完dir命令后不封闭命令窗口. cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会封闭. cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会封闭. 可以用cmd / 查看帮助信息. 二.java调用cmd执行maven package命

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

代码部分 using System.Diagnostics; public class CmdHelper     {         private static string CmdPath = @"C:\Windows\System32\cmd.exe";         /// <summary>         /// 执行cmd命令         /// 多命令请使用批处理命令连接符:         /// <![CDATA[         ///

C#程序调用cmd.exe执行其他exe进程(并且含多个参数),并把进程结果返回给字符串

1.关键代码部分. using System.Diagnostics; public class CmdHelper     {         private static string CmdPath = @"C:\Windows\System32\cmd.exe";         /// <summary>         /// 执行cmd命令         /// 多命令请使用批处理命令连接符:         /// <![CDATA[