C # call external programs of the two methods

I civilization, which represents the identity and my personal opinion, there are different views can leave a message Ha, thank you for reading the article there is a typo or error codes please correct me, thank you oh.

c # to call an external program exe:

  1. Process calls:
                Process process = new Process();
                process.StartInfo.UseShellExecute = false; //必要参数
                process.StartInfo.RedirectStandardOutput = true;//输出参数设定
                process.StartInfo.RedirectStandardInput = true;//传入参数设定
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\map.exe";
                process.Start();
                process.WaitForExit();//等待程序执行完退出进程    
                process.Close();
  2. cmd call:
                cmd=""  //cmd命令
        
                cmd = cmd + "&exit";
                Process process = new Process())
            
                process.StartInfo.FileName = @"cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();//启动程序
                process.StandardInput.WriteLine(cmd); //向cmd窗口写入命令
                process.StandardInput.AutoFlush = true;

                process.WaitForExit();//等待程序执行完退出进程
                process.Close();
            
        

External program, if the program does not automatically end the program interruption, add the taskkill command in the command line interrupt process  

End Process: taskkill / f / t / im process name; Parameter Description: 

 1, / f Specifies to forcefully terminate the process.

2, / t to terminate the specified process and any child processes thus initiated.

 Why use this two similar methods? ? A little time can not directly run the program must use the command-line invocation, python package code Sometimes the problem will be a commonly used method to, (a little more powerful method two)

Published 35 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/wenming111/article/details/83857829