2010-05-16 43 views
3

这是我想要做的,我想在第一次安装程序时存储日期,并且还存储程序上次运行的日期。我希望代码检查自安装以来是否超过30天,以便关闭功能。我还想检查系统日期是否小于上次打开的日期,如果是这样,请将安装的日期写入到1/1/1901以防止程序运行。为试用版本读取和写入注册表的日期

请记住,这不是一个消费者计划,而是一个商业计划,我不希望黑客破解它,他们可能会这样做,但这很好,我只是想让潜在客户有理由考虑购买该程序和审判结束后会提示。

Q1:这听起来合理吗?

问题2:我应该如何隐藏这些日期的事实,以便它不易识别和更改?

非常感谢李

回答

2

命名空间Microsoft.Win32是你需要的。您需要查看以下两个课程:RegistryRegistryKey

您可以将您的日期的哈希码存储在您将使用的注册表项中。

除了我不会把它放在注册表中。除了本地安装文件夹之外,AppData文件夹是更好的地方。也许你会想要使用System.IO命名空间的二进制文件,以便可以编写二进制数据。 BinaryWriterBinaryReader类可能是您需要这样做的。

+0

你的意思是AppData文件夹? – Juan 2011-01-03 01:04:34

-1

我不会这个存储在注册表中,因为它真的很容易改变(在地方至少可以写)。我会将它写在Local Data文件夹中的一个小文件中并加密它。可能将其存储在几个地方以防有人删除文件。

1

我会建议隐藏的通用应用程序数据目录而不是注册表。并用二进制格式写日期:

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

此代码中设置的30天限制在哪里?谢谢 – Jamie 2010-05-17 16:26:53