C#程序調(diào)用cmd.exe執(zhí)行命令
在windows環(huán)境下,命令行程序?yàn)閏md.exe,是一個(gè)32位的命令行程序,微軟Windows系統(tǒng)基于Windows上的命令解釋程序,類似于微軟的DOS操作系統(tǒng)。輸入一些命令,cmd.exe可以執(zhí)行,比如輸入shutdown -s就會(huì)在30秒后關(guān)機(jī)??傊浅S杏?。打開方法:開始-所有程序-附件 或 開始-尋找-輸入:cmd/cmd.exe 回車。它也可以執(zhí)行BAT文件。
下面介紹使用C#程序調(diào)用cmd執(zhí)行命令:
代碼:
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("請(qǐng)輸入要執(zhí)行的命令:"); string strInput = Console.ReadLine(); Process p = new Process(); //設(shè)置要啟動(dòng)的應(yīng)用程序 p.StartInfo.FileName = "cmd.exe"; //是否使用操作系統(tǒng)shell啟動(dòng) p.StartInfo.UseShellExecute = false; // 接受來自調(diào)用程序的輸入信息 p.StartInfo.RedirectStandardInput = true; //輸出信息 p.StartInfo.RedirectStandardOutput = true; // 輸出錯(cuò)誤 p.StartInfo.RedirectStandardError = true; //不顯示程序窗口 p.StartInfo.CreateNoWindow = true; //啟動(dòng)程序 p.Start(); //向cmd窗口發(fā)送輸入信息 p.StandardInput.WriteLine(strInput+"&exit"); p.StandardInput.AutoFlush=true; //獲取輸出信息 string strOuput = p.StandardOutput.ReadToEnd(); //等待程序執(zhí)行完退出進(jìn)程 p.WaitForExit(); p.Close(); Console.WriteLine(strOuput); Console.ReadKey(); } } }
運(yùn)行效果:
應(yīng)用:使用C#程序調(diào)用cmd命令生成WCF服務(wù)的客戶端調(diào)用文件
設(shè)計(jì)界面:
代碼如下:
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 { //創(chuàng)建一個(gè)進(jìn)程 Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false;//是否使用操作系統(tǒng)shell啟動(dòng) p.StartInfo.RedirectStandardInput = true;//接受來自調(diào)用程序的輸入信息 p.StartInfo.RedirectStandardOutput = true;//由調(diào)用程序獲取輸出信息 p.StartInfo.RedirectStandardError = true;//重定向標(biāo)準(zhǔn)錯(cuò)誤輸出 p.StartInfo.CreateNoWindow = true;//不顯示程序窗口 p.Start();//啟動(dòng)程序 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窗口發(fā)送輸入信息 p.StandardInput.WriteLine(strCMD + "&exit"); p.StandardInput.AutoFlush = true; //獲取cmd窗口的輸出信息 string output = p.StandardOutput.ReadToEnd(); //等待程序執(zhí)行完退出進(jìn)程 p.WaitForExit(); p.Close(); MessageBox.Show(output); Console.WriteLine(output); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n跟蹤;" + ex.StackTrace); } } } }
點(diǎn)擊創(chuàng)建按鈕,會(huì)在bin\Debug目錄下面生成對(duì)于的cs文件
到此這篇關(guān)于C#程序調(diào)用cmd.exe執(zhí)行命令的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C# 執(zhí)行CMD命令并接收返回結(jié)果的操作方式
- C# 調(diào)用命令行執(zhí)行Cmd命令的操作
- C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法
- C#隱式運(yùn)行CMD命令(隱藏命令窗口)
- C#調(diào)用CMD命令實(shí)例
- c#通過進(jìn)程調(diào)用cmd判斷登錄用戶權(quán)限代碼分享
- C# cmd中修改顯示(顯示進(jìn)度變化效果)的方法
- C#中調(diào)用命令行cmd開啟wifi熱點(diǎn)的實(shí)例代碼
- 通過C#調(diào)用cmd來修改服務(wù)啟動(dòng)類型
- 在asp.net(c#)下實(shí)現(xiàn)調(diào)用cmd的方法
- C#中隱式運(yùn)行CMD命令行窗口的方法
相關(guān)文章
Unity接入百度AI實(shí)現(xiàn)通用物體和場(chǎng)景識(shí)別
這篇文章主要介紹了在Unity中接入百度AI接口,從而實(shí)現(xiàn)通用物體和場(chǎng)景的識(shí)別,其中接口返回大類及細(xì)分類的名稱,并支持獲取識(shí)別結(jié)果對(duì)應(yīng)的百科信息。感興趣的可以學(xué)習(xí)一下2022-01-01C#日期時(shí)間類的使用方法(DateTime類、TimeSpan類與DateTimeOffset類)
在C#中我們常使用到關(guān)于時(shí)間的相關(guān)操作,這篇文章主要給大家介紹了關(guān)于C#日期時(shí)間類的使用方法,文中介紹的方法分別包括DateTime類、TimeSpan類與DateTimeOffset類的相關(guān)資料,需要的朋友可以參考下2023-11-11C#難點(diǎn)逐個(gè)擊破(9):類型轉(zhuǎn)換
類型之間的轉(zhuǎn)換可以分為隱式轉(zhuǎn)換與顯式轉(zhuǎn)換,如int類型可直接轉(zhuǎn)換為long類型。2010-02-02Unity3D Shader實(shí)現(xiàn)鏡子效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)鏡子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05c#實(shí)現(xiàn)sunday算法實(shí)例
Sunday算法思想跟BM算法很相似,在匹配失敗時(shí)關(guān)注的是文本串中參加匹配的最末位字符的下一位字符,下面是用C#實(shí)現(xiàn)sunday的實(shí)例代碼,有需要的朋友可以參考一下2013-08-08