C# 判断当前控制台程序是否重复运行

 /// <summary>
        /// 获取正在运行的程序,没有运行的程序则返回null
        /// </summary>
        /// <returns></returns>
        private static Process RunningInstance()
        {
            Process currentProcess = Process.GetCurrentProcess();

            var processcollection = Process.GetProcessesByName(currentProcess.ProcessName.Replace(".vshost", "")).Where(a => a.Id != currentProcess.Id).ToList();
            foreach (Process process in processcollection)
            {
                if (process.ProcessName == currentProcess.ProcessName)
                {
                    return process;
                }
            }

            return null;
        }

posted on 2022-11-22 16:39  快乐海盗  阅读(181)  评论(0编辑  收藏  举报