日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#程序調用cmd.exe執行命令_C#教程

作者:.NET開發菜鳥 ? 更新時間: 2022-05-01 編程語言

在windows環境下,命令行程序為cmd.exe,是一個32位的命令行程序,微軟Windows系統基于Windows上的命令解釋程序,類似于微軟的DOS操作系統。輸入一些命令,cmd.exe可以執行,比如輸入shutdown -s就會在30秒后關機。總之,它非常有用。打開方法:開始-所有程序-附件 或 開始-尋找-輸入:cmd/cmd.exe 回車。它也可以執行BAT文件。

下面介紹使用C#程序調用cmd執行命令:

代碼:

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("請輸入要執行的命令:");
            string strInput = Console.ReadLine();
            Process p = new Process();
            //設置要啟動的應用程序
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系統shell啟動
            p.StartInfo.UseShellExecute = false;
            // 接受來自調用程序的輸入信息
            p.StartInfo.RedirectStandardInput = true;
            //輸出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 輸出錯誤
            p.StartInfo.RedirectStandardError = true;
            //不顯示程序窗口
            p.StartInfo.CreateNoWindow = true;
            //啟動程序
            p.Start();

            //向cmd窗口發送輸入信息
            p.StandardInput.WriteLine(strInput+"&exit");

            p.StandardInput.AutoFlush=true;

             //獲取輸出信息
            string strOuput = p.StandardOutput.ReadToEnd();
            //等待程序執行完退出進程
            p.WaitForExit();
            p.Close();

            Console.WriteLine(strOuput);

            Console.ReadKey();
        }
    }
}

運行效果:

應用:使用C#程序調用cmd命令生成WCF服務的客戶端調用文件

設計界面:

代碼如下:

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
            {
                //創建一個進程
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;//是否使用操作系統shell啟動
                p.StartInfo.RedirectStandardInput = true;//接受來自調用程序的輸入信息
                p.StartInfo.RedirectStandardOutput = true;//由調用程序獲取輸出信息
                p.StartInfo.RedirectStandardError = true;//重定向標準錯誤輸出
                p.StartInfo.CreateNoWindow = true;//不顯示程序窗口
                p.Start();//啟動程序

                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窗口發送輸入信息
                p.StandardInput.WriteLine(strCMD + "&exit");

                p.StandardInput.AutoFlush = true;

                //獲取cmd窗口的輸出信息
                string output = p.StandardOutput.ReadToEnd();
                //等待程序執行完退出進程
                p.WaitForExit();
                p.Close();


                MessageBox.Show(output);
                Console.WriteLine(output);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n跟蹤;" + ex.StackTrace);
            }
        }
    }
}

點擊創建按鈕,會在bin\Debug目錄下面生成對于的cs文件

原文鏈接:https://www.cnblogs.com/dotnet261010/p/7087290.html

相關推薦

欄目分類
最近更新