单例启动程序

string FilePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, AppDomain.CurrentDomain.SetupInformation.ApplicationName);
 Assembly asm = Assembly.LoadFrom(FilePath);    //当前程序使用Assembly.Load会加载失败
 AssemblyCompanyAttribute asmcpn = (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(asm, typeof(AssemblyCompanyAttribute));
bool createdNew;
System.Threading.Mutex _mutex = new System.Threading.Mutex(true, AppDomain.CurrentDomain.SetupInformation.ApplicationName+ asmcpn.Company,out createdNew);

//winform应用程序使用Application获取相关名称

//System.Threading.Mutex _mutex = new System.Threading.Mutex(true, Application.ProductName + Application.CompanyName,out createdNew);
if(createdNew)
{
        Console.WriteLine("startNew!");

        _mutex.ReleaseMutex();
}
else
{
        Console.WriteLine("Procedure is exist!");
}
Console.ReadLine();

posted on 2020-06-16 14:45  fanu  阅读(149)  评论(0)    收藏  举报

导航