Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » .NET Knowledge Base

Post new topic   Reply to topic
Read Vlaues from Registry in VB .NET
View previous topic :: View next topic  
Author Message
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Oct 29th, 2004 07:24 AM    Post subject: Read Vlaues from Registry in VB .NET Reply with quote

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!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » .NET Knowledge Base All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic