让程序只运行一个实例比如启动杀毒软件时只能启动一个再启动的话就没什么效果!利用程序名来判断不是一个好办法如果我们把程序名称改一下就可以运行两个完全一样的进程我们最好利用程序集的Attribute存放特定信息然后用Assembly对象的GetCustomAttributes()方法获取该信息进行判断
在这里我有两个解决方法
方法一我把AssemblyInfocs里的[assembly AssemblyFileVersion()]改为[assemblyAssemblyFileVersion()]然后利用该信息进行判断
代码如下
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemDiagnostics;
using SystemReflection;
using SystemCollections;
using SystemThreading;
namespace MyWork_
{
class Program
{
static void Main(string[] args)
{
Process[] processes = ProcessGetProcesses(); //获得当前所有进程
Process currentProcess = ProcessGetCurrentProcess(); //获取当前正在运行进程
ProcessModule currentPM = currentProcessModules[];
int same = ; //相同运行实例个数
ArrayList proList = new ArrayList(); //将相同实例加入此集合中
foreach (Process p in processes)
{
try//由于进程不同有的进程不包含Modules信息所以要用try保护
{
if (pModules != null)
if (pModulesCount > )
{
SystemDiagnosticsProcessModule pm = pModules[];
if (pmFileVersionInfoFileVersionEquals(currentPMFileVersionInfoFileVersion))
{
same++;
proListAdd(p);
}
if (same > )
{
same++;
proListAdd(p);
if (same > )
{
for (int i = ; i < proListCount; i++)
if (((Process)(proList[i]))Id == currentProcessId)
{
ConsoleWriteLine(该进程已经启动了一个实例);
ThreadSleep();
((Process)(proList[i]))Kill();
}
}
}
}
}
}
catch
{ }
}
ConsoleRead();
}
}
}
方法二直接定义一个属性类利用此属性信息进行判断
代码如下
?using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemReflection;
using SystemDiagnostics;
using SystemCollections;
using SystemThreading;
[assembly: Help(This Assembly demonstrates custom attributes creation and their runtime query)]
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
thisdescription = Description_in;
}
protected String description;
public String Description
{
get
{
return thisdescription;
}
}
}
class Program
{
static void Main(string[] args)
{
HelpAttribute HelpAttr = null;
HelpAttribute HelpAttr = null;
Process currentProcess = ProcessGetCurrentProcess(); //获取当前正在运行进程
Assembly a = AssemblyLoadFrom(currentProcessMainModuleFileName);
foreach (Attribute attr in aGetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
//ConsoleWriteLine(Description of {}:\n{} currentProcessMainModuleFileName HelpAttrDescription);
break;
}
}
Process[] processes = ProcessGetProcesses(); //获得当前所有进程
int same = ; //相同运行实例个数
ArrayList proList = new ArrayList(); //将相同实例加入此集合中
foreach (Process pro in processes)
{
try//由于进程不同有的进程不包含Modules信息所以要用try保护
{
if (proModules != null)
if (proModulesCount > )
{
Assembly b = AssemblyLoadFrom(proMainModuleFileName);
foreach (Attribute attr in bGetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
if (HelpAttrDescriptionEquals(HelpAttrDescription))
{
same++;
proListAdd(pro);
if (same > )
{
for (int i = ; i < proListCount; i++)
{
if (((Process)(proList[i]))Id == currentProcessId )
{
ConsoleWriteLine(该进程已经启动了一个实例);
ThreadSleep();
((Process)(proList[i]))Kill();
}
}
}
}
}
}
}
}
catch
{
}
}
ConsoleReadLine();
}