Posted by: R Manimaran | April 18, 2007

Description to a Windows Service using C#

//This code should be inserted into your ProjectInstaller class’ code

 public override void Install(IDictionary stateServer){   Microsoft.Win32.RegistryKey system,    currentControlSet,    services,    service,    config; //config is where I’ll be putting service-specific configuration(Parameters)   try  {   

 //Let the project installer do its job    base.Install(stateServer);     

 //Open the HKEY_LOCAL_MACHINE\SYSTEM key  system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(“System”);     //Open CurrentControlSet 

   currentControlSet = system.OpenSubKey(“CurrentControlSet”); 

   //Go to the services key 

   services = currentControlSet.OpenSubKey(“Services”);  

  //Open the key for your service, and allow writing

    service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true); 

   //Add your service’s description as a REG_SZ value named “Description”    service.SetValue(“Description”, “This is my service’s description.”);   

 //(Optional) Add some other information your service will be looking for…  

  config = service.CreateSubKey(“Parameters”); 

 } 

catch(Exception e)

  {  

  Console.WriteLine(“An exception was thrown during serviceinstallation:\n” + e.ToString());  }

} public override void Uninstall(IDictionary stateServer){   Microsoft.Win32.RegistryKey system,    currentControlSet,    services,    service;    try  {    //Drill down to the service key and open it with write permission    system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(“System”);    currentControlSet = system.OpenSubKey(“CurrentControlSet”); 

    services = currentControlSet.OpenSubKey(“Services”);  

  service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);  

  //Delete any keys you created during installation (or that your servicecreated)    service.DeleteSubKeyTree(“Parameters”);  

  //… 

 } 

catch(Exception e) 

 {   

Console.WriteLine(“Exception encountered while uninstalling service:\n”+ e.ToString());

  } 

finally  {

    //Let the project installer do its job   

 base.Uninstall(stateServer); 

 }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: