C# winform 判断程序是否已经在运行 判断程序是否已经打开

版权声明:欢迎转载,转载请标明原文作者和链接。 如对您有帮助,不胜荣幸,同时也求高人指点 https://blog.csdn.net/a770kfof/article/details/77877398
在   Program.cs  打开的时候在里面修改
[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false); 
            
            if (IsRunning())
            {
                MessageBox.Show("程序已经打开!");
                return;
            }
            Application.Run(new Main());
        }

        public static bool IsRunning()
        {
            Process current = default(Process);
            current = System.Diagnostics.Process.GetCurrentProcess();
            Process[] processes = null;
            processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);

            Process process = default(Process);

            foreach (Process tempLoopVar_process in processes)
            {
                process = tempLoopVar_process;

                if (process.Id != current.Id)
                {
                    if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        return true;

                    }

                }
            }
            return false;

        }


没有更多推荐了,返回首页