C#啟動外部Exe控制台程序並輸入命令


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace ReNewCert
{
    class Program
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            //設置要啟動的應用程序
            p.StartInfo.FileName = "C:\\Desktop\\wacs.exe";//是否使用操作系統shell啟動
            p.StartInfo.UseShellExecute = false;
            // 接受來自調用程序的輸入信息
            p.StartInfo.RedirectStandardInput = true;
            //輸出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 輸出錯誤
            p.StartInfo.RedirectStandardError = true;
            //不顯示程序窗口
            p.StartInfo.CreateNoWindow = true ;
           
            //啟動程序
            p.Start();
            Thread.Sleep(5000);
            //向cmd窗口發送輸入信息
            p.StandardInput.WriteLine("R");
            p.StandardInput.WriteLine("exit");
            p.StandardInput.AutoFlush =true ;
            Thread.Sleep(5000);
            p.StandardInput.WriteLine("Q");
            p.StandardInput.WriteLine("exit");
            string strOuput = p.StandardOutput.ReadToEnd();
            //等待程序執行完退出進程
            p.WaitForExit();
            p.Close();
          
            Console.WriteLine(strOuput);            
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM