Does Registry Key exist, if not go to form.. in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to detect if a key exists like this,

HKEY_CURRENT_USER\Software\MyCompany\MyProgram\UserName

If the key does exist, I am opening my main form;
Application.Run(new frm_Main());

Else Show.Dialog of my User Setup form.

The form launching I can handle without problem, my problem is seeing if the
key exists, and if not, then doing the redirect. I just can't seem to get
this to function properly, if the key isn't there I get a object error, if
the key is there then the app loads the main form properly.

All suggestions are greatly appreciated!
 
Hello,
Please see MSDN help for Microsoft.Win32.RegistryKey.GetSubKeyNames
method. You can check existance of registry key name from the array of
subkey names it returns.

HTH. Cheers.
Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net
 
Hi premierdata,

Does Maqsood's reply make sense to you? Beside this, we can also use
RegistryKey.OpenSubKey method to access the specified key, if null
reference is returned, this key should not exist. If you have any concern,
please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
That is the direction I have been trying to go, but if the key doesn't exist
I get a exception, then if the key does exist I pass through just fine. So I
am missing something in how the pieces go together.

I am sure I am just missing something in the syntax, but everything I have
seen referenced online deals with more elaborate structures, all I am trying
to do is see if a key exists, if not do this, else continue.
 
Hi dewright_ca,

Thanks for your feedback.

I am not sure why on your side, if the key does not existed, an exception
will throw. In fact, if you view OpenSubKey method reference in MSDN link
below:
"RegistryKey.OpenSubKey Method (String, Boolean)"
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfMicrosoftWin32Regist
ryKeyClassOpenSubKeyTopic2.asp

In "Remarks" section, it stated: "Rather than throwing an exception, a null
value is returned if the requested key does not exist." So there should not
be any exception thrown for registry key that does not existed.

Also, based on the test on my side, only a null reference will return for
registry key that does not exist. Can you provide some more information for
me to reproduce our your problem? Thanks

Addtionally, can you show us what exception your code throws out? Can you
provide the detailed call stack for this exception? Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi dewright_ca,

Is your problem resolved? If you have any concern, please feel free to tell
me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top