C#:程序只打开一次,再打开自动激活之前打开的

这篇博客介绍了如何使用C#编写代码,确保程序在运行时只启动一次。当尝试再次打开程序时,它会自动激活已打开的实例。

简单点。

直接上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Test
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Process process = GetRunningProcess();
            if (process != null)
            {
                ShowRunningApplication(process);
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }

        /// <summary>
        /// 获取已运行的进程,未运行返回null
        /// </summary>
        /// <returns></returns>
        private static Process GetRunningProcess()
        {
            //当前程序的进程
            Process currentProcess=Process.GetCurrentProcess(); 

            //在系统进程中查找当前进程名
            Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);

            foreach (Process process in processes)
            {
                if (process.Id != currentProcess.Id) //不是本次打开的程序的进程自己
                {
                    //当前程序的含路径文件名
                    string fileName = Assembly.GetExecutingAssembly().Location.Replace("/", "//");

                    //进程的含路径文件名
                    string processFileName = process.MainModule.FileName; 

                    if (fileName == processFileName) //确认过是一个东西
                    {
                        return process;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 显示已运行的程序
        /// </summary>
        /// <param name="process"></param>
        private static void ShowRunningApplication(Process process)
        {
            IntPtr hwnd = process.MainWindowHandle; //获取进程的窗体句柄
            ShowWindowAsync(hwnd, 1); //显示窗体
            SetForegroundWindow(hwnd); //窗体前置
        }


        /// <summary>
        /// 功能是该函数设置由不同线程产生的窗口的显示状态
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="cmdShow">SW_HIDE:0  SW_NORMAL:1  SW_MAXIMIZE:3  SW_MINIMIZE:6  SW_RESTORE:9</param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

        /// <summary>
        /// 函数将创建指定窗口的线程设置到前台,并且激活该窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值