2010-05-16 41 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