In some application scenarios there may be instances where you need to obtain
or modify information in the Windows registry. For example, say you have a
service or another application that your Web application needs to interact with.
You may need to obtain system information for your server, or write some
information as part of your application distribution. In this article we will
show how easy it is to modify the registry within an ASP.Net application.
In this specific example, we're going to write and read values in the
registry for a registration system for our Web application. For example, you can
create a Web form that accepts a license key, this key then gets submitted via a
Web service to your license server, upon success the appropriate value or key is
then entered into the registry allowing the Web application to run fully
licensed and enabled.
Take note, you should always create a backup of your registry whenever
modifying it.
Let's introduce you to the
Microsoft.Win32 namespace, this namespace provides access to events generated by
the system, and the ability to manipulate the system registry. In this tutorial
we're working the registry aspects of the namespace.
First we'll import our namespace and create our class:
Imports Microsoft.Win32
Public Class Register
#Region "Declarations"
Private thisRegKeyLength As Integer = 8
Private thisRegNumber As String
Private thisDaysLeft As Integer
#End Region
Reading Values
In this first method we are going to read a value that was written to the
registry when the application first ran. The initial run date was entered. We
will read this value to determine if the application has been ran.
#Region "Methods"
Private Function GetDaysUsed() As Integer
Try
Dim runDate As Date
runDate = CType(GetRegValue(RegistryHive.LocalMachine, _
"SOFTWARE\MyCompany\MySoftware\", "InitDate"), Date)
Return DateDiff(DateInterval.Day, runDate, Now)
Catch ex As Exception
Return 0
End Try
End Function
This function could be accomplished a number of ways.
You could hard code your registration key within the code and then compile, or
create a Web service that takes the name in, checks it against the Web service
and returns a valid key. Once a valid key is returned it can then be written to
the registry for future checking instead of calling the Web service.
Public Sub GenerateRegistrationKey(ByVal inName As String)
'call your web service or some other function to obtain key.
End Sub
The following method is going to check the current key against the licensee name to see if it is correct. If not, you can then do a double check
against your Web service to confirm whether or not the user is valid or prompt
for a correct key.
Public Function CheckForValidRegistrationKey(ByVal inRegCode As String, _
ByVal inName As String) As Boolean
GenerateRegistrationKey(inName)
Dim temp As String
Dim length As Integer
Dim regCode As String = thisRegNumber
length = thisRegKeyLength
temp = Left$(regCode, length)
If inRegCode = temp Then
Return True
Else
Return False
End If
End Function
This
next function will check the registry to see if a flag is set called "IsRun"
within the registry, if it doesn't find the value it will write to the value to
the registry using the WriteToRegistry method. This method is called twice once
to set the flag, and another to set the initialization date for the application.
Public Function CheckIfRunBefore() As Boolean
'first check to see if there is an install date. if not create one.
If RegValue(RegistryHive.LocalMachine, "SOFTWARE\MyCompany\MySoftware\", "IsRun") = "" Then
WriteToRegistry(RegistryHive.LocalMachine, "SOFTWARE\MyCompany\MySoftware\", "IsRun", "Y")
WriteToRegistry(RegistryHive.LocalMachine, "SOFTWARE\MyCompany\MySoftware\", _
"InitDate", CType(Date.Today, String))
Return False
Else
Return True
End If
End Function
This next method is what allows our application to read values from the registry on the Web server.
Public Function GetRegValue(ByVal thisHive As RegistryHive, _
ByVal thisKey As String, ByVal thisValueName As String) As String
Dim objParent As RegistryKey
Dim objSubkey As RegistryKey
Dim sAns As String
Select Case thisHive
Case RegistryHive.ClassesRoot
objParent = Registry.ClassesRoot
Case RegistryHive.CurrentConfig
objParent = Registry.CurrentConfig
Case RegistryHive.CurrentUser
objParent = Registry.CurrentUser
Case RegistryHive.DynData
objParent = Registry.DynData
Case RegistryHive.LocalMachine
objParent = Registry.LocalMachine
Case RegistryHive.PerformanceData
objParent = Registry.PerformanceData
Case RegistryHive.Users
objParent = Registry.Users
End Select
Try
objSubkey = objParent.OpenSubKey(thisKey)
'Check to see if the object is in the registry.
If Not objSubkey Is Nothing Then
sAns = (objSubkey.GetValue(thisValueName))
End If
Catch ex As Exception
End Try
Return sAns
End Function
Writing Values
This method is going to write to the registry, previously we entered in some
values such as the registration information, flag when the program was first ran,
and a date for the application startup.
Public Function WriteToRegistry(ByVal thisParentKeyHive As RegistryHive, _
ByVal thisSubKeyName As String, ByVal thisValueName As String, ByVal thisValue As Object) As Boolean
Dim objSubKey As RegistryKey
Dim sException As String
Dim objParentKey As RegistryKey
Dim bAns As Boolean
Try
Select Case thisParentKeyHive
Case RegistryHive.ClassesRoot
objParentKey = Registry.ClassesRoot
Case RegistryHive.CurrentConfig
objParentKey = Registry.CurrentConfig
Case RegistryHive.CurrentUser
objParentKey = Registry.CurrentUser
Case RegistryHive.DynData
objParentKey = Registry.DynData
Case RegistryHive.LocalMachine
objParentKey = Registry.LocalMachine
Case RegistryHive.PerformanceData
objParentKey = Registry.PerformanceData
Case RegistryHive.Users
objParentKey = Registry.Users
End Select
'Open the registry key.
objSubKey = objParentKey.OpenSubKey(thisSubKeyName, True)
'We'll create it.
If objSubKey Is Nothing Then
objSubKey = objParentKey.CreateSubKey(thisSubKeyName)
End If
objSubKey.SetValue(ValueName, thisValue)
bAns = True
Catch ex As Exception
bAns = False
End Try
Return True
End Function
#End Region
End Class
This code is only provided as an example of how to write to the registry
using VB.Net. Keep in mind other issues may arise where a registration system for
your application will not work. The process will need to have permissions to
access the registry in order to read and write to it. In addition, if your
application is load balanced then each machine will have most likely end up with
different values in the registry.
There are plenty of other options for storing data that are preferred over
this method like in a database, or inside the web.config, but if you need to
read existing values or even hide values using the registry may be an option.