Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Checking a registry key exists

Checking a registry key exists

Scheduled Pinned Locked Moved C#
helpwindows-admin
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin31088
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • M Martin31088

      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

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      I am not sure that I understand this. Are you saying that if software\\MyApp\\SaveData doesn't exist, the if 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.”

      M 1 Reply Last reply
      0
      • H Henry Minute

        I am not sure that I understand this. Are you saying that if software\\MyApp\\SaveData doesn't exist, the if 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.”

        M Offline
        M Offline
        Martin31088
        wrote on last edited by
        #3

        the other way around, if that key doesn't exist run the else part to create the key.

        H 1 Reply Last reply
        0
        • M Martin31088

          the other way around, if that key doesn't exist run the else part to create the key.

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          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.”

          M 1 Reply Last reply
          0
          • H Henry Minute

            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.”

            M Offline
            M Offline
            Martin31088
            wrote on last edited by
            #5

            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 my if 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

            H 1 Reply Last reply
            0
            • M Martin31088

              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 my if 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

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              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 of OpenSubKey. 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.”

              M 1 Reply Last reply
              0
              • H Henry Minute

                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 of OpenSubKey. 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.”

                M Offline
                M Offline
                Martin31088
                wrote on last edited by
                #7

                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

                M 1 Reply Last reply
                0
                • M Martin31088

                  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

                  M Offline
                  M Offline
                  Martin31088
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups