判断进程法:(修改程序名字后依然能执行)
private void Form1_Load(object sender, EventArgs e)
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (process.MainModule.FileName == current.MainModule.FileName)
{
MessageBox.Show("程序已经运行!", Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Close();
return;
}
}
}
}