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
  1. Home
  2. General Programming
  3. C#
  4. Changing Windows registry with C#

Changing Windows registry with C#

Scheduled Pinned Locked Moved C#
csharpvisual-studiowindows-adminhelpquestion
22 Posts 4 Posters 2 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.
  • I Ismael_1999

    Hi. I'm working with Visual Studio on a application that changes a windows registry. There is a small forms that requests an address in a text box. The contents of the text box wil be writen is the registry. I need the key to be LocalMachine and here is a snippet of this code:

        private void button1\_Click(object sender, EventArgs e)
        {
            RegistryKey chaveBase, chave;
            chaveBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            chave = chaveBase.CreateSubKey("Software\\\\Antares", true);
            chave.SetValue("Endereço", textBox1.Text); 
        }
    

    The problem is that I receive the message: "System.UnauthorizedAccessException: 'Access to the registry key 'HKEY_LOCAL_MACHINE\Software\Antares' is denied". If I change LocalMachine for CurrentUser, it works fine. What should I do to access LocalMachine?

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #3

    To add to what Dave has - rightly - said ... The problem is due to a layer of security added after the registry became too bloated and vulnerable: it was filled with rubbish, and fully available to code to view or modify. In more modern OSes (think Vista and later) it became much harder to access the registry, and Admin access was required for most of the content. This included the entire LocalMachine branch. Do not expect it to get easier to access, if anything it will get harder as storing app settings there is now discouraged to reduce the bloat. So unless you have a very, very good reason to use the registry you shouldn't do it: use an app config file, or store data in a User data folder instead. It's easier to do, and a lot more future proof! This may help: SWhere should I store my data?[^]

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    I 1 Reply Last reply
    0
    • D Dave Kreskowiak

      This is because you have to be running the code as an administrator to make any changes to anything under HKEY_LOCAL_MACHINE. Hold down a Shift key, right-click your executable and click "Run as administrator". You'll, of course, have to supply admin credentials, or if you're already using an admin account, you'll have to OK using the admin part of your account to run the app. Then it'll work.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      I Offline
      I Offline
      Ismael_1999
      wrote on last edited by
      #4

      Thank you Dave, but code should run without user intervention.

      D 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        To add to what Dave has - rightly - said ... The problem is due to a layer of security added after the registry became too bloated and vulnerable: it was filled with rubbish, and fully available to code to view or modify. In more modern OSes (think Vista and later) it became much harder to access the registry, and Admin access was required for most of the content. This included the entire LocalMachine branch. Do not expect it to get easier to access, if anything it will get harder as storing app settings there is now discouraged to reduce the bloat. So unless you have a very, very good reason to use the registry you shouldn't do it: use an app config file, or store data in a User data folder instead. It's easier to do, and a lot more future proof! This may help: SWhere should I store my data?[^]

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        I Offline
        I Offline
        Ismael_1999
        wrote on last edited by
        #5

        Hi, OriginalGriff. I read your article which you suggested and I'll probably change my storage place. On question: your article is 10 years old. Are those information still valid? Thank you.

        OriginalGriffO 1 Reply Last reply
        0
        • I Ismael_1999

          Hi, OriginalGriff. I read your article which you suggested and I'll probably change my storage place. On question: your article is 10 years old. Are those information still valid? Thank you.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #6

          Yes - the locations are still the same!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          I 1 Reply Last reply
          0
          • I Ismael_1999

            Thank you Dave, but code should run without user intervention.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #7

            Yeah, ... and? You said you created a WINDOWS FORMS APP, which is an app that requires user interaction. If this is a test app for what you're really writing, like a Windows Service app, then you can run that code under the Local System account, which will have admin permissions.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Yes - the locations are still the same!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              I Offline
              I Offline
              Ismael_1999
              wrote on last edited by
              #8

              Hi, OriginalGriff. I used the code you wrote in your article, but I received an error message. First see a snipet of my code:

                  public static Guid AppGuid
                  {
                      get
                      {
                          Assembly asm = Assembly.GetEntryAssembly();
                          object\[\] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true));
                          return new Guid((attr\[0\] as GuidAttribute).Value);
                      }
                  }
              

              This is in fact your code. The error is on the line of the return: "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" Do you know what causes this error? Thanks.

              D 1 Reply Last reply
              0
              • I Ismael_1999

                Hi, OriginalGriff. I used the code you wrote in your article, but I received an error message. First see a snipet of my code:

                    public static Guid AppGuid
                    {
                        get
                        {
                            Assembly asm = Assembly.GetEntryAssembly();
                            object\[\] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true));
                            return new Guid((attr\[0\] as GuidAttribute).Value);
                        }
                    }
                

                This is in fact your code. The error is on the line of the return: "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" Do you know what causes this error? Thanks.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #9

                That's because your app doesn't have a GuidAttribute. Go into your project Properties, click on the Application tab, then Assembly Information button. You'll find a GUID box in the dialog that pops up. That box is empty. That's where you have to put your GUID.

                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                Dave Kreskowiak

                OriginalGriffO 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  That's because your app doesn't have a GuidAttribute. Go into your project Properties, click on the Application tab, then Assembly Information button. You'll find a GUID box in the dialog that pops up. That box is empty. That's where you have to put your GUID.

                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                  Dave Kreskowiak

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #10

                  Normally, that is filled in by VS when it creates the project template. Dunno what he used to do that ... :laugh:

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  D 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Normally, that is filled in by VS when it creates the project template. Dunno what he used to do that ... :laugh:

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #11

                    Under the .NET Framework projects (4.8 at least), it's filled in by the Windows Forms template, but not the WPF template. I haven't tried any of the others. On .NET Core projects, the "Assembly Information" option doesn't even exist. The assembly info is automatically generated at compile time, but is controllable in the .csproj file. I haven't spent very much time looking at this because I don't expose much to COM, so YMMV.

                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                    Dave Kreskowiak

                    I 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Under the .NET Framework projects (4.8 at least), it's filled in by the Windows Forms template, but not the WPF template. I haven't tried any of the others. On .NET Core projects, the "Assembly Information" option doesn't even exist. The assembly info is automatically generated at compile time, but is controllable in the .csproj file. I haven't spent very much time looking at this because I don't expose much to COM, so YMMV.

                      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                      Dave Kreskowiak

                      I Offline
                      I Offline
                      Ismael_1999
                      wrote on last edited by
                      #12

                      Hi, Dave. I tried here, but didn't get it. I use VS Community 2022 in portuguese. In my superior menu there is "Tools" and then "Create GUID". Next appears a small form with many options (1 - IMPLEMENT_OLECREATE(...) 2 - DEFINE_GUID(...) and others). Menu says I must copy and paste the chosen option in my source code. But I don't understand what I should do. Have you seen this? Can you help me? Thanks again.

                      D 1 Reply Last reply
                      0
                      • I Ismael_1999

                        Hi, Dave. I tried here, but didn't get it. I use VS Community 2022 in portuguese. In my superior menu there is "Tools" and then "Create GUID". Next appears a small form with many options (1 - IMPLEMENT_OLECREATE(...) 2 - DEFINE_GUID(...) and others). Menu says I must copy and paste the chosen option in my source code. But I don't understand what I should do. Have you seen this? Can you help me? Thanks again.

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #13

                        Pick Registry format, click the Copy button, then you go back into the Project Properties, ..., and paste that value into the GUID box and click OK.

                        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                        Dave Kreskowiak

                        I 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          Pick Registry format, click the Copy button, then you go back into the Project Properties, ..., and paste that value into the GUID box and click OK.

                          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                          Dave Kreskowiak

                          I Offline
                          I Offline
                          Ismael_1999
                          wrote on last edited by
                          #14

                          Dave. For me there is no GUID box. But I'll tell yu my original problem, and maybe you can give another tip. I'll install a desktop application for a client and need to store the instalation folder somewhere. Inicially I thougt of using windows registry, but the local available was CURRENTUSER, and I didn't like it. Then I read OriginalGriff's article and thougt of a text file anywhere I can find and the ALLUSERS folder appeared to be good for me. Then these problems arouse. Do you have any suggestion? Thanks a lot.

                          D L 2 Replies Last reply
                          0
                          • I Ismael_1999

                            Dave. For me there is no GUID box. But I'll tell yu my original problem, and maybe you can give another tip. I'll install a desktop application for a client and need to store the instalation folder somewhere. Inicially I thougt of using windows registry, but the local available was CURRENTUSER, and I didn't like it. Then I read OriginalGriff's article and thougt of a text file anywhere I can find and the ALLUSERS folder appeared to be good for me. Then these problems arouse. Do you have any suggestion? Thanks a lot.

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #15

                            What type of app are you writing, which version of .NET or the .NET Framework are you writing this code against, and are you exposing any components in your app to COM?

                            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                            Dave Kreskowiak

                            I 1 Reply Last reply
                            0
                            • I Ismael_1999

                              Dave. For me there is no GUID box. But I'll tell yu my original problem, and maybe you can give another tip. I'll install a desktop application for a client and need to store the instalation folder somewhere. Inicially I thougt of using windows registry, but the local available was CURRENTUSER, and I didn't like it. Then I read OriginalGriff's article and thougt of a text file anywhere I can find and the ALLUSERS folder appeared to be good for me. Then these problems arouse. Do you have any suggestion? Thanks a lot.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #16

                              You do not need to store that. You can get that info at runtime.

                              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                              I 1 Reply Last reply
                              0
                              • L Lost User

                                You do not need to store that. You can get that info at runtime.

                                Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                I Offline
                                I Offline
                                Ismael_1999
                                wrote on last edited by
                                #17

                                Hi, Eddy. How can I obtain the installation folder of an application? Note that I need it from another application, not the one running at this moment. Thanks.

                                L 1 Reply Last reply
                                0
                                • D Dave Kreskowiak

                                  What type of app are you writing, which version of .NET or the .NET Framework are you writing this code against, and are you exposing any components in your app to COM?

                                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                  Dave Kreskowiak

                                  I Offline
                                  I Offline
                                  Ismael_1999
                                  wrote on last edited by
                                  #18

                                  I'm writing a program to control incomes and expenses of a small company. .NET Framework version 4.8.04084. Microsoft Visual Studio Community 2022 Version 17.2.6 Is this what you wanted?

                                  D 1 Reply Last reply
                                  0
                                  • I Ismael_1999

                                    I'm writing a program to control incomes and expenses of a small company. .NET Framework version 4.8.04084. Microsoft Visual Studio Community 2022 Version 17.2.6 Is this what you wanted?

                                    D Offline
                                    D Offline
                                    Dave Kreskowiak
                                    wrote on last edited by
                                    #19

                                    OK, so you're writing either a Windows Forms app, or WPF, for some accounting function. You have no reason at all to be saving anything to HKEY_LOCAL_MACHINE. Follow the advice of Griff. As for the Guid of the app, you really have no use for it so why are you interested in it?

                                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                    Dave Kreskowiak

                                    I 1 Reply Last reply
                                    0
                                    • I Ismael_1999

                                      Hi, Eddy. How can I obtain the installation folder of an application? Note that I need it from another application, not the one running at this moment. Thanks.

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #20

                                      And it's prolly not an application that you launched, so it is a running app that you did not launch? Does the app have a windows handle? Then you can get the executables location. https://www.autohotkey.com/boards/viewtopic.php?t=69925[^]

                                      Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                                      1 Reply Last reply
                                      0
                                      • D Dave Kreskowiak

                                        OK, so you're writing either a Windows Forms app, or WPF, for some accounting function. You have no reason at all to be saving anything to HKEY_LOCAL_MACHINE. Follow the advice of Griff. As for the Guid of the app, you really have no use for it so why are you interested in it?

                                        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                        Dave Kreskowiak

                                        I Offline
                                        I Offline
                                        Ismael_1999
                                        wrote on last edited by
                                        #21

                                        Hi, Dave. I changed my approach. Now I get a special folder path and will use it to store a text file containing the installation folder of my app. The command is:

                                                string caminho = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData).ToString();
                                        

                                        I think it will solve my problem. Thanks for your help and interest!

                                        D 1 Reply Last reply
                                        0
                                        • I Ismael_1999

                                          Hi, Dave. I changed my approach. Now I get a special folder path and will use it to store a text file containing the installation folder of my app. The command is:

                                                  string caminho = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData).ToString();
                                          

                                          I think it will solve my problem. Thanks for your help and interest!

                                          D Offline
                                          D Offline
                                          Dave Kreskowiak
                                          wrote on last edited by
                                          #22

                                          GetFolderPath always returns a string, so why are you calling .ToString() on a string?

                                          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                          Dave Kreskowiak

                                          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