码迷,mamicode.com
首页 > 其他好文 > 详细

保证应用程序只有一个实例运行

时间:2018-07-18 14:13:44      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:name   other   running   color   exe   mod   processes   oca   []   

         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 the other process instance.
                        return process;
                    }
                }
            }
            //No other instance was found, return null.
            return null;
        }

 

保证应用程序只有一个实例运行

标签:name   other   running   color   exe   mod   processes   oca   []   

原文地址:https://www.cnblogs.com/lee24789229/p/9328256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!