C# 设置应用程序不能多开

设置应用程序不能多开

在项目实际应用中,经常会涉及到应用程序不允许多开的问题。
根据应用程序在操作系统中运行,都有一个自己的id,我们可以借此进行操作

  1. 通过遍历当前运行的所有程序id
  2. 判断是否存在和正要启动的程序是否存在相同id的程序正在运行
  3. 判断相同id的程序是否位于同一运行路径
  4. 决定当前程序是否允许启动
实例1:
using System;
using System.Diagnostics;
using System.Reflection;

namespace TestSingleInstance
{
    class Program
    {
        static void Main(string[] args)
        {
            if (RunningInstance() != null)
                return;

            //开启程序部分
            //......
            Console.WriteLine("成功启动");
            Console.ReadKey();
        }

        /// <summary>
        /// 返回相同运行路径的 Process
        /// </summary>
        /// <returns></returns>
        private 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;
        }
    }
}

这儿我们还可以添加一些其他的功能,比如,当已有一个进程再运行中时,可以将已经正在运行的程序显示出来,并前置。
需要用到user32.dll中的两个方法 ShowWindowAsync 显示窗口 和 SetForegroundWindow 窗口前置

引用 user32.dll

1.位置: C:/windows/system32/user32.dll
2.网上自行下载

实例2:
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

namespace TestSingleInstance
{
    class Program
    {
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(System.IntPtr hWnd);

        static void Main(string[] args)
        {
            Process p1 = RunningInstance();
            if (p1 != null)
            {
                ShowWindowAsync(p1.MainWindowHandle, 1); //调用api函数,正常显示窗口   0: 后台执行;1:正常显示窗口;2:最小化到任务栏;3:最大化
                SetForegroundWindow(p1.MainWindowHandle); //将窗口放置最前端
                return;
            }

            //开启程序部分
            //......
            Console.WriteLine("成功启动");
            Console.ReadKey();
        }

        /// <summary>
        /// 返回相同运行路径的 Process
        /// </summary>
        /// <returns></returns>
        private 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;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Q672405097/article/details/89509301