C#程序只运行一次,防止多次启动
原理就是查找进程中该程序的运行数量,如果超过一个,就弹出信息,退出运行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SystemControl
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
{
MessageBox.Show("程序正在运行中!", "提示信息");
return;
}
Application.Run(new Form1());
}
}
}