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. Problem Reading And Writing Registry In C#

Problem Reading And Writing Registry In C#

Scheduled Pinned Locked Moved C#
csharpcomwindows-adminhelp
9 Posts 4 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.
  • B Offline
    B Offline
    Ben Magee
    wrote on last edited by
    #1

    Hi guys, I have this code in my form1_load event:

            RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise");
            string licenseCo = licenseCheck.GetValue("tlb").ToString();
           DateTime licenseDate = Convert.ToDateTime(licenseCo);
                if (licenseDate.AddDays(30) == System.DateTime.Now)
                {
                    MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    Application.Exit();
                }
    

    now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:

    RegistryKey licenseKey;
    licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
    licenseKey.SetValue("tlb",DateTime.Now);
    Registry.LocalMachine.Flush();

    However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.

    L L A 3 Replies Last reply
    0
    • B Ben Magee

      Hi guys, I have this code in my form1_load event:

              RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise");
              string licenseCo = licenseCheck.GetValue("tlb").ToString();
             DateTime licenseDate = Convert.ToDateTime(licenseCo);
                  if (licenseDate.AddDays(30) == System.DateTime.Now)
                  {
                      MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                      Application.Exit();
                  }
      

      now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:

      RegistryKey licenseKey;
      licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
      licenseKey.SetValue("tlb",DateTime.Now);
      Registry.LocalMachine.Flush();

      However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, some remarks: 1. exceptions tend to contain a lot of information, however you need to look at all of it, i.e. use Exception.ToString() instead of Exception.Message one of the pieces of information it offers is a filename and linenumber; that would point straight to the culprit line. Also teach your IDE to always show line numbers in edit windows, see here[^]. 2. Recent Windows versions don't allow a regular user to access/modify system settings that would extend to other users; the LocalMachine registry hive would be in that category. 3. Your expiration test needs a less than comparison, not an equals. DateTimes seldom are equal as they contain information up to the tick (one tenth of a microsecond). :)

      Luc Pattyn


      Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


      Local announcement (Antwerp region): Lange Wapper? Neen!


      1 Reply Last reply
      0
      • B Ben Magee

        Hi guys, I have this code in my form1_load event:

                RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise");
                string licenseCo = licenseCheck.GetValue("tlb").ToString();
               DateTime licenseDate = Convert.ToDateTime(licenseCo);
                    if (licenseDate.AddDays(30) == System.DateTime.Now)
                    {
                        MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        Application.Exit();
                    }
        

        now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:

        RegistryKey licenseKey;
        licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
        licenseKey.SetValue("tlb",DateTime.Now);
        Registry.LocalMachine.Flush();

        However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.

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

        I can only speculate on this but if the statement

        licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");

        fails for any reason then licenseKey will be uninitialised.

        1 Reply Last reply
        0
        • B Ben Magee

          Hi guys, I have this code in my form1_load event:

                  RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise");
                  string licenseCo = licenseCheck.GetValue("tlb").ToString();
                 DateTime licenseDate = Convert.ToDateTime(licenseCo);
                      if (licenseDate.AddDays(30) == System.DateTime.Now)
                      {
                          MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                          Application.Exit();
                      }
          

          now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:

          RegistryKey licenseKey;
          licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
          licenseKey.SetValue("tlb",DateTime.Now);
          Registry.LocalMachine.Flush();

          However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          Ben Magee wrote:

          OpenSubKey(@"software/openorganise");

          Ben Magee wrote:

          CreateSubKey(@"Software\openorganise");

          Should these both be backslash? Alan.

          B 2 Replies Last reply
          0
          • A Alan N

            Ben Magee wrote:

            OpenSubKey(@"software/openorganise");

            Ben Magee wrote:

            CreateSubKey(@"Software\openorganise");

            Should these both be backslash? Alan.

            B Offline
            B Offline
            Ben Magee
            wrote on last edited by
            #5

            Thanks for the feedback, I will look into all these issues now. Could you please give a example of a lessthan statement in the current context im using? thanks, Ben.

            L 1 Reply Last reply
            0
            • B Ben Magee

              Thanks for the feedback, I will look into all these issues now. Could you please give a example of a lessthan statement in the current context im using? thanks, Ben.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              if (1 < 2) MesageBox("one is less than two");

              :doh:

              Luc Pattyn


              Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


              Local announcement (Antwerp region): Lange Wapper? Neen!


              1 Reply Last reply
              0
              • A Alan N

                Ben Magee wrote:

                OpenSubKey(@"software/openorganise");

                Ben Magee wrote:

                CreateSubKey(@"Software\openorganise");

                Should these both be backslash? Alan.

                B Offline
                B Offline
                Ben Magee
                wrote on last edited by
                #7

                After changing my registry edits to CurrentUser and changing to backslashes im still getting my exception error. I also changed

                System.DateTime.Now;

                to

                system.DateTime.Today;

                would that work any better? Cheers, Ben.

                L L 2 Replies Last reply
                0
                • B Ben Magee

                  After changing my registry edits to CurrentUser and changing to backslashes im still getting my exception error. I also changed

                  System.DateTime.Now;

                  to

                  system.DateTime.Today;

                  would that work any better? Cheers, Ben.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  use the line numbers as I've told you, and learn to debug your code yourself. This thread already is a lot longer than the code it is about... :)

                  Luc Pattyn


                  Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


                  Local announcement (Antwerp region): Lange Wapper? Neen!


                  1 Reply Last reply
                  0
                  • B Ben Magee

                    After changing my registry edits to CurrentUser and changing to backslashes im still getting my exception error. I also changed

                    System.DateTime.Now;

                    to

                    system.DateTime.Today;

                    would that work any better? Cheers, Ben.

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

                    Ben Magee wrote:

                    would that work any better?

                    Would what work? And better how? You need to show the extract of the code where the problem occurs, and the full description or text of the exception. The question above really makes almost no sense whatever, and even less in the context of your original question. Read through a few more of the posts here to get an idea of the sort of questions that get resolved quickly.

                    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