2009-11-03 127 views
20

我需要我的應用程序從今天開始過期30天,我會將當前日期存儲在應用程序配置中。如何檢查應用程序是否已過期?我不介意用戶是否改變了時鐘,並且應用程序可以正常工作(用戶太愚蠢了)。C#從今天開始的30天日期

if (appmode == "Trial") {    

      ???? 

      } 

回答

42
string dateInString = "01.10.2009"; 

DateTime startDate = DateTime.Parse(dateInString); 
DateTime expiryDate = startDate.AddDays(30); 
if (DateTime.Now > expiryDate) { 
    //... trial expired 
} 
+0

'DateTime expiryDate = new DateTime(startDate);'不需要 – Scoregraphic 2009-11-03 08:34:07

+5

'DateTime expiryDate = startDate.AddDays(30);'會正確的,會導致'expiryDate.AddDays(30)'將結果寫入涅v。 – Oliver 2009-11-03 08:48:13

+0

奧利弗,感謝您的補丁:) – 2009-11-03 11:29:39

4
DateTime _expiryDate = DateTime.Now + TimeSpan.FromDays(30); 
13

一個我可以自信地回答!

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

或可能 - 如果你只是想沒有附加的時間,這可能是更合適的日期:

DateTime expiryDate = DateTime.Today.AddDays(30); 
18

DateTime.AddDays做的是:

DateTime expires = yourDate.AddDays(30); 
+2

確保記得分配結果。我總是在做yourdate.AddDays(30),然後想知道爲什麼yourDate沒有改變。 – 2009-11-03 08:36:55

+1

yourDate雖然沒有改變,但它的過期日期在這種情況下是可以的。 – James 2009-11-03 08:38:35

+0

@Cameron:這就是'DateTime expires'部分出現在圖片 – 2009-11-03 10:11:12

1

更好的解決方案可能需要在櫃檯上引入許可證文件。在許可證文件中寫入應用程序的安裝日期(安裝期間)。然後每次運行應用程序,您可以編輯許可文件,並以1增加計數每次應用程序啓動時,你只是做一個快速檢查,以查看該應用程序的30個使用已即

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

而且這將解決這個問題,如果用戶已經把系統時鐘撥回,你可以做一個簡單的檢查,說

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

與當前設置的問題是用戶將不得不每天使用的應用程序30天才能完成30天的全天試用。

+0

從安裝日期或首次使用日期起30天不是不尋常或不合理。 – Murph 2009-11-03 08:51:02

+1

是的,但正如我所提到的,您必須每天使用該應用程序30天,才能完成30天的完整試用。現在的大多數應用程序都會確保您獲得整整30天的時間,而不管您第一次使用/安裝應用程序的時間。 – James 2009-11-03 09:03:27

2

可能的解決方案將在第一次運行時創建一個包含當前日期的文件並將其放入IsolatedStorage中。對於後續運行,請檢查文件的內容並與當前日期進行比較;如果日期差異大於30天,請通知用戶並關閉應用程序。

+1

+1孤立存儲在行動...註冊表也可能是另一個候選人,雖然需要比孤立存儲更多的安全性。 – Ian 2009-11-03 10:06:05

0

@Ed courtenay,@詹姆斯, 我有一個愚蠢的問題。 如何讓用戶遠離該文件?(包含到期日期的文件)。 如果用戶具有安裝權限,那麼顯然用戶也可以訪問查看文件。 更改文件的擴展名不會幫助。 那麼,如何保持這個文件安全並遠離用戶的手?

+0

解決方案:加密數據和/或添加文件哈希。如果文件已損壞,請考慮將試用期限過期 – 2009-11-03 10:46:38

+0

感謝您回答Victor.Encrypting,肯定會使文件無法讀取。 – Shekhar 2009-11-03 10:49:22

+0

DPAPI可能是有用的嗎?特別是對於應用程序獨特的salt,可能使用機器上下文而不是用戶上下文(System.Security.Cryptography.DataProtection中的C#中的包裝器) – 2009-11-03 10:54:46

0

您需要存儲程序的第一次運行時間才能執行此操作。我可能會這樣做是使用visual studio中的內置應用程序設置。創建一個名爲InstallDate的用戶設置,默認爲DateTime.MinValue或類似的東西(例如1/1/1900)。

然後,當程序運行檢查很簡單:

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.) 
    } 
} 

