code_junkie

What's the best way to set a windows service description in .net

Nov 13th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. private System.ServiceProcess.ServiceInstaller serviceInstaller =
  2. new System.ServiceProcess.ServiceInstaller();
  3. this.serviceInstaller.Description = "Handles Service Stuff";
  4.  
  5. using System;
  6. using System.Collections;
  7. using System.ComponentModel;
  8. using System.Configuration.Install;
  9. using System.ServiceProcess;
  10. using Microsoft.Win32;
  11.  
  12. namespace SomeService
  13. {
  14. [RunInstaller(true)]
  15. public partial class ProjectInstaller : System.Configuration.Install.Installer
  16. {
  17. public ProjectInstaller()
  18. {
  19. InitializeComponent();
  20. }
  21. /// <summary>
  22. /// Overriden to get more control over service installation.
  23. /// </summary>
  24. /// <param name="stateServer"></param>
  25. public override void Install(IDictionary stateServer)
  26. {
  27. RegistryKey system;
  28.  
  29. //HKEY_LOCAL_MACHINEServicesCurrentControlSet
  30. RegistryKey currentControlSet;
  31.  
  32. //...Services
  33. RegistryKey services;
  34.  
  35. //...<Service Name>
  36. RegistryKey service;
  37.  
  38. // ...Parameters - this is where you can put service-specific configuration
  39. // Microsoft.Win32.RegistryKey config;
  40.  
  41. try
  42. {
  43. //Let the project installer do its job
  44. base.Install(stateServer);
  45.  
  46. //Open the HKEY_LOCAL_MACHINESYSTEM key
  47. system = Registry.LocalMachine.OpenSubKey("System");
  48. //Open CurrentControlSet
  49. currentControlSet = system.OpenSubKey("CurrentControlSet");
  50. //Go to the services key
  51. services = currentControlSet.OpenSubKey("Services");
  52.  
  53. //Open the key for your service, and allow writing
  54. service = services.OpenSubKey("MyService", true);
  55. //Add your service's description as a REG_SZ value named "Description"
  56. service.SetValue("Description", "A service that does so and so");
  57. //(Optional) Add some custom information your service will use...
  58. // config = service.CreateSubKey("Parameters");
  59. }
  60. catch (Exception e)
  61. {
  62.  
  63. throw new Exception(e.Message + "n" + e.StackTrace);
  64. }
  65. }
  66. }
  67. }
Add Comment
Please, Sign In to add comment