65.9K
CodeProject is changing. Read more.
Home

Registry Clean Automation for Windows Service

Apr 2, 2007

1 min read

viewsIcon

27420

downloadIcon

886

This application demonstrates to create custom subkeys in registry while installing/uninstalling windows service.

Screenshot - ServiceRegistryClean1.jpg

Introduction

Microsoft.Win32.Registry.LocalMachine

This class provides the set of standard root keys found in the registry on machines running Windows. The registry is a storage facility for Information about applications, users, and default system settings. For example, applications can use the registry for storing information that needs to be preserved after the application is closed, and access that same information when the application is reloaded. For instance, you can store color preferences, screen locations, or the size of the window. You can control this data for each user by storing the information in a different location in the registry.

The base, or root RegistryKey instances that are exposed by the Registry class delineate the basic storage mechanism for subkeys and values in the registry. All keys are read-only because the registry depends on their existence. Please go through the following steps.

Step 1:

In ProjectInstaller.vb add the following methods as override.

For Install

Public Overrides Sub Install(ByVal stateServer As IDictionary) 
    MyBase.Install(stateServer)
    Dim rkSystem, rkCurrentControlSet, rkServices, rkService, rkSubKey As RegistryKey
    rkSystem = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System") 
    rkCurrentControlSet= rkSystem.OpenSubKey("CurrentControlSet") 
    rkServices = rkCurrentControlSet.OpenSubKey("Services")
    rkService = rkServices.OpenSubKey(Me.ServiceInstaller1.ServiceName, True) 
    rkService.SetValue("Description","Test Service") 
    rkSubKey = rkService.CreateSubKey("CHILD") 
End Sub 

For Uninstall

Public Overrides Sub Uninstall(ByVal stateServer As IDictionary) 
    Dim rkSystem,rkCurrentControlSet, rkServices, rkService As RegistryKey 
    rkSystem = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System")
    rkCurrentControlSet = rkSystem.OpenSubKey("CurrentControlSet") 
    rkServices = rkCurrentControlSet.OpenSubKey("Services")
    rkService = rkServices.OpenSubKey(Me.ServiceInstaller1.ServiceName, True) 
    rkService.DeleteSubKeyTree("CHILD")
    MyBase.Uninstall(stateServer)
End Sub 

Step 2:

Build the windows service project and deploy the project. In deployment project, add primary output to install and uninstall custom actions. Refer. Image2 & Image3

Image2

Screenshot - ServiceRegistryClean2.jpg

Image3

Screenshot - ServiceRegistryClean3.jpg

Final notes

I hope that you found this article useful. If you found this article stupid, annoying, incorrect, etc. express this fact by rating the article as you see fit. However I would appreciate if you would leave a comment explaining your reasons so that I can (hopefully) learn from my mistakes.