可以解决同名的程序集(但非同一程序集)只能运行一次的问题,网上很资料都是只检测程序是否同名,不能真正的保证是同一程序集。
private bool prevInstance()
{
Process[] myProcesses= Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (myProcesses.Length > 1)
{
GuidAttribute curGuid = (GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false)[0]);
foreach (Process process in myProcesses)
{
if (Process.GetCurrentProcess().Id != process.Id)
{
Assembly ass = Assembly.LoadFrom(process.MainModule.FileName);
GuidAttribute[] guids = (GuidAttribute[])ass.GetCustomAttributes(typeof(GuidAttribute), false);
if (guids.Length > 0)
{
if (curGuid.Value.ToString().Equals(guids[0].Value.ToString()))
return true;
}
}
}
}
return false;
}