Compare date in Registry with local time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michaeldebruin
    New Member
    • Feb 2011
    • 134

    Compare date in Registry with local time

    Hello everyone,

    Can you guys please help me with the following problem. The first time my application opens up, it should get the local date out of the System and store this in a registry key. But the second time it should get the date stored in that registryKey and compare it to the date right now.
    And there is where I am stuck, I just don't know how to get the date out of that key and compare it to the date right now.

    This is my code:

    Code:
    RegistryKey key1;
    
            private void btnTrial_Click(object sender, EventArgs e)
            {
                DateTime current = DateTime.Now;
    
                key1 = Registry.CurrentUser.OpenSubKey("myKey");
                
    
                if (key1 == null)
                {
                    key1 = Registry.CurrentUser.CreateSubKey("myKey");
                    key1.SetValue("StartTrialDate", current.ToShortDateString());
                    key1.Close();
    
                    using (mainForm mainform = new mainForm())
                    {
                        Hide();
                        mainform.ShowDialog();
                        Environment.Exit(0);
                    }
                }
                if (key1 != null)
                {
                    RegistryKey openKey = Registry.CurrentUser.OpenSubKey("myKey");
    
                    if (openKey.GetValue("StartTrialDate") > current)
                    {
    
                    }
                }
            }
    at the last "if" statement it should compare the date stored in the key with the DateTime.Now.

    I hope you guys can help me out.

    Thanks in advance,

    Michael
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    You fail to provide the information for the name-space etc...
    You also fail to provide the exact nature of the failure of your code. Frankly, most of us do not have the time to pull your script, compile and so-forth.

    Therefore, I'll direct you to a general tutorial that should help you find your specific error.
    Working with Windows Registry in C#

    And because I like a video occasionally for walk-thrus, and because I'm at home and have access to these today: C# Tutorial: Create and Delete Registry Keys
    Last edited by zmbd; Feb 3 '13, 12:55 AM.

    Comment

    • michaeldebruin
      New Member
      • Feb 2011
      • 134

      #3
      The reason why it fails, is because I've saved a date in a registrykey. But when I want to open it, I want to check if that date is older then date right now.
      For that reason I've used an "if" statement like the following one:
      Code:
       if (openKey.GetValue("StartTrialDate") > current)
                      {
       
                      }
      But when I use on that way, it gives me an error that I cannot compare a object with a system.datetime . So I'll have to create 2 strings out of. Which results in the following if statement:
      Code:
      if (openKey.GetValue("StartTrialDate").ToString() == current.ToShortDateString())
                      {
      
                      }
      But I want to check if the date from the registrykey is lower then the date right now. So I am trying to use the lower then symbol "<". But this gives me an error that I can't use such a symbol to compare 2 strings. So the question is, how can I check whether the date stored in the registryKey is lower or earlier then the system date right now?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Don't convert both to a string. The registry key you're reading is a string, convert that to a date.

        Comment

        • michaeldebruin
          New Member
          • Feb 2011
          • 134

          #5
          Oke thanks, but how should I do that?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Use the Convert.ToDateT ime() function.

            Comment

            • michaeldebruin
              New Member
              • Feb 2011
              • 134

              #7
              Oke so afther all the problem was very easy to solve. But thanks Rabbit, your solution solved it.

              Comment

              • michaeldebruin
                New Member
                • Feb 2011
                • 134

                #8
                Oke nevermind. This is my check now:
                Code:
                if (Convert.ToDateTime(openKey) < current)
                But as soon as I run it. It gives me the error that it can't convert a System.Registry Key to a System.IConvert ible.

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  Pull the value from the key to variable.
                  Change the variable data type to date
                  Compare the variables.

                  Comment

                  • michaeldebruin
                    New Member
                    • Feb 2011
                    • 134

                    #10
                    Thanks a lot, it seemed that I've made the big mistake by trying to convert the path to the RegistryKey to a date. Instead of converting its value.

                    Comment

                    Working...