Need Example; Test to see if regkey/value exists

  • Thread starter Thread starter Jeff Foster
  • Start date Start date
I've been playing with the RegistryKey class in the Microsoft.Win32
namespace.
I can do various things but I can't seem to get the syntax correct to see if
a value or a key exsists.

I know I need to use the GetValue & OpenSubKey methods

I've been trying variations on these methods with no luck.
Can someone give me a working example?

This is my latest (failed) attempt

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\0000000";

using (RegistryKey Key = Registry.CurrentUser)

{
if (Key.OpenSubKey(sKey)= null)
{
}
}
 
Yogi_Bear_79 said:
I've been playing with the RegistryKey class in the Microsoft.Win32
namespace.
I can do various things but I can't seem to get the syntax correct to see if
a value or a key exsists.

I know I need to use the GetValue & OpenSubKey methods

I've been trying variations on these methods with no luck.
Can someone give me a working example?

This is my latest (failed) attempt

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\0000000";

using (RegistryKey Key = Registry.CurrentUser)

{
if (Key.OpenSubKey(sKey)= null)
{
}
}

For one thing, your "using" block is guarding the wrong thing there -
you don't really want to dispose of the Registry.CurrentUser key, as
that's always open. You want something like:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(sKey))
{
if (key==null)
{
....
}
}

Note the == rather than = in the condition.
 
Well, it doesn't retunr an error, however it returns a false postive.

I'm lookin in the
HKCU\Software\Microsoft\Office\Outlook\OMI Account Manager\Accounts"
Key for subkeys that consist of 7 zeros + 01~99

In the below example it should return "Exsits" but no matter if the key is
there or not I always get Doesn't Exsist

private void button1_Click(object sender, System.EventArgs e)

{

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\00000001";

string sPort = "LDAP Port";

string sConnection = "LDAP Secure Connection";

int x;


using (RegistryKey Key = Registry.CurrentUser)

{

if (Key.OpenSubKey(sKey)==null)

{

MessageBox.Show("Doesn't Exists");

}

else

{

MessageBox.Show("Exists");

}

}
 
Jon,

Getting the same results.
I'm getting the Doesn't Exist message box on keys htat exist and ones
that don't exist

private void button1_Click(object sender, System.EventArgs e)

{

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\00000000";

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(sKey))

{

if (key==null)

{

MessageBox.Show("Doesn't Exist");

}

else

{

MessageBox.Show("Exists");

}

}
 
Yogi_Bear_79 said:
Getting the same results.
I'm getting the Doesn't Exist message box on keys htat exist and ones
that don't exist

private void button1_Click(object sender, System.EventArgs e)

{

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\00000000";

The "\" at the start is the cause of your problems - get rid of it and
everything should be fine.
 
Back
Top