开发者

Reading and writing dates to the registry for trial version purposes

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 progr开发者_如何转开发am 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


The Microsoft.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 AppData folder is a better place, in addition 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 to do this.


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();
   }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