Getting Registry Values in VB.NET

Using VB6, it took a lot of code to read values from the Registration Database. Well, not anymore with with VB.NET! Take a look at the code below.

 

Imports Microsoft.Win32
Function GetDBConnect() As String
Dim pobjRegKey As RegistryKey
Dim pstrValue As String
 pobjRegKey = _
 Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\MyService\Parameters", False)
 If Not IsNothing(pobjRegKey) Then
   pstrValue = pobjRegKey.GetValue("DBConnect", vbNullString).ToString
   Return pstrValue
   Else
     Return vbNullString
  End If
End Function

 

That’s all there is to it! Basically two lines of code! This function gets a a value called “DBConnect” (database connection string) out of the Registry for a Windows Service.

 

Tip By: David McCarter

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s