欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#判断外部可执行程序是否已打开(若未打开则打开)

程序员文章站 2022-04-12 12:49:49
#region 通过当前代码执行路径向上找到相关exe,并根据processes.Length判断是否已启动 private bool CheckAndOpenExe(string exeName) { Process[] processes = Process.GetProcessesByName ......

#region 通过当前代码执行路径向上找到相关exe,并根据processes.Length判断是否已启动

private bool CheckAndOpenExe(string exeName)
{
Process[] processes = Process.GetProcessesByName(exeName);
if (processes.Length > 0)
{
return true;
}
else
{
return OpenExe();
}
}

 

private bool OpenExe()
{
Process pr = new Process();
try
{
pr.StartInfo.FileName = string.Format(@"{0}\..\LotteryPro\LotteryPro.exe", AssemblyDirectory);
pr.Start();
return true;
}
catch
{
return true;
}
finally
{
if (pr != null)
{
pr.Close();
}

}
}

 

public static string AssemblyDirectory
{
get
{
string codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}//获取当前代码运行的目录

#endregion