Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
Posted: Oct 29th, 2004 07:24 AM Post subject: Read Vlaues from Registry in VB .NET |
|
|
This function accepts a root key, a subkey, and the name part of a name/value registry value, and returns the value. You can also get error information if the call fails. See the code's comments for a demo on how to use the function.
Declarations: Imports Microsoft.Win32
[vb:1:dbd874cfab] Public Function RegValue(ByVal Hive As RegistryHive, _
ByVal Key As String, ByVal ValueName As String, _
OptionalByRef ErrInfo As String = "") As String
'DEMO USAGE
'Dim sAns As String
'Dim sErr As String = ""
'sAns = RegValue(RegistryHive.LocalMachine, _
' "SOFTWARE\Microsoft\Windows\CurrentVersion", _
' "ProgramFilesDir", sErr)
'If sAns <> "" Then
' Debug.WriteLine("Value = " & sAns)
'Else
' Debug.WriteLine("This error occurred: " & sErr)
'End If
Dim objParent As RegistryKey
Dim objSubkey As RegistryKey
Dim sAns As String
Select Case Hive
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(Key)
'if can't be found, object is not initialized
If Not objSubkey Is Nothing Then
sAns = (objSubkey.GetValue(ValueName))
End If
Catch ex As Exception
ErrInfo = ex.Message
Finally
'if no error but value is empty, populate errinfo
If ErrInfo = "" And sAns = "" Then
ErrInfo = _
"No value found for requested registry key"
End If
End Try
Return sAns
End Function[/vb:1:dbd874cfab]
Hope this helps! _________________ Code Snippets, Tutorials, Utilities, Controls
Low cost Web Hosting
Hosting starts at as low as $4 per year!
Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags! |
|