【C#】限制应用程序打开一次

 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Process[] proc = Process.GetProcesses();
            int cnt = 0;
            for (int i = 0; i < proc.Length; i++)
            {
                if (proc[i].ProcessName != null && proc[i].ProcessName == "xxx")
                {
                    cnt++;
                }
                if (cnt > 1)
                {
                    MessageBox.Show("重复启动" + proc[i].ProcessName);
                    Application.Exit();
                    return;
                }
            }        

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

 

posted @ 2022-07-06 14:31  不溯流光  阅读(89)  评论(0编辑  收藏  举报