欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#程序調(diào)用cmd.exe執(zhí)行命令

 更新時(shí)間:2022年03月01日 09:08:03   作者:.NET開發(fā)菜鳥  
這篇文章介紹了C#程序調(diào)用cmd.exe執(zhí)行命令的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論