当前位置: 移动技术网 > IT编程>开发语言>.net > c#判断外部可执行程序是否已打开(若未打开则打开)

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

2018年03月03日  | 移动技术网IT编程  | 我要评论

太平间里的恶魔,六哥喊麦,电脑桌面图

#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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网