C#调用外部程序的两种方法

我是文明,以下即代表我的个人认同与看法,有不同看法的可以留言哈,谢谢你的阅读,文章有错字或代码错误请指正,谢谢你哦。

c# 调用外部程序  exe:

  1. 进程调用:
                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 调用:
                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();
            
        

外部程序,如果程序结束时程序不会自动中断,请在命令行中添加taskkill命令中断进程  

结束进程  :taskkill /f /t /im 进程名;  参数解释: 

 1、/f      指定要强行终止进程。

2、/t      终止指定的进程和任何由此启动的子进程。

 为什么要使用这个两个类似的方法呢??  有点时候不能直接运行程序   必须使用命令行调用,python封装的代码有时候就会出现这个问题  通常使用方法一即可,(方法二更强大一点)

发布了35 篇原创文章 · 获赞 8 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wenming111/article/details/83857829