描述:程序只能开启一次,再次运行时会提示程序已经运行!
写在form程序的Program.cs中!
static void Main()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
//判断之前是否已经打开了相同的进程
//new Bestzone.MIS.Report.F1BookContainer();
bool alreadyExist = false;
try
{
System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in ps)
{
if (p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id)
{
alreadyExist = true;
}
}
}
catch (System.PlatformNotSupportedException ex) { ex.ToString(); }
catch (System.InvalidOperationException ex) { ex.ToString(); }
//如果已经存在,则放弃本进程
if (alreadyExist)
{
MessageBox.Show("该程序正在运行!");
return;
}
Application.Run(new Form1());
}