Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.
  • *Experts*
Posted

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.

Posted

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

  • 10 months later...
Posted
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 ?

Laredo512

* using VS.NET Pro 2003 *

  • 8 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...