c# 执行cmd命令跳过"press any key to continue"

一 、场景

       在C#程序中需要调用一个exe程序(控制台应用程序),该exe程序执行到最后出现 “press any key to continue”,需按键结束。要求在C#程序中实现模拟输入,结束调用的exe程序。

二、代码实现

  1. exe程序代码:
    #include "stdafx.h"
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int i=3;
    	while(i){
    		printf("本进程执行位置%d!\n",i);
    		Sleep(1000);
    		i--;
    	} 
    	system("Pause"); 
    	return 0; 
    }
  2. C#程序代码:
    public static void RunCmd(string cmd)
    {
           Process MyProcess = new Process();
           MyProcess.StartInfo.FileName = "cmd.exe";//获取或设置要传递给 Process 的 Start 方法的属性。
    
           MyProcess.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
           MyProcess.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
           MyProcess.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
           MyProcess.StartInfo.RedirectStandardError = true;//重定向标准错误输出
           MyProcess.StartInfo.CreateNoWindow = true;//不显示程序窗口
           //设置参数
           MyProcess.StartInfo.Arguments = "/c " + cmd;  //  /c是执行完命令后关闭命令窗口 
           MyProcess.Start(); 
    
           //若在此处获取输出信息,进程会因为需要按键而阻塞
           //string output = MyProcess.StandardOutput.ReadToEnd();
    
           while (!MyProcess.HasExited)
           {
               MyProcess.StandardInput.WriteLine();//输入一个字符,以结束进程
           }
           //输入字符后获取输出信息
           string output = MyProcess.StandardOutput.ReadToEnd();
           Console.WriteLine(output);
           MyProcess.WaitForExit(); 
           MyProcess.Close(); 
    }
    
    static void Main(string[] args)
    {
           string cmd = @"***\pauseTest.exe";
           RunCmd(cmd);
           Console.WriteLine("end");
    }
  3.  输出(运行程序的时候打断点才能看到):

         

三、错误代码示例:

  1. 代码 (无法模拟按键跳过pause):
    public static void RunCmd_Wrong(string cmd)
    {
           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();//启动程序 
    
           p.StandardInput.WriteLine(cmd);    //cmd 命令并不是作为cmd.exe进程的参数输入的而是代码写入的。
           p.StandardInput.AutoFlush = true;
           p.StandardInput.WriteLine("exit");
                
           while (!p.HasExited)    //当执行到 pause 时,此处会阻塞,并不会继续往下执行了
           {
                p.StandardInput.WriteLine();//输入一个字符,以结束进程 
           }
           //获取cmd窗口的输出信息
           string output = p.StandardOutput.ReadToEnd();
           Console.WriteLine(output);
               
           p.WaitForExit();
           p.Close(); 
    }
  2. 区别:

  • 正确方法将cmd命令作为进程的参数,然后启动进程:

MyProcess.StartInfo.Arguments = "/c " + cmd;

  • 错误方法是先启动进程,然后将cmd命令写入:

p.Start();//启动程序 

p.StandardInput.WriteLine(cmd);

    3. 没有 Pause 情况下对比输出

  • RunCmd():

        

  • RunCmd_Wrong():

      

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值