Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
C#
namespace Thermocoders_Licensing_System
{
  class TrialTimeManager
  {
     private string temp = "";

      public TrialTimeManager() { }

  public void SetNewDate()
  {
    DateTime newDate = DateTime.Now.AddDays(31);
    temp = newDate.ToLongDateString();
    StoreDate(temp);
  }

public void Expired()
{
    string d = "";
    using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyApp"))
    {
        d = (String)key.GetValue("Date");
    }
    DateTime now = DateTime.Parse(d);
    int day = (now.Subtract(DateTime.Now)).Days;
    if (day > 30) { }
    else if (0 < day && day <= 30)
    {
        string daysLeft = string.Format("{0} days more to expire", now.Subtract(DateTime.Now).Days);
        MessageBox.Show(daysLeft);

    }
    else if (day <= 0)
    {
        MessageBox.Show("Trial Expired Please Purchase The Product");
    }
}

public void StoreDate(string value)
{
    try
    {
        using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MyApp"))
        {
            key.SetValue("Date", value, Microsoft.Win32.RegistryValueKind.String);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

}

}




Hi Everyone,

I found the above code from code project.It was working fine but there was an issue while storing and retrieving date information from the registry.For eg If I run the application say on 1st May 2014,the expiration date is correctly set to 1st june 2014.

The above code was working fine and it properly displayed the date when I changed my system time say If I change the date to 2nd may 2014 ,it would display 29days remaining.Now what is happening is that whenever I set the date say from 1st may 2014 to 1st june 2014, instead of throwing an error saying "Trial Period has expired" , it keeps saying 30 days remaining

And when I set the date from 1st may to 1st june,The date stored in the registry keeps changing and it sets the expiration date 30days ahead of the current date which should not be happening. What could be the error?

Can Anyone help me fix this?

Thanks
Posted
Comments
[no name] 8-May-14 8:25am    
Probably because you are calling SetNewDate from somewhere when you do not want to set the new date. As a side note, you do realize that someone can break your scheme in about 10 seconds or less right?
vivek murli 8-May-14 8:28am    
@Wes Aday Thanks for replying.Yes I guess anyone can break this code.So I plan to make it more sophisticated which I will deal with later.Now the problem here is that the date in the registry keeps updating and my application displays 30days remaining.What part of the code should I modify?

1 solution

First off, don't use the registry.
The registry in Vista and above need admin access to modify, so every time you call SetNewDate you app will require UAE access, and this can only get more rigid in future versions.

Instead, create a licence file and store it somewhere more accessible: Where should I store my data?[^] explains better locations.

I'd look at what you stored myself: start with putting a breakpoint in the StoreDate method and see how often it is called, and with what values.
 
Share this answer
 
Comments
vivek murli 8-May-14 8:43am    
Thanks OriginalGriff.
OriginalGriff 8-May-14 8:46am    
You're welcome!
vivek murli 8-May-14 14:24pm    
Hey I read your article @OriginalGriff and its a good idea to store information in files at accessible locations.Only One issue that I had is how to make a license file and encrypt it?
OriginalGriff 8-May-14 14:28pm    
http://msdn.microsoft.com/en-gb/library/system.security.cryptography.aspx

You'll probably want to use some pc specific info as a salt for the licence file.
vivek murli 8-May-14 14:29pm    
Yes @OriginalGriff you're right.I want to make the license computer specific that is 1 key per machine.Thanks for the quick reply :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900