用戶設置存儲在一個非常奇怪的位置(類似於C:\ Documents和Settings \ YOURNAME \本地設置\應用數據),所以無論如何,平均喬會很難找到它。如果你想成爲偏執狂,只需在將日期保存到設置之前加密日期。

編輯:嘆了口氣,誤解了這個問題,並不像我想的那麼複雜。>

13

DateTime.Now.Add(-30)

使你的日期從現在

+0

好點!很高興在這裏看到這一點。 – 2016-11-03 22:47:33

1
string[] servers = new string[] { 
     "nist1-ny.ustiming.org", 
     "nist1-nj.ustiming.org", 
     "nist1-pa.ustiming.org", 
     "time-a.nist.gov", 
     "time-b.nist.gov", 
     "nist1.aol-va.symmetricom.com", 
     "nist1.columbiacountyga.gov", 
     "nist1-chi.ustiming.org", 
     "nist.expertsmi.com", 
     "nist.netservicesgroup.com" 
     }; 
string dateStart, dateEnd; 

void SetDateToday() 
    { 
     Random rnd = new Random(); 
     DateTime result = new DateTime(); 
     int found = 0; 
     foreach (string server in servers.OrderBy(s => rnd.NextDouble()).Take(5)) 
     { 
      Console.Write("."); 
      try 
      { 
       string serverResponse = string.Empty; 
       using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream())) 
       { 
        serverResponse = reader.ReadToEnd(); 
        Console.WriteLine(serverResponse); 
       } 

       if (!string.IsNullOrEmpty(serverResponse)) 
       { 
        string[] tokens = serverResponse.Split(' '); 
        string[] date = tokens[1].Split(' '); 
        string time = tokens[2]; 
        string properTime; 

        dateStart = date[2] + "/" + date[0] + "/" + date[1]; 

        int month = Convert.ToInt16(date[0]), day = Convert.ToInt16(date[2]), year = Convert.ToInt16(date[1]); 
        day = day + 30; 
        if ((month % 2) == 0) 
        { 
         //MAX DAYS IS 30 
         if (day > 30) 
         { 
          day = day - 30; 
          month++; 
          if (month > 12) 
          { 
           month = 1; 
           year++; 
          } 
         } 
        } 
        else 
        { 
         //MAX DAYS IS 31 
         if (day > 31) 
         { 
          day = day - 31; 
          month++; 
          if (month > 12) 
          { 
           month = 1; 
           year++; 
          } 
         } 
        } 
        string sday, smonth; 
        if (day < 10) 
        { 
         sday = "0" + day; 
        } 
        if (month < 10) 
        { 
         smonth = "0" + month; 
        } 
        dateEnd = sday + "/" + smonth + "/" + year.ToString(); 

       } 

      } 
      catch 
      { 
       // Ignore exception and try the next server 
      } 
     } 
     if (found == 0) 
     { 
      MessageBox.Show(this, "Internet Connection is required to complete Registration. Please check your internet connection and try again.", "Not connected", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      Success = false; 
     } 
    } 

30天回我看到的代碼在某些網站的某些部分。做上面的例子暴露了一個小故障:將當前時間和日期更改爲開始日期將延長應用程序到期。

解決方案?請參閱在線時間服務器。

-2
if (cmb_mode_of_service.SelectedItem != null && cmb_term_of_service.SelectedItem != null) 
      { 
       if (cmb_mode_of_service.SelectedIndex > 0 && cmb_term_of_service.SelectedIndex > 0) 
       { 
        if (cmb_mode_of_service.SelectedItem.ToString() == "Single Service/Installation" || cmb_term_of_service.SelectedItem.ToString() == "Single Time") 
        { 
         int c2 = 1; 
         char c1 = 'A'; 
         DataRow dr = dt.NewRow(); 
         dr["SN"] = c2++; 
         dr["serviceid"] = txt_service_id.Text + "-" + c1++; 
         dr["servicedate"] = service_start_date.Text; 
         dr["servicestatus"] = "Pending"; 
         dr["serviceexcutive"] = "Not Alowed"; 
         dt.Rows.Add(dr); 
         dataGridView1.DataSource = dt; 

         txtexpirydate.Text = (Convert.ToDateTime(service_start_date.Text).AddDays(1)).ToString(); 

        } 
        else 
        { 



         if (cmb_mode_of_service.SelectedItem.ToString() == "Weekly Service") 
         { 
          int year = 0; 
          if (cmb_term_of_service.SelectedItem.ToString() == "One Year") 
          { 
           year = 1; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "Two Year") 
          { 
           year = 2; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "three year") 
          { 
           year = 3; 
          } 

          DateTime currentdate = Convert.ToDateTime(service_start_date.Text); 
          DateTime Enddate = currentdate.AddYears(+year); 
          txtexpirydate.Text = Enddate.ToString(); 

          char c1 = 'A'; 
          int c2 = 1; 
          for (var dt1 = currentdate.AddDays(7); dt1 <= Enddate; dt1 = dt1.AddDays(7)) 
          { 
           DataRow dr = dt.NewRow(); 
           dr["SN"] = c2++; 
           dr["serviceid"] = txt_service_id.Text + "-" + c1++; 
           dr["servicedate"] = dt1.ToString(); 
           dr["servicestatus"] = "Pending"; 
           dr["serviceexcutive"] = "Not Alowed"; 
           //txtexpirydate.Text = dt1.ToString(); 
           dt.Rows.Add(dr); 

          } 
          dataGridView1.DataSource = dt; 

         } 

         if (cmb_mode_of_service.SelectedItem.ToString() == "Fortnight Service") 
         { 
          int year = 0; 
          if (cmb_term_of_service.SelectedItem.ToString() == "One Year") 
          { 
           year = 1; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "Two Year") 
          { 
           year = 2; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "three year") 
          { 
           year = 3; 
          } 

          DateTime currentdate = Convert.ToDateTime(service_start_date.Text); 
          DateTime Enddate = currentdate.AddYears(+year); 
          txtexpirydate.Text = Enddate.ToString(); 

          char c1 = 'A'; 
          int c2 = 1; 
          for (var dt1 = currentdate.AddDays(15); dt1 <= Enddate; dt1 = dt1.AddDays(15)) 
          { 
           DataRow dr = dt.NewRow(); 
           dr["SN"] = c2++; 
           dr["serviceid"] = txt_service_id.Text + "-" + c1++; 
           dr["servicedate"] = dt1.ToString(); 
           dr["servicestatus"] = "Pending"; 
           dr["serviceexcutive"] = "Not Alowed"; 
           // txtexpirydate.Text = dt1.ToString(); 
           dt.Rows.Add(dr); 

          } 
          dataGridView1.DataSource = dt; 

         } 

         if (cmb_mode_of_service.SelectedItem.ToString() == "Monthly Service") 
         { 
          int year = 0; 
          if (cmb_term_of_service.SelectedItem.ToString() == "One Year") 
          { 
           year = 1; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "Two Year") 
          { 
           year = 2; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "three year") 
          { 
           year = 3; 
          } 

          DateTime currentdate = Convert.ToDateTime(service_start_date.Text); 
          DateTime Enddate = currentdate.AddYears(+year); 
          txtexpirydate.Text = Enddate.ToString(); 

          char c1 = 'A'; 
          int c2 = 1; 
          for (var dt1 = currentdate.AddDays(30); dt1 <= Enddate; dt1 = dt1.AddDays(30)) 
          { 
           DataRow dr = dt.NewRow(); 
           dr["SN"] = c2++; 
           dr["serviceid"] = txt_service_id.Text + "-" + c1++; 
           dr["servicedate"] = dt1.ToString(); 
           dr["servicestatus"] = "Pending"; 
           dr["serviceexcutive"] = "Not Alowed"; 
           // txtexpirydate.Text = dt1.ToString(); 
           dt.Rows.Add(dr); 

          } 
          dataGridView1.DataSource = dt; 

         } 


         if (cmb_mode_of_service.SelectedItem.ToString() == "Trimister Service") 
         { 
          int year = 0; 
          if (cmb_term_of_service.SelectedItem.ToString() == "One Year") 
          { 
           year = 1; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "Two Year") 
          { 
           year = 2; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "three year") 
          { 
           year = 3; 
          } 

          DateTime currentdate = Convert.ToDateTime(service_start_date.Text); 
          DateTime Enddate = currentdate.AddYears(+year); 
          txtexpirydate.Text = Enddate.ToString(); 

          char c1 = 'A'; 
          int c2 = 1; 
          for (var dt1 = currentdate.AddDays(90); dt1 <= Enddate; dt1 = dt1.AddDays(90)) 
          { 
           DataRow dr = dt.NewRow(); 
           dr["SN"] = c2++; 
           dr["serviceid"] = txt_service_id.Text + "-" + c1++; 
           dr["servicedate"] = dt1.ToString(); 
           dr["servicestatus"] = "Pending"; 
           dr["serviceexcutive"] = "Not Alowed"; 
           // txtexpirydate.Text = dt1.ToString(); 
           dt.Rows.Add(dr); 

          } 
          dataGridView1.DataSource = dt; 

         } 

         if (cmb_mode_of_service.SelectedItem.ToString() == "Half Yearly Service") 
         { 
          int year = 0; 
          if (cmb_term_of_service.SelectedItem.ToString() == "One Year") 
          { 
           year = 1; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "Two Year") 
          { 
           year = 2; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "three year") 
          { 
           year = 3; 
          } 

          DateTime currentdate = Convert.ToDateTime(service_start_date.Text); 
          DateTime Enddate = currentdate.AddYears(+year); 
          txtexpirydate.Text = Enddate.ToString(); 

          char c1 = 'A'; 
          int c2 = 1; 
          for (var dt1 = currentdate.AddDays(180); dt1 <= Enddate; dt1 = dt1.AddDays(180)) 
          { 
           DataRow dr = dt.NewRow(); 
           dr["SN"] = c2++; 
           dr["serviceid"] = txt_service_id.Text + "-" + c1++; 
           dr["servicedate"] = dt1.ToString(); 
           dr["servicestatus"] = "Pending"; 
           dr["serviceexcutive"] = "Not Alowed"; 
           // txtexpirydate.Text = dt1.ToString(); 
           dt.Rows.Add(dr); 

          } 
          dataGridView1.DataSource = dt; 

         } 

         if (cmb_mode_of_service.SelectedItem.ToString() == "Yearly Service") 
         { 
          int year = 0; 
          if (cmb_term_of_service.SelectedItem.ToString() == "One Year") 
          { 
           year = 1; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "Two Year") 
          { 
           year = 2; 
          } 

          if (cmb_term_of_service.SelectedItem.ToString() == "three year") 
          { 
           year = 3; 
          } 

          DateTime currentdate = Convert.ToDateTime(service_start_date.Text); 
          DateTime Enddate = currentdate.AddYears(+year); 
          txtexpirydate.Text = Enddate.ToString(); 

          char c1 = 'A'; 
          int c2 = 1; 
          for (var dt1 = currentdate.AddDays(365); dt1 <= Enddate; dt1 = dt1.AddDays(365)) 
          { 
           DataRow dr = dt.NewRow(); 
           dr["SN"] = c2++; 
           dr["serviceid"] = txt_service_id.Text + "-" + c1++; 
           dr["servicedate"] = dt1.ToString(); 
           dr["servicestatus"] = "Pending"; 
           dr["serviceexcutive"] = "Not Alowed"; 
           //txtexpirydate.Text = dt1.ToString(); 
           dt.Rows.Add(dr); 

          } 
          dataGridView1.DataSource = dt; 

         } 

        } 
       } 
+1

如果您真的認爲您的代碼塊添加了其他10個答案中未提及的內容,請提供解釋。儘管如此,解釋應該始終遵循StackOverflow中的代碼! – ZygD 2015-06-29 12:41:02