Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class Program
{
    static void Main(string[] args)
    {

        string fileName = @"C:\\Temp\\test.txt";

        try
        {

            // Create a new file 
            Directory.CreateDirectory(Path.GetDirectoryName(fileName));

            using (StreamWriter sw = File.CreateText(fileName))
            {

                sw.WriteLine("Thermo Licensing System file");
                sw.WriteLine("------------------------------------");
                sw.WriteLine("Installed Date: {0}", DateTime.Now.ToString());


                DateTime newDate = DateTime.Now.AddDays(30);
                DateTime date = DateTime.Now;
                sw.WriteLine("License Expires After"+" "+newDate);

                int numberOfDays = newDate.Subtract(date).Days;
                sw.WriteLine("Number of Days Remaining: " + "  " + numberOfDays.ToString());
                sw.Close();


            }

            // Write file contents on console. 
            using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
                     Console.ReadLine();
            }
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex.ToString());
        }
    }
}


Output (.txt file)

Thermo Licensing System file
------------------------------------
Installed Date: 20-05-2014 16:01:42
License Expires After 20-06-2014 16:01:42
Number Of Days Remaining


I have written the above code that stores the date to a .txt file as shown. I am able to store the current date and time. I want that the date should be set only once when the application is executed for the first time.

Installed Date: Set to the date when the application is executed the first time and should not change irrespective of how many time the application is executed

I am trying to implement 30Days licensing. I want that when the application is executed for the very first time, the date when he executed the application should be stored into the .txt file and should not change, so that the remaining days could be calculated on the basis of that. My main aim is to stop the user from using my application after 30 days.

I hope my doubt is clear if not please let me know

Thanks
Posted
Comments
Richard MacCutchan 26-May-14 4:06am    
Well this is not going to work, since the user just needs to load that file into Notepad and edit the data. You need to look into proper secure licencing for your product; Google will help.
vivek murli 26-May-14 4:08am    
@Richard MacCutchan I googled but couldn't find a solution.I will encrypt the .txt file so that the user does not tamper with it.
Richard MacCutchan 26-May-14 6:56am    
It's still not a secure method, .NET licencing is much more complex than that. And unless your software is very valuable and expensive you are probably wasting your time.

Hi,

Fews months ago, i needed a licence system. i started witn this thread, it helpd me a lot.

Application Trial Maker[^]

I ended up with a system totaly diffrent, yet inspierd from the one in the link.

My application is always connected to internet, other wise, the version is demo.

If you need more details (in case your app is also connected to internet) don't hesitate to let me now.

Hope it helps.
 
Share this answer
 
Comments
vivek murli 26-May-14 4:34am    
Hi Ziee-M.I am a new developer and not that good when it comes to programming.I checked application trial maker link but was not able to understand anything.I am focusing on offline licensing at the moment ie without an internet connection
Actually the simplest way is to check if the file exists or not when attempting a write. If it does not exist you can set the date, else you ignore the write.

However, I agree with Richard here that this is not the good way to go.
Check here[^] for some reading.

Hope this helps.
 
Share this answer
 
Comments
vivek murli 26-May-14 4:39am    
V. Thanks.What would you suggest? because if I store the details to the registry ,it could be a problem
[no name] 26-May-14 5:41am    
The problem is, no matter what you do, your scheme is not going to work. You are putting a bunch of time and effort into something that is going to get broken in about 30 seconds or less. You need to do what Richard suggested and get a real licensing system if you think your software is valuable enough that you need to protect it.
V. 26-May-14 5:55am    
Think about the maximum security you'ld like. If you create an ecrypted file, your average user won't be able to handle it, but it is surprisingly easy to backwards engineer an application and derive the license system. So what you'ld like is that your application and your licensing system are independant.
After saving the file , you may compare the saved date to the current date if that does not match then discontinue with the code.
 
Share this answer
 

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