給windows服務添加描述
2024-07-21 02:16:05
供稿:網友
 
  最近寫了個windows服務(windows services),安裝了以后,覺得和已有的windows服務不一樣。為什么?我的缺少描述,中間一欄是空的。
  再看.net的servicebase類沒有添加描述的屬性。
  public class projectinstaller : system.configuration.install.installer中也沒有什么屬性來添加。從網搜了后才知道要重載projectinstaller 的install和uninstall虛方法。其實重寫這些虛方法就是為了在注冊表相應服務中加一個鍵值"description",其值填為相應的描述就可以了。
 public override void install(idictionary stateserver)
  {
   microsoft.win32.registrykey system,
       service,
       config; 
   try
   {
    //let the project installer do its job
    base.install(stateserver);
    system = microsoft.win32.registry.localmachine.opensubkey("system").opensubkey("currentcontrolset").opensubkey("services");
    service = system.opensubkey(this.serviceinstaller1.servicename, true);
    service.setvalue("description", "服務描述");
   //添加額外的鍵
    config = service.createsubkey("additionalinformation");
   }
   catch(exception e)
   {  
   }
  }
  public override void uninstall(idictionary stateserver)
  {
   microsoft.win32.registrykey system,
    currentcontrolset,
    services,
    service;
   try
   {
        system = microsoft.win32.registry.localmachine.opensubkey("system");
    currentcontrolset = system.opensubkey("currentcontrolset");
    services = currentcontrolset.opensubkey("services");
    service = services.opensubkey(this.serviceinstaller1.servicename, true);
       //刪除額外的鍵
    service.deletesubkeytree("additionalinformation");
    //...
   }
   catch(exception e)
   {
      }
   finally
   {
    base.uninstall(stateserver);
   }
  }
注意這些代碼是在projectinstaller 文件中的。
也許有更好的辦法,望大家指教。