lothos12345 Posted November 26, 2003 Posted November 26, 2003 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. Quote
*Experts* mutant Posted November 26, 2003 *Experts* Posted November 26, 2003 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. Quote
fadi Posted November 27, 2003 Posted November 27, 2003 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 Quote
laredo512 Posted September 28, 2004 Posted September 28, 2004 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 ? Quote Laredo512 * using VS.NET Pro 2003 *
laredo512 Posted September 28, 2004 Posted September 28, 2004 Never mind.... found it. Great piece of code! :) Quote Laredo512 * using VS.NET Pro 2003 *
TheWizardofInt Posted June 3, 2005 Posted June 3, 2005 Excellent - worked exactly as you say Thanks, so much! Quote Read the Fovean Chronicles Because you just can't spend your whole day programming!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.