tags:

views:

145

answers:

3

Hi all,

Here's what I want to do, I want to store the date the first time the program is installed and also store a date when was the program was last run. I want the code to check to see if it was more than 30 days since the installation so I can turn off features. I also want to check if the system date is less than the last opened date and if so write the installed date to 1/1/1901 to prevent the program from running.

Keeping in mind that this is not a consumer program but a business program I don't expect hackers to crack it, they may do but that is fine I simply want to give potential customers a reason to consider purchasing the program and the end of the trial will prompt this.

Q1: Does this sound reasonable?

Q2: How should I hide the fact these are dates so it's not easily identified and changed?

Many thanks Lee

A: 

I would not store this in the registry because it's really easy to change (at least in the places you can write). I would write it in the Local Data folder in a little file and encrypt it. Probably store it in a couple of places in case someone deletes the file.

Jouke van der Maas
+2  A: 

The Micrsoft.Win32 namespace is what you need. You will want to look at the two following classes: Registry and RegistryKey.

You could store the hash code of your date within the registry key you will use.

Except that I would neither place it in the registry. The AddData folder is a better place, in addiditon to your local installation folder. Perhaps will you want to to use binaries with the System.IO namespace so that you can write binary data. The BinaryWriter and BinaryReader classes are probably what you will need.

Will Marcouiller
+1  A: 

I would suggest the hidden common application data directory instead of the registry. And write the dates in binary format:

static string appDataFile;

static void Main(string[] args)
{
   string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
   appDataPath = System.IO.Path.Combine(appDataPath, "MyApplication");
   if (!System.IO.Directory.Exists(appDataPath))
      System.IO.Directory.CreateDirectory(appDataPath);
   appDataFile = System.IO.Path.Combine(appDataPath, "History.dat");

   DateTime[] dates;
   if (System.IO.File.Exists(appDataFile))
      dates = ReadDates();
   else
      dates = new DateTime[] {DateTime.Now, DateTime.Now};

   Console.WriteLine("First: {0}\r\nLast: {1}", dates[0], dates[1]);

   dates[1] = DateTime.Now;
   WriteDates(dates);
}

static DateTime[] ReadDates()
{
   System.IO.FileStream appData = new System.IO.FileStream(
      appDataFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);

   List<DateTime> result = new List<DateTime>();
   using (System.IO.BinaryReader br = new System.IO.BinaryReader(appData))
   {
      while (br.PeekChar() > 0)
      {
         result.Add(new DateTime(br.ReadInt64()));
      }
      br.Close();
   }
   return result.ToArray();
}

static void WriteDates(IEnumerable<DateTime> dates)
{
   System.IO.FileStream appData = new System.IO.FileStream(
      appDataFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);

   List<DateTime> result = new List<DateTime>();
   using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(appData))
   {
      foreach(DateTime date in dates)
         bw.Write(date.Ticks);
      bw.Close();
   }
}
BlueMonkMN
Where is the 30 day limit set in this code? Thanks
Jamie