.net利用程序集的GUID解决程序只能运行一次的问题
可以解決同名的程序集(但非同一程序集)只能運行一次的問題,網上很資料都是只檢測程序是否同名,不能真正的保證是同一程序集。
?
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;
}
轉載于:https://www.cnblogs.com/blogsChen/p/3884577.html
總結
以上是生活随笔為你收集整理的.net利用程序集的GUID解决程序只能运行一次的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自定义UISearchBar的背景图
- 下一篇: compare two oracle d