Click here to Skip to main content
15,904,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to read the value in (Default)from Registry. This is under "HKEY_LOCAL_MACHINE\"

I'hv tried like different ways like below ,but its returning NULL.
C#
RegistryKey key = Registry.LocalMachine;
key.OpenSubKey(@"\SOFTWARE\Classes\ADs\Clsid");

string InstallPath = string.Empty;
InstallPath = (string)key.GetValue(".");                    ---- Way1
InstallPath = (string)key.GetValue("");                     ---- Way2
InstallPath = (string)key.GetValue("(Default)",string.Empty);---- Way3
InstallPath = key.GetValue(".",null).ToString();       --- Way4 // error cann't assign to an object


string InstallPath = (string)key.GetValue("");               ---- Way5

please guide, how can i get the value of (Default)

Thanks in advance:)
Posted
Updated 22-Jan-13 2:23am
v2
Comments
Sandeep Mewara 22-Jan-13 9:18am    
Ia there any key named (Default)? I doubt it.

According to the MSDN documentation[^] you can get this value by specifiying an empty string (as in Way 2 above), and from personal experience I know this works. You need to check that the items you are trying to access actually exist, and also that you have the requisite privilege level to read them.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 12:20pm    
Correct, a 5.
—SA
A little breakdown:
C#
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\ADs\Clsid");

2 things here:
1) You can combine the first two lines into one. If not, you must assign the key to itself, i.e.
C#
key = key.OpenSubKey(...

2) Remove the first backslash (before "SOFTWARE") - otherwise you'll get an "Object reference not set to an instance of an object" error

Now, you can use this:
C#
InstallPath = (string)key.GetValue(null);

3) Finally, don't forget to close the key when you have finished with it!
 
Share this answer
 
Comments
litu kumar 22-Jan-13 10:53am    
Thank you very much .I able to read the value at (Default) :)
The so called default value is really an unnamed value but RegEdit always displays it as "(Default)".

The presence of an assigned, i.e. non null, default value can be detected by looking for an empty string, i.e. "", in the array returned by RegistryKey.GetValueNames().

The value may be read with either RegistryKey.GetValue("") or RegistryKey.GetValue(null). If unassigned then the value will be null and RegEdit will display "(value not set)".

Alan.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 12:21pm    
Correct, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900