How to check if registry key exists

Youll want to use the Registry object. For instance, to see if theres a key exists:
C#:
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft2");
if(key==null)
	Debug.WriteLine("Key does not exist");
else
	Debug.WriteLine("Key does exist");

To see if a value exists:
C#:
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft");
if(key!=null)
{
	if(key.GetValue("hello")==null)
		Debug.WriteLine("Value does not exist");
	else
		Debug.WriteLine("Value does exist");
}

-Nerseus
 
Code:
Dim strKey As Microsoft.Win32.RegistryKey
strKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\MSNChat\4.0")
This retreives the registry key ( just have to add as nessecary to it )
thats just an example of opening a registry key
 
Last edited by a moderator:
This should do it.

Code:
Imports Microsoft.Win32
...
dim key as RegistryKey = Registry.LocalMachine Of course you can pick anything 
instead of Local Machine

This will delete the subkey and it wont work if you have
any subkeys to that given subkey.
key.deletesubkey("path to whatever you wish to delete") 

or

This will delete that subkey and all subkeys of that subkey
key.deletesubkeytree("path to whatever you wish to delete")
 
So is this method of accessing registry keys better than using SaveSetting and GetSetting?
 
Its easy and its better. Save setting doesnt allow you to choose the location of key i think.
 
SaveSetting doesnt allow you to tell it the location as far as "HKEY_CURRENTUSER" etc..., only allows you to create a named key, in the default place (eg: hkey_currentuser\software\visual basic\YOUR PROGRAMME NAME )
 
hmmmm i cant quite get it to work mutant

i used this code u gave a while back ago where u save ur app into load up with windows

but now i duno how to delete it



heres the saving code


PHP:
Dim key As RegistryKey
                Dim subkey As RegistryKey
                key = Registry.LocalMachine
                subkey = key.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run\")
                subkey.SetValue(Application.ProductName, Application.StartupPath & "\Task Schedular.exe")

                Call subkey.Close()
                Call key.Close()
 
You didnt put the slashes in the path.

Change the code to:

Code:
Dim key As RegistryKey
Dim subkey As RegistryKey
key = Registry.LocalMachine
subkey = key.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run\putthenameofapphere")or whatever you want to call the key
subkey.SetValue(Application.ProductName, Application.StartupPath & "Task Schedular.exe")
subkey.Close()
key.Close()

I tried this and it works OK.
 
Back
Top