473,513 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare date in Registry with local time

134 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. RegistryKey key1;
  2.  
  3.         private void btnTrial_Click(object sender, EventArgs e)
  4.         {
  5.             DateTime current = DateTime.Now;
  6.  
  7.             key1 = Registry.CurrentUser.OpenSubKey("myKey");
  8.  
  9.  
  10.             if (key1 == null)
  11.             {
  12.                 key1 = Registry.CurrentUser.CreateSubKey("myKey");
  13.                 key1.SetValue("StartTrialDate", current.ToShortDateString());
  14.                 key1.Close();
  15.  
  16.                 using (mainForm mainform = new mainForm())
  17.                 {
  18.                     Hide();
  19.                     mainform.ShowDialog();
  20.                     Environment.Exit(0);
  21.                 }
  22.             }
  23.             if (key1 != null)
  24.             {
  25.                 RegistryKey openKey = Registry.CurrentUser.OpenSubKey("myKey");
  26.  
  27.                 if (openKey.GetValue("StartTrialDate") > current)
  28.                 {
  29.  
  30.                 }
  31.             }
  32.         }
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
Feb 2 '13 #1
9 2958
zmbd
5,501 Recognized Expert Moderator Expert
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
Feb 3 '13 #2
michaeldebruin
134 New Member
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:
Expand|Select|Wrap|Line Numbers
  1.  if (openKey.GetValue("StartTrialDate") > current)
  2.                 {
  3.  
  4.                 }
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:
Expand|Select|Wrap|Line Numbers
  1. if (openKey.GetValue("StartTrialDate").ToString() == current.ToShortDateString())
  2.                 {
  3.  
  4.                 }
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?
Feb 4 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
Don't convert both to a string. The registry key you're reading is a string, convert that to a date.
Feb 4 '13 #4
michaeldebruin
134 New Member
Oke thanks, but how should I do that?
Feb 4 '13 #5
Rabbit
12,516 Recognized Expert Moderator MVP
Use the Convert.ToDateTime() function.
Feb 4 '13 #6
michaeldebruin
134 New Member
Oke so afther all the problem was very easy to solve. But thanks Rabbit, your solution solved it.
Feb 5 '13 #7
michaeldebruin
134 New Member
Oke nevermind. This is my check now:
Expand|Select|Wrap|Line Numbers
  1. if (Convert.ToDateTime(openKey) < current)
But as soon as I run it. It gives me the error that it can't convert a System.RegistryKey to a System.IConvertible.
Feb 5 '13 #8
zmbd
5,501 Recognized Expert Moderator Expert
Pull the value from the key to variable.
Change the variable data type to date
Compare the variables.
Feb 5 '13 #9
michaeldebruin
134 New Member
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.
Feb 5 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
16224
by: jorntk | last post by:
Hi, How do i compare two date in YYYY-MM-DD format to determine which one is earlier? thanks and regards Jorn
3
35045
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
7
7034
by: Drunkalot | last post by:
How can I set the system time using a DateTime object??? s...
5
8023
by: rgrandidier | last post by:
I have a SQL table that stores the UTC time for a column, I then want to display the UTC time to the user in their local time. My approach is to have the user set their time zone via their user...
3
23734
by: Randall Parker | last post by:
Suppose one has a database of UTC times that are from different dates in the past. The problem with translating those times to a local time is that one does not know for each UTC time whether the...
6
21174
by: vunet.us | last post by:
How can I convert UTC time such as 1173451235415 to a local time I can read? Thank you
1
14580
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
8
6035
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is running in a different time zone as compared to my...
2
8270
by: deepikashalini | last post by:
hi, Im storing Issue date in database. Issuedate stored as varchar. and then compare to Passing string date('03/06/2008') what is query to compare storing varchar date to passing string...
4
2687
Claus Mygind
by: Claus Mygind | last post by:
I have a date variable which I receive in the following format: "2010-02-07T16:49:26Z" This is a GMT timestamp on my Garmin GPS. I would like the result in my own local time, based on the...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7111
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.