VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之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


相关教程