C#程序调用外部exe程序

在编写程序时常常会使用到调用可执行程序的状况,本文将简单介绍C#调用exe的方法。在C#中,经过Process类来进行进程操做。 Process类在System.Diagnostics包中。shell

示例一

using System.Diagnostics;
Process p = Process.Start("notepad.exe");
p.WaitForExit();//关键,等待外部程序退出后才能往下执行

经过上述代码能够调用记事本程序,注意若是不是调用系统程序,则须要输入全路径。code

示例二

当须要调用cmd程序时,使用上述调用方法会弹出使人讨厌的黑窗。若是要消除,则须要进行更详细的设置。进程

Process类的StartInfo属性包含了一些进程启动信息,其中比较重要的几个字符串

FileName                可执行程序文件名cmd

Arguments              程序参数,已字符串形式输入 
CreateNoWindow     是否不须要建立窗口 
UseShellExecute      是否须要系统shell调用程序it

经过上述几个参数能够让讨厌的黑屏消失class

System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = binStr;
exep.StartInfo.Arguments = cmdStr;
exep.StartInfo.CreateNoWindow = true;
exep.StartInfo.UseShellExecute = false;
exep.Start();exep.WaitForExit();//关键,等待外部程序退出后才能往下执行

或者程序

System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = binStr;
startInfo.Arguments = cmdStr;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.Start(startInfo);
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行
相关文章
相关标签/搜索