• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

让程序只运行一个实例(C#)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

通常我们会遇到这样的情况,让程序只运行一个实例,比如启动杀毒软件时,只能启动一个,再启动的话就没什么效果!利用程序名来判断不是一个好办法,如果我们把程序名称改一下就可以运行两个完全一样的进程。我们最好利用程序集的Attribute存放特定信息,然后用Assembly对象的GetCustomAttributes()方法获取该信息进行判断。
在这里,我有两个解决方法:

方法一.我把AssemblyInfo.cs里的[assembly: AssemblyFileVersion("1.0.0.0")]改为[assembly:AssemblyFileVersion("2.0.0.8")],然后利用该信息进行判断。
代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Collections;
using System.Threading;

namespace MyWork_01
{
class Program
{
staticvoid Main(string[] args)
{
Process[] processes
= Process.GetProcesses(); //获得当前所有进程
Process currentProcess = Process.GetCurrentProcess(); //获取当前正在运行进程
ProcessModule currentPM = currentProcess.Modules[0];

int same =0; //相同运行实例个数

ArrayList proList
=new ArrayList(); //将相同实例加入此集合中

foreach (Process p in processes)
{
try//由于进程不同,有的进程不包含Modules信息,所以要用try保护
{
if (p.Modules !=null)
if (p.Modules.Count >0)
{
System.Diagnostics.ProcessModule pm
= p.Modules[0];

if (pm.FileVersionInfo.FileVersion.Equals(currentPM.FileVersionInfo.FileVersion))
{
same
++;
proList.Add(p);
}

if (same >1)
{
same
++;
proList.Add(p);
if (same >1)
{
for (int i =0; i < proList.Count; i++)
{
if (((Process)(proList[i])).Id == currentProcess.Id)
{
Console.WriteLine(
"该进程已经启动了一个实例");
Thread.Sleep(
1000);
((Process)(proList[i])).Kill();
}
}
}
}
}
}
catch
{ }
}
Console.Read();
}
}
}

方法二:直接定义一个属性类,利用此属性信息进行判断。
代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Threading;

[assembly: Help(
"This Assembly demonstrates custom attributes creation and their run-time query.")]
publicclass HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description;
public String Description
{
get
{
returnthis.description;
}
}
}
class Program
{
staticvoid Main(string[] args)
{
HelpAttribute HelpAttr1
=null;
HelpAttribute HelpAttr2
=null;
Process currentProcess
= Process.GetCurrentProcess(); //获取当前正在运行进程
Assembly a = Assembly.LoadFrom(currentProcess.MainModule.FileName);
foreach (Attribute attr in a.GetCustomAttributes(true))
{
HelpAttr1
= attr as HelpAttribute;
if (null!= HelpAttr1)
{
//Console.WriteLine("Description of {0}:\n{1}", currentProcess.MainModule.FileName, HelpAttr1.Description);
break;
}
}
Process[] processes
= Process.GetProcesses(); //获得当前所有进程
int same =0; //相同运行实例个数
ArrayList proList =new ArrayList(); //将相同实例加入此集合中
foreach (Process pro in processes)
{
try//由于进程不同,有的进程不包含Modules信息,所以要用try保护
{
if (pro.Modules !=null)
if (pro.Modules.Count >0)
{
Assembly b
= Assembly.LoadFrom(pro.MainModule.FileName);
foreach (Attribute attr in b.GetCustomAttributes(true))
{
HelpAttr2
= attr as HelpAttribute;
if (null!= HelpAttr2)
{
if (HelpAttr1.Description.Equals(HelpAttr2.Description))
{
same
++;
proList.Add(pro);
if (same >1)
{
for (int i =0; i < proList.Count; i++)
{
if (((Process)(proList[i])).Id == currentProcess.Id )
{
Console.WriteLine(
"该进程已经启动了一个实例");

Thread.Sleep(
1000);

((Process)(proList[i])).Kill();

}
}
}
}
}
}
}
}
catch
{
}
}
Console.ReadLine();
}

}

参考:
C#中的属性http://www.vckbase.com/document/viewdoc/?id=994


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
LINQ系列:C#中与LINQ相关特性发布时间:2022-07-10
下一篇:
用C#生成不重复的随机数发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap