C# 打开 EXE 文件

命名空间是using System.Diagnostics;

 

 

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

示例一

using System.Diagnostics;

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

 

通过上述代码可以调用记事本程序,注意如果不是调用系统程序,则需要输入全路径。

示例二

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

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

FileName                可执行程序文件名

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

通过上述几个参数可以让讨厌的黑屏消失

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();//关键,等待外部程序退出后才能往下执行

 

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

 

 

string name = "aaa";//程序进程名称
   int ProgressCount = 0123456;//判断进程是否运行的标识
   Process[] prc = Process.GetProcesses(); 
   foreach(Process pr in prc) //遍历整个进程
   {  
            if (name == pr.ProcessName)  //如果进程存在
            {     
                    ProgressCount = 0; //计数器清空
                     return;
            }  
   }
   if(ProgressCount!=0)//如果计数器不为0,说名所指定程序没有运行
   {
    try
    {
     //调用外部程序
     Process MyProcess = new Process();
     MyProcess.StartInfo.FileName = "d:/aaa.exe";
     MyProcess.StartInfo.Verb = "Open";
     MyProcess.StartInfo.CreateNoWindow = true;
     MyProcess.Start();
    }
    catch(Exception d)
    {
     MessageBox.Show(d.Message+"","提示!!!!");
    }
   }
   else
   {
    MessageBox.Show("对不起,本地已经有系统正在运行!/n.","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
   }
  

c#调用外部程序


命名空间是using System.Diagnostics;

程序源码为:
privater void StaartForm()
{
     Process MyProcess = new Process();
     MyProcess.StartInfo.FileName = "d:/aaa.exe";//外部程序路径
     MyProcess.StartInfo.Verb = "Open";
     MyProcess.StartInfo.CreateNoWindow = true;
     MyProcess.Start();
}

posted on 2019-05-14 09:47  马什么梅  阅读(2116)  评论(0编辑  收藏  举报

导航