tags:

views:

962

answers:

9

I need my application to expire 30 days from today, I will store the current date in the application config.How will I check if the application has expired ? I don't mind if the user changed the clock back and the app works (too stupid a user to do that).

if (appmode == "Trial") {      

      ????

      }
+2  A: 
DateTime _expiryDate = DateTime.Now + TimeSpan.FromDays(30);
SWeko
+3  A: 

One I can answer confidently!

DateTime expiryDate = DateTime.Now.AddDays(30);

Or possibly - if you just want the date without a time attached which might be more appropriate:

DateTime expiryDate = DateTime.Today.AddDays(30);
Murph
+11  A: 

DateTime.AddDays does that:

DateTime expires = yourDate.AddDays(30);
Fredrik Mörk
Make sure to remeber to assign the result. I'm always doing yourdate.AddDays(30) and then wondering why yourDate hasn't changed.
Cameron MacFarland
yourDate isn't changing though, its the expires date that is so in this instance its ok.
James
@Cameron: that's where `DateTime expires` part comes into the picture
Fredrik Mörk
+4  A: 
string dateInString = "01.10.2009";

DateTime startDate = DateTime.Parse(dateInString);
DateTime expiryDate = startDate.AddDays(30);
if (DateTime.Now > expiryDate) {
  //... trial expired
}
Rafal Ziolkowski
`DateTime expiryDate = new DateTime(startDate);` isn't required
Scoregraphic
`DateTime expiryDate = startDate.AddDays(30);` would be correct, cause `expiryDate.AddDays(30)` will write the result into nirvana.
Oliver
Oliver, thanks for patch :)
Rafal Ziolkowski
+1  A: 

A better solution might be to introduce a license file with a counter. Write into the license file the install date of the application (during installation). Then everytime the application is run you can edit the license file and increment the count by 1. Each time the application starts up you just do a quick check to see if the 30 uses of the application has been reached i.e.

if (LicenseFile.Counter == 30)
    // go into expired mode

Also this will solve the issue if the user has put the system clock back as you can do a simple check to say

if (LicenseFile.InstallationDate < SystemDate)
    // go into expired mode (as punishment for trying to trick the app!)

The problem with your current setup is the user will have to use the application every day for 30 days to get their full 30 day trial.

James
30 days from date of installation or from date of first use is not at all uncommon or unreasonable.
Murph
Yeah but as I mentioned, you would have to use the application every day for 30 days on the trot to get your full 30 day trial. Most applications nowadays would make sure you get your full 30 days regardless of when you first used/installed the application.
James
+2  A: 

A possible solution would be on first run, create a file containing the current date and put it in IsolatedStorage. For subsequent runs, check the contents of the file and compare with the current date; if the date difference is greater than 30 days, inform the user and close the application.

Ed Courtenay
+1 isolated storage in action... the registry might also be another candidate, although requires more security than isolated storage.
Ian
A: 

@Ed courtenay, @James, I have one stupid question. How to keep user away from this file?(File containing expiry date). If the user has installation rights, then obviously user has access to view files also. Changing the extension of file wont help. So, how to keep this file safe and away from users hands?

Shekhar
Solutions: encrypt data and/or add file hash. If file is corrupted consider trial as expired
Victor Hurdugaci
Thanks for answering Victor.Encrypting will surely make the file unreadable.
Shekhar
DPAPI might be useful? Especially with an application unique salt and possibly using the machine context instead of the user context (wrappers in C# at System.Security.Cryptography.DataProtection)
Oskar Duveborn
As already suggested you would encrypt the data, if you are not too keen on using a file you can simply bury the data into the registry.
James
A: 

You need to store the first run time of the program in order to do this. How I'd probably do it is using the built in application settings in visual studio. Make one called InstallDate which is a User Setting and defaults to DateTime.MinValue or something like that (e.g. 1/1/1900).

Then when the program is run the check is simple:

if (appmode == "trial")
{
  // If the FirstRunDate is MinValue, it's the first run, so set this value up
  if (Properties.Settings.Default.FirstRunDate == DateTime.MinValue)
  {
    Properties.Settings.Default.FirstRunDate = DateTime.Now;
    Properties.Settings.Default.Save(); 
  }

  // Now check whether 30 days have passed since the first run date
  if (Properties.Settings.Default.FirstRunDate.AddMonths(1) < DateTime.Now)
  {
    // Do whatever you want to do on expiry (exception message/shut down/etc.)
  }
}

User settings are stored in a pretty weird location (something like C:\Documents and Settings\YourName\Local Settings\Application Data) so it will be pretty hard for average joe to find it anyway. If you want to be paranoid, just encrypt the date before saving it to settings.

EDIT: Sigh, misread the question, not as complex as I thought >.>

mrnye