Checking a registry key exists
-
Hi everyone, I'm writing a little application and I've hit a bit of a problem. part of the program uses the registry to store certain things. I have no problem writing or reading a key. But I need to be able to check the key exists, if it does do this, if not write the key. Here's the code I'm currently using:
using Microsoft.Win32; RegistryKey MyRegKey = Registry.LocalMachine.OpenSubKey("software\\MyApp\\SaveData"); if (MyRegKey != null) { string OptCurrency = MyRegKey .GetValue("Currency").ToString(); } else { RegistryKey CreateMyRegKey = Registry.LocalMachine.CreateSubKey("software\\MyApp\\SaveData"); CreateMyRegKey .SetValue("Currency", 0); string OptCurrency = "0"; } MyRegKey.Close();
This is fine if the key exists, but doesn't recognise if it does not, and runs through the if rather than the else. Any help would be greatly appreciated Many thanks Martin -
Hi everyone, I'm writing a little application and I've hit a bit of a problem. part of the program uses the registry to store certain things. I have no problem writing or reading a key. But I need to be able to check the key exists, if it does do this, if not write the key. Here's the code I'm currently using:
using Microsoft.Win32; RegistryKey MyRegKey = Registry.LocalMachine.OpenSubKey("software\\MyApp\\SaveData"); if (MyRegKey != null) { string OptCurrency = MyRegKey .GetValue("Currency").ToString(); } else { RegistryKey CreateMyRegKey = Registry.LocalMachine.CreateSubKey("software\\MyApp\\SaveData"); CreateMyRegKey .SetValue("Currency", 0); string OptCurrency = "0"; } MyRegKey.Close();
This is fine if the key exists, but doesn't recognise if it does not, and runs through the if rather than the else. Any help would be greatly appreciated Many thanks MartinI am not sure that I understand this. Are you saying that if
software\\MyApp\\SaveData
doesn't exist, theif
part of your code executes? BTW: if you surround your code snippet with <pre></pre>, rather than <code></code>, it will allow you to retain the formatting and make it easier for others to read. (use the 'code block' widget, not the 'inline code' one).Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I am not sure that I understand this. Are you saying that if
software\\MyApp\\SaveData
doesn't exist, theif
part of your code executes? BTW: if you surround your code snippet with <pre></pre>, rather than <code></code>, it will allow you to retain the formatting and make it easier for others to read. (use the 'code block' widget, not the 'inline code' one).Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
the other way around, if that key doesn't exist run the
else
part to create the key. -
the other way around, if that key doesn't exist run the
else
part to create the key.I realise that is what you intend to happen. But your OP indicated the reverse.
This is fine if the key exists, but doesn't recognise if it does not, and runs through the if rather than the else.
indicates to me that the
if
block gets executed regardless of whether the key exists or not. Perhaps you might try rewording your question, to differentiate what you want to happen from what currently happens.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I realise that is what you intend to happen. But your OP indicated the reverse.
This is fine if the key exists, but doesn't recognise if it does not, and runs through the if rather than the else.
indicates to me that the
if
block gets executed regardless of whether the key exists or not. Perhaps you might try rewording your question, to differentiate what you want to happen from what currently happens.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Currently It does run through the
if
regardless of whether the key exists or not. The way the code is at the moment (some changes have been made to the previous post) the key will get created if it does not exist. I can then make a change in the options of my application and apply them which saves the change to their respective keys. Then when I close the program and re-open it, the keys are reset back to the default because the code is running through the wrong part of myif
statement and overwriting the key. Intended I would like it to be able to tell if the key exists, if it does not, create it, if it does, copy the value of each key into the defined global variables. So that program can use these values wherever necessary. I'll post the altered code if you need. I hope this clears it up. Thanks -
Currently It does run through the
if
regardless of whether the key exists or not. The way the code is at the moment (some changes have been made to the previous post) the key will get created if it does not exist. I can then make a change in the options of my application and apply them which saves the change to their respective keys. Then when I close the program and re-open it, the keys are reset back to the default because the code is running through the wrong part of myif
statement and overwriting the key. Intended I would like it to be able to tell if the key exists, if it does not, create it, if it does, copy the value of each key into the defined global variables. So that program can use these values wherever necessary. I'll post the altered code if you need. I hope this clears it up. ThanksI do not use the registry any more, or very rarely, so I have had to go back and look at some of my old code. The only obviously different things that I used to do is as below.
string myAppSaveDataSubKey = @"software\MyApp\SaveData"; <=== use a variable. saves having to keep typing it in and possible typos
using (RegistryKey MyRegKey = Registry.LocalMachine.OpenSubKey(myAppSaveDataSubKey)) <== using 'using' saves having to remember 'close'
{
if (MyRegKey == null) <=========== Check == null instead of != null
{
RegistryKey CreateMyRegKey = Registry.LocalMachine.CreateSubKey(myAppSaveDataSubKey);
CreateMyRegKey.SetValue("Currency", 0);
string OptCurrency = "0";
}
else
{
string OptCurrency = MyRegKey.GetValue("Currency").ToString(); <== you need to load OptCurrency anyway so don't 'else' it
}
}which used to work OK for me. The only other thing you might consider is to use
CreateSubKey
instead ofOpenSubKey
. This has the advantage of creating the subkey if it doesn't exist, and simply opening it for writing if it does. Hope some of this is useful. :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I do not use the registry any more, or very rarely, so I have had to go back and look at some of my old code. The only obviously different things that I used to do is as below.
string myAppSaveDataSubKey = @"software\MyApp\SaveData"; <=== use a variable. saves having to keep typing it in and possible typos
using (RegistryKey MyRegKey = Registry.LocalMachine.OpenSubKey(myAppSaveDataSubKey)) <== using 'using' saves having to remember 'close'
{
if (MyRegKey == null) <=========== Check == null instead of != null
{
RegistryKey CreateMyRegKey = Registry.LocalMachine.CreateSubKey(myAppSaveDataSubKey);
CreateMyRegKey.SetValue("Currency", 0);
string OptCurrency = "0";
}
else
{
string OptCurrency = MyRegKey.GetValue("Currency").ToString(); <== you need to load OptCurrency anyway so don't 'else' it
}
}which used to work OK for me. The only other thing you might consider is to use
CreateSubKey
instead ofOpenSubKey
. This has the advantage of creating the subkey if it doesn't exist, and simply opening it for writing if it does. Hope some of this is useful. :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Thanks for the suggestion, I'll have to look at it tomorrow. I'll let you know how I get on. Thanks alot for your support Martin
-
Thanks for the suggestion, I'll have to look at it tomorrow. I'll let you know how I get on. Thanks alot for your support Martin
I've finally sorted it! I used some suggestions from your last post which helped. But I decided to tackle it from a different angle, eventually i came up with doing a
valueCount
If (Count != 3) { create the keys } then copy values of each. Works perfectly. Thanks alot for your support Henry Best regards Martin