Question Get Path from the registry key

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
I’m using VB.NET application.

[HKEY_LOCAL_MACHINE\SOFTWARE\QuickProjects.\QSUpdate]
"InstallDir"="C:\Program Files\ QuickProjects.\QSUpdate"


Can anyone tell me how to read the full path from registery key?

Sometimes the path might be “C:\Projects\QuickProjects.\QSUpdate”, sometimes it might be “T1\root\Program Files\QuickProjects.\QSUpdate”.

I have no idea knowing what it is… That’s why i need to get it from the registry. Whatever path is in that registry entry is the path I need.

I just want my VB.NET program to determine the full path from the registery key.

I know that the registery key is: HKEY_LOCAL_MACHINE\SOFTWARE\QuickProjects.\QSUpdate

What I meant by "full path" is the location of where the
registry key been installed. For example: "C:\Program
Files\ QuickProjects.\QSUpdate"

If you know how to find out the path from registry key, please let me know.

Thanks in advance.
 
Registry and RegistryKey classes in Microsoft.Win32 namespace have what you need. Should be like this:
VB.NET:
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\QuickProjects.\QSUpdate", False)
Dim value As String = CStr(key.GetValue("InstallDir"))
key.Close()
 
Thanks for your reply.

i just tried the codes as you said
VB.NET:
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\QuickProjects.\\QSUpdate", False)
Dim value As String = CType(key.GetValue("InstallDir"), String)
key.Close()

but an error message occurs
An unhandled exception of type 'system.NullReferenceException' occured in UpdateProgram.exe

Additional information : Object reference not set to an instance of an object.

And i tried this code too,
VB.NET:
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\\QuickProjects.\QSUpdate", False)
Dim value As String = CType(key.GetValue("InstallDir"), String)
key.Close()

but value is nothing.

using regedit.exe i double check and Software\\QuickProjects.\QSUpdate exist.

if you know how to find the full path from registry key, please help me.

Thanks in advance.
 
The 'key' RegistryKey is the only thing that can be NullReference there, so that means the path is different than you think. You could open the Software key and look at the GetSubKeyNames etc, go step by step investigating and you will discover the truth.
 
This is the first time i'm working in Registry Key.

the code i used to create SubKey is

Code:
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\\QuickProjects.\\QSUpdate", True)
If Not NewKey Is Nothing Then
'nothing
Else
Dim Mykey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software", True)
'Add one more sub key
Dim NewKey1 As RegistryKey = Mykey.CreateSubKey("QuickProjects.\\QSUpdate")
End If

then i tried this code,
VB.NET:
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\\QuickProjects.\\QSUpdate", False)
Dim value As String = CType(key.GetValue("InstallDir"), String)
key.Close()
value is nothing.

i tried this code too,
VB.NET:
Dim OurKey As RegistryKey = Registry.LocalMachine
OurKey = OurKey.OpenSubKey("Software\\Quickservice Projects.\\QSUpdate", True)
Dim value As String = CType(OurKey.GetValue("InstallDir"), String)
'The first example shows it using a foreach loop to display each subkeyname 
Dim Keyname As String
For Each Keyname In OurKey.GetSubKeyNames()
     MessageBox.Show(Keyname)
Next Keyname
value is nothing.

Anything i'm doing wrong or anything i'm missing to do. is it the way i need to use registry key to find the path.

if you know how to find the full path from registry key, please help me.

Thanks in advance.
 
As you said i tried this,

VB.NET:
Dim path As String
        Dim NewKey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\QuickProjects.\QSUpdate", True)
        
	If Not NewKey Is Nothing Then
             path= CType(NewKey.GetValue("InstallDir"), String)
            MessageBox.Show(path)
        Else
            Dim Mykey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software", True)
            'Add one more sub key
            Dim NewKey1 As RegistryKey = Mykey.CreateSubKey("QuickProjects.\QSUpdate")
	    'Set value of sub key
            NewKey1.SetValue("InstallDir", "C:\Program Files\QuickProjects.\QSUpdate")
        End If
MessageBox.Show(path)


its working.

Thank you so much for your help. Thanks a lot.
 
Great you figured your way, but what you're doing is not what you asked to do, perhaps it is a linguistic thing. Also remember to close all keys you're opening.
 
Back
Top