C#防止应用多开

有一些应用,我们不希望被用户多次打开。那么我们需要在应用的入口做一些处理。我把我应用里的代码贴出来。

在衡阳县等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站设计 网站设计制作定制设计,公司网站建设,企业网站建设,高端网站设计,网络营销推广,成都外贸网站制作,衡阳县网站建设费用合理。

1、如果只是需要,发现已经打开的时候,直接退出的话,用下面的代码:

static void Main()
        {
   #region 防止多开
            Process CurProc= Process.GetCurrentProcess();
            Process[] Procs= Process.GetProcessesByName(CurProc.ProcessName.Replace(".vshost", string.Empty));
   if (Procs.Length > 1)
            {
                MessageBox.Show("应用已打开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
       return;
            }
   #endregion

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

相当于在Main函数的开始部分,判断一下是否已经有相同进程,有的话,直接退出。

 

2、如果发现已经打开的话,退出当前进程,并且切换激活到前面打开的进程。那么需要用user32.dll库文件里的函数。

#region 防止多开
            Process CurProc= Process.GetCurrentProcess();
            Process[] Procs= Process.GetProcessesByName(CurProc.ProcessName.Replace(".vshost", string.Empty));
   if (Procs.Length > 1)
            {
       foreach (Process proc in Procs)
                {
           if (proc.Id != CurProc.Id)
                    {
               if (proc.MainWindowHandle.ToInt32() == 0)
                        {
                   // 获得窗体句柄
                            formhwnd = FindWindow(null, "PictureManager");
                   // 重新显示该窗体并切换到带入到前台
                            ShowWindow(formhwnd, SW_RESTORE);
                            SwitchToThisWindow(formhwnd,true);
                        }
               else
                        {
                   // 如果窗体没有隐藏,就直接切换到该窗体并带入到前台
                   // 因为窗体除了隐藏到托盘,还可以最小化
                            SwitchToThisWindow(proc.MainWindowHandle, true);
                        }
                    }
                }
       return;
            }
   #endregion
里面用到的几个函数需要用到user32.dll库文件。需要引用一下。
#region 方法四:使用的Win32函数的声明

        /// 
        /// 找到某个窗口与给出的类别名和窗口名相同窗口
/// 非托管定义为:http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
/// 
        /// 类别名
        /// 窗口名
        /// 成功找到返回窗口句柄,否则返回null
        [DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

/// 
        /// 切换到窗口并把窗口设入前台,类似 SetForegroundWindow方法的功能
/// 
        /// 窗口句柄
        /// True代表窗口正在通过Alt/Ctrl +Tab被切换
        [DllImport("user32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

/////
        /////  设置窗口的显示状态
/////  Win32 函数定义为:http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
/////
        /////窗口句柄
        /////指示窗口如何被显示
        /////如果窗体之前是可见,返回值为非零;如果窗体之前被隐藏,返回值为零
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
public const int SW_RESTORE = 9;
public static IntPtr formhwnd;
#endregion        /// 

注:用到user32.dll里面的函数以后,在用InstallShield Limited Edition制作安装包的时候,会报错,提示你添加user32.dll,目前我还没有解决。所以现在是直接用方法1。


当前文章:C#防止应用多开
网页地址:http://ybzwz.com/article/jdiegd.html