C#程序调用CMD执行命令

本文转载自: https://www.cnblogs.com/dotnet261010/p/7087290.html 作者:dotnet261010 转载请注明该声明。

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

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#的`Process`类来启动命令行并执行命令,同时使用`Timer`类来定时调用该方法。这样就可以避免阻塞了。 以下是一个示例代码: ```csharp using System; using System.Diagnostics; using System.Timers; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Timer timer = new Timer(5000); // 定时器间隔为5秒 timer.Elapsed += new ElapsedEventHandler(OnTimedEvent); timer.Enabled = true; Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } private static void OnTimedEvent(object source, ElapsedEventArgs e) { Console.WriteLine("Executing command..."); Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = "/C dir"; // 执行dir命令 process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.Start(); string output = process.StandardOutput.ReadToEnd(); Console.WriteLine(output); process.WaitForExit(); process.Close(); Console.WriteLine("Command executed."); } } } ``` 在上面的示例中,`OnTimedEvent`方法会定时执行,启动命令行并执行`dir`命令。注意,我们使用`/C`参数来告诉命令行执行完命令后自动关闭。同时,我们还将`RedirectStandardOutput`属性设置为`true`,这样就可以获取命令行输出并打印到控制台上。 另外,要注意的是,在执行命令时,我们使用了`process.WaitForExit()`方法来等待命令行执行完毕。这里可以根据具体情况调整等待时间,或者使用异步方式执行命令

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值