Click here to Skip to main content
15,885,817 members
Articles / Programming Languages / Visual Basic
Article

Registry Clean Automation for Windows Service

Rate me:
Please Sign up or sign in to vote.
1.73/5 (6 votes)
2 Apr 20071 min read 27.3K   886   12  
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer iInterchange Systems
India India
My main goal as a developer is to improve the way software is designed, and how it interacts with the user.
I have extensive knowledge of the .NET Framework, SQL Server, Javascript. I specialize in working with VS 2005,AJAX,XSLT, Windows Services, COM+ and SQL 2005.

Comments and Discussions

 
-- There are no messages in this forum --