Tuesday, November 28, 2006

Reading and Editing Registry Keys

First, you need to import Microsoft.win32.

Sample code for edit the registry

Public Shared Sub UpdateiPhysicalFlagTo0()
Dim regConnectionKey As RegistryKey
regConnectionKey = Registry.LocalMachine.OpenSubKey("key here", True)
If (regConnectionKey Is Nothing) Then
regConnectionKey.Close()
Else
Try
regConnectionKey.SetValue("FirstTimeTrain", "0")
regConnectionKey.Close()
Catch er As Exception
Throw New Exception(er.Message)
End Try
End If
End Sub

Sample code for reading registry

Public Shared Function GetRegistryFirstTimeTraining() As Boolean
Dim bool As Boolean = False
Dim regConnectionKey As RegistryKey
regConnectionKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\SQLView\\DMSConnection")
If (regConnectionKey Is Nothing) Then
Throw New Exception("GetRegistryFirstTimeTraining() : The registry key is not found")
Else
Try
Dim vr As String
vr = regConnectionKey.GetValue("FirstTimeTrain")
regConnectionKey.Close()
If (vr.ToString() = "1") Then
bool = True
End If
Return bool
Catch er As Exception
Throw New Exception(er.Message)
End Try
End If
End Function

No comments:

Post a Comment