领先的中文IT技术网站    IT技术从现在起飞

飞诺旗下: 技术社区 | 在线电子书 | 在线试题 | 资源下载 | 飞诺搜索 | 技术博客
用户名: 密   码:
   飞诺网 加入收藏
飞诺网 Asp.net 新闻频道 开发频道 系统频道 服务器 网络频道 网络安全 Java频道 C/C++ PHP开发 电子书 资源下载 社 区 博 客 在线试题
网站开发 VBScript ASP Asp.net Jsp php XML CGI-Perl 搜索引擎 ajax
编程开发 JAVA C/C++ C++ VC C语言 VB C# Delphi Foxpro 汇编 shell编程 游戏开发 软件工程师 WEB开发 PHP ASP Asp.net JSP AJAX CGI JavaScript HTML CSS 数据库 MSSQL Mysql Oracle Access Sybase DB2 sql2005 Office Word Excel Powerpoint Wps 认证考试 二级C语言 三级网络 程序员 网络工程师 思科认证

您当前的位置:飞诺网 >> .net >> .Net技术文章

C# 限制软件单进程运行

www.firnow.com    时间 : 2008-09-23  作者:佚名   编辑:本站 点击:   [ 评论 ]


  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. using System.Diagnostics;
  5. using System.Reflection;
  6. namespace AvsRecoder
  7. {
  8.     static class Program
  9.     {
  10.         /// <summary>
  11.         /// 应用程序的主入口点。
  12.         /// </summary>
  13.         [STAThread]
  14.         static void Main()
  15.         {
  16.             Application.EnableVisualStyles();
  17.             Application.SetCompatibleTextRenderingDefault(false);
  18.             Process instance = RunningInstance();
  19.             if (instance == null)
  20.             {
  21.                 //如果没有其它例程,就新建一个窗体   
  22.                 Application.Run(new mainForm());
  23.             }
  24.             else
  25.             {
  26.                 MessageBox.Show("程序已在运行");
  27.             }
  28.         }
  29.         public static Process RunningInstance()
  30.         {
  31.             Process current = Process.GetCurrentProcess();
  32.             Process[] processes = Process.GetProcessesByName(current.ProcessName);
  33.             //遍历正在有相同名字运行的例程   
  34.             foreach (Process process in processes)
  35.             {
  36.                 //忽略现有的例程   
  37.                 if (process.Id != current.Id)
  38.                 {
  39.                     //确保例程从EXE文件运行   
  40.                     if (Assembly.GetExecutingAssembly().Location.Replace("/""\\") ==
  41.                         current.MainModule.FileName)
  42.                     {
  43.                         //返回另一个例程实例   
  44.                         return process;
  45.                     }
  46.                 }
  47.             }
  48.             //没有其它的例程,返回Null   
  49.             return null;
  50.         }
  51.     }  // end class
  52. }   // end NameSpace
 
如果图片或页面不能正常显示请点击这里
.Net技术文章推荐文章

文章评论

BBS社区热贴