c#如何编程防止一个程序多次启动

解决方案 »

  1.   


    /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            Process test = RunningInstance();
                if (test == null)
                {
                    //Go on your code
                }
                else
                {
                   MessageBox.Show("Application already running!");
                }
            }        public static Process RunningInstance()
            {
                Process current = Process.GetCurrentProcess();
                Process[] processes = Process.GetProcessesByName(current.ProcessName);            foreach (Process process in processes)
                {
                    if (process.Id != current.Id)
                    {
                        if (Assembly.GetExecutingAssembly().Location.Replace("/ ", "\\ ") ==
                                current.MainModule.FileName)
                        {
                            return process;
                        }
                    }
                }
                return null;
            }
      

  2.   

    Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1