c#判断外部可执行程序是否已打开(若未打开则打开)
#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
上一篇: 什么叫文化差异呢