C#调用一下CMD


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文件

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM