C#中防止程序多次运行
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//防止程序多次运行的代码
string MName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
string PName = System.IO.Path.GetFileNameWithoutExtension(MName);
System.Diagnostics.Process[] myProcess = System.Diagnostics.Process.GetProcessesByName(PName);
if (myProcess.Length > 1)
{
MessageBox.Show("本程序一次只能运行一个实例!", "提示", MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}