Registry Editing

lothos12345

Well-known member
Joined
May 2, 2002
Messages
294
Location
Texas
I want to write a program that edits a particuliar registry value every time the button is clicked. I have no ideal how to accomplish this task. I want to edit it using VB.NET any help given would be greatly appreiciated. The reason being is that there is a program that I have that used a partcular registry value as a default everytime it is opened. If I edit that registry value the default will change and it will save me a great deal of time with some of my tasks.
 
Look into the following classes:
Microsoft.Win32.Registry
Microsoft.Win32.RegistryKey
To read, or set registry settings look into the OpenSubKey, GetValue, SetValue methods of the Registry class.
 
take this code to get,update,delete registery values
Gets a value data for a specific ValueName in the registry
Public Function GetFromRegistry(ByVal KeyPath As String, ByVal ValueName As String) As String
Dim regKey As RegistryKey
Dim strValueData As String
Try
Open the key with write access
regKey = Registry.LocalMachine.OpenSubKey(KeyPath, False)
If regKey Is Nothing Then
Exit Function
End If

Get the data of [ValueName] Value, if no data is found return ""
strValueData = CStr(regKey.GetValue(ValueName, ""))
Return strValueData
Catch ex As Exception
Return ""
End Try
End Function

Saves a value name with its data and parent key in the registry
Public Sub SaveToRegistry(ByVal KeyPath As String, ByVal ValueName As String, ByVal ValueData As Object)
Dim regKey As RegistryKey
Try
Open the key with write access
regKey = Registry.LocalMachine.OpenSubKey(KeyPath, True)

If not found, create the key
If regKey Is Nothing Then
Create a key in HKEY_LOCAL_MACHINE\[Path] with a name of [ValueName]
regKey = Registry.LocalMachine.CreateSubKey(KeyPath)
End If

Set a new value
regKey.SetValue(ValueName, ValueData)

Catch ex As Exception
Throw New System.Exception("Error saving to registry")
End Try
End Sub

Delete a value for a parent key in the registry
Public Sub DeleteFromRegistry(ByVal KeyPath As String, ByVal ValueName As String)
Dim regKey As RegistryKey
Try
Open the key with write access
regKey = Registry.LocalMachine.OpenSubKey(KeyPath, True)
regKey.DeleteValue(ValueName)
Catch ex As Exception
End Try
End Sub
 
fadi said:
take this code to get,update,delete registery values
Gets a value data for a specific ValueName in the registry
Public Function GetFromRegistry(ByVal KeyPath As String, ByVal ValueName As String) As String
Dim regKey As RegistryKey
Dim strValueData As String
Try
Open the key with write access
regKey = Registry.LocalMachine.OpenSubKey(KeyPath, False)
If regKey Is Nothing Then
Exit Function
End If

Get the data of [ValueName] Value, if no data is found return ""
strValueData = CStr(regKey.GetValue(ValueName, ""))
Return strValueData
Catch ex As Exception
Return ""
End Try
End Function

Saves a value name with its data and parent key in the registry
Public Sub SaveToRegistry(ByVal KeyPath As String, ByVal ValueName As String, ByVal ValueData As Object)
Dim regKey As RegistryKey
Try
Open the key with write access
regKey = Registry.LocalMachine.OpenSubKey(KeyPath, True)

If not found, create the key
If regKey Is Nothing Then
Create a key in HKEY_LOCAL_MACHINE\[Path] with a name of [ValueName]
regKey = Registry.LocalMachine.CreateSubKey(KeyPath)
End If

Set a new value
regKey.SetValue(ValueName, ValueData)

Catch ex As Exception
Throw New System.Exception("Error saving to registry")
End Try
End Sub

Delete a value for a parent key in the registry
Public Sub DeleteFromRegistry(ByVal KeyPath As String, ByVal ValueName As String)
Dim regKey As RegistryKey
Try
Open the key with write access
regKey = Registry.LocalMachine.OpenSubKey(KeyPath, True)
regKey.DeleteValue(ValueName)
Catch ex As Exception
End Try
End Sub
I have tried this code and keep getting a System Null Reference Exception... Object not set to a reference of an object

How would I properly apply your code in a sub routine ?
 
Back
Top