知识大全 C#中让程序只运行一个实例的操作方法

Posted

篇首语:我们要像海绵一样吸收有用的知识。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C#中让程序只运行一个实例的操作方法相关的知识,希望对你有一定的参考价值。

C#中让程序只运行一个实例的操作方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

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

  在这里 我有两个解决方法

  方法一 我把AssemblyInfo cs里的[assembly AssemblyFileVersion( )]改为[assembly AssemblyFileVersion( )] 然后利用该信息进行判断

  代码如下

  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_

  

  class Program

  

  static void Main(string[] args)

  

  Process[] processes = Process GetProcesses(); //获得当前所有进程

  Process currentProcess = Process GetCurrentProcess(); //获取当前正在运行进程

  ProcessModule currentPM = currentProcess Modules[ ];

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

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

  foreach (Process p in processes)

  

  try//由于进程不同 有的进程不包含Modules信息 所以要用try保护

  

  if (p Modules != null)

  if (p Modules Count > )

  

  System Diagnostics ProcessModule pm = p Modules[ ];

  if (pm FileVersionInfo FileVersion Equals(currentPM FileVersionInfo FileVersion))

  

  same++;

  proList Add(p);

  

  if (same > )

  

  same++;

  proList Add(p);

  if (same > )

  

  for (int i = ; i < proList Count; i++)

  if (((Process)(proList[i])) Id == currentProcess Id)

  

  Console WriteLine( 该进程已经启动了一个实例 );

  Thread Sleep( );

  ((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 )]

  public class HelpAttribute : Attributepublic HelpAttribute(String Description_in)this description = Description_in;

  protected String description;

  public String Descriptiongetreturn this description;class Programstatic void Main(string[] args)HelpAttribute HelpAttr = null;HelpAttribute HelpAttr = null;Process currentProcess = Process GetCurrentProcess(); //获取当前正在运行进程Assembly a = Assembly LoadFrom(currentProcess MainModule FileName);foreach (Attribute attr in a GetCustomAttributes(true))HelpAttr = attr as HelpAttribute;if (null != HelpAttr )//Console WriteLine( Description of :\\n currentProcess MainModule FileName HelpAttr Description);break;Process[] processes = Process GetProcesses(); //获得当前所有进程int same = ; //相同运行实例个数ArrayList proList = new ArrayList(); //将相同实例加入此集合中foreach (Process pro in processes)try//由于进程不同 有的进程不包含Modules信息 所以要用try保护if (pro Modules != null)if (pro Modules Count > )Assembly b = Assembly LoadFrom(pro MainModule FileName);foreach (Attribute attr in b GetCustomAttributes(true))HelpAttr = attr as HelpAttribute;if (null != HelpAttr )if (HelpAttr Description Equals(HelpAttr Description))same++;proList Add(pro);if (same > )for (int i = ; i < proList Count; i++)if (((Process)(proList[i])) Id == currentProcess Id )Console WriteLine( 该进程已经启动了一个实例 );

  Thread Sleep( );

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

cha138/Article/program/ASP/201311/21798

相关参考