我希望我的应用程序从今天起30天后过期,我将当前日期存储在应用程序配置中。如何检查应用程序是否已过期?如果用户将时钟往回更改并且应用程序仍能运行(用户太愚蠢了),那么我不介意。
if (appmode == "Trial") {
????
}
string dateInString = "01.10.2009";
DateTime startDate = DateTime.Parse(dateInString);
DateTime expiryDate = startDate.AddDays(30);
if (DateTime.Now > expiryDate) {
//... trial expired
}
DateTime.AddDays
可以实现这一功能:
DateTime expires = yourDate.AddDays(30);
DateTime expires
部分发挥作用的地方。 - Fredrik MörkDateTime.Now.Add(-30)
这段代码会返回当前时间向前推 30 天的日期。
有一个问题我可以自信地回答!
DateTime expiryDate = DateTime.Now.AddDays(30);
或者,如果您只想要日期而没有时间的话,这可能更为合适:
DateTime expiryDate = DateTime.Today.AddDays(30);
DateTime _expiryDate = DateTime.Now + TimeSpan.FromDays(30);
一种可能的解决方案是,在第一次运行时,创建一个包含当前日期的文件并将其放入独立存储中。对于后续的运行,检查文件内容并与当前日期进行比较;如果日期差大于30天,则通知用户并关闭应用程序。
你只需要使用DateTime.AddDays
例如:
从今天开始加30天:
DateTime expiryDate = DateTime.Today.AddDays(30);
从今天开始减去30天:
DateTime expiryDate = DateTime.Today.AddDays(-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!)
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;
}
}
我在某个网站的某个部分看到了那段代码。上面的示例揭示了一个漏洞:将当前时间和日期更改为开始日期会延长应用程序的过期时间。
解决方案?参考在线时间服务器。
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 and Settings\YourName\Local Settings\Application Data),因此对于普通用户来说,找到它将会非常困难。如果您想要更加谨慎,只需在保存设置之前对数据进行加密。
编辑:唉,我误读了问题,它并不像我想象的那么复杂 >.>
DateTime expiryDate = new DateTime(startDate);
不是必需的。 - ScoregraphicDateTime expiryDate = startDate.AddDays(30);
是正确的,因为expiryDate.AddDays(30)
会把结果写入虚无之中。 - Oliver