It's late but I have implemented this algorithm:
You need to implement AppConstants class with the values then it will work.
private void CheckTrialVersion()
{
var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
string startDayPath = $"Software\\{AppConstants.CompanyName}\\{AppConstants.SoftwareName}";
string specialFilePath = $"Software\\{AppConstants.SpecialPath}\\{AppConstants.CompanyName}\\{AppConstants.SoftwareName}";
string lastLoginDayPath = $"Software\\{AppConstants.CompanyName}\\{AppConstants.SoftwareName}";
try
{
RegistryKey startDay = key.OpenSubKey(startDayPath);
RegistryKey specialFile = key.OpenSubKey(specialFilePath);
RegistryKey lastLogin = key.OpenSubKey(lastLoginDayPath);
if (startDay != null && lastLogin != null)
{
var lastLoginTemp = lastLogin.GetValue(AppConstants.LastLogin);
var startDateTemp = startDay.GetValue(AppConstants.StartDay);
if (startDateTemp == null || lastLoginTemp == null)
{
MessageBox.Show("It seems somebody try to HACK the application!!!", "Access denied!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var startDayValue = StringCipher.Decrypt(startDateTemp.ToString());
var lastLoginValue = StringCipher.Decrypt(lastLoginTemp.ToString());
if (DateTime.TryParse(startDayValue, out DateTime startDayDateValue) && DateTime.TryParse(lastLoginValue, out DateTime lastLoginDateValue))
{
if (startDayDateValue.AddDays(AppConstants.TrialPeriod) > DateTime.Now
&& lastLoginDateValue <= DateTime.Now)
{
Activated = true;
ModifyLastLogin();
}
else if (lastLoginDateValue > DateTime.Now)
{
MessageBox.Show("It seems Date and Time is modified!!!", "Date and Time problem", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Globals.ThisAddIn.Application.WorkbookActivate -= Application_WorkbookActivate;
}
else
{
MessageBox.Show("Trial time is finished!!", "Expired", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Globals.ThisAddIn.Application.WorkbookActivate -= Application_WorkbookActivate;
//todo: create a way to buy license.
}
}
else
{
MessageBox.Show("It seems somebody try to HACK the application!!!", "Access denied!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Globals.ThisAddIn.Application.WorkbookActivate -= Application_WorkbookActivate;
}
}
else
{
if (specialFile != null)
{
MessageBox.Show("It seems somebody try to HACK the application!!!", "Access denied!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
SetStartDay();
SetSpecialFile();
ModifyLastLogin();
Activated = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show($"There is some problem with your activation license!!! Error:{ex.Message}", "Access denied!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}