1
namespace SimpleLicense
{
class Program
{
    static void Main(string[] args)
    {

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

        try
        {
            // Check if file already exists. If yes, delete it. 
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            // 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(31);
                sw.WriteLine("License Expires After"+" "+newDate);

                sw.WriteLine("Number of Days Remaining  ");
                sw.Close();

               // sw.WriteLine("Add ");
               // sw.WriteLine("Done! ");
            }

            // 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());
        }
    }
  }
}

Contents of .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

Hi Everyone,

I have written the above code to store date and time information to a .txt file as given above.I want that the information about the remaining days be stored in .txt file.As you can see today's date is 20-5-2014 and the license expires on 20-6-2014.So 30 should be printed next to Number Of Days Remaining.

If the user changes the system clock,and changes the date to say 21-5-2014, then 29Days should be printed next to Number of Days remaining

I also want that when the application is executed on a particular date, the date should be set to that date ie at installed date and should not change and the remaining days be calculated on the basis of Installed date

Can anyone help me to figure this out?

Thanks

user2614235
  • 161
  • 3
  • 7
  • 21

3 Answers3

2

This code will give you the number of days:

int numberOfDays = newDate.Subtract(DateTime.Now).Days;
sw.WriteLine("Number of Days Remaining: " + numberOfDays.ToString());
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • Thanks.Your Code works. Can you tell me the code so that Installed Date remains the same ie if I change the date from today 20-5-2014 to 20-6-2014,the date should be set to 20-5-2014 – user2614235 May 20 '14 at 11:08
  • 1
    You can always create an instance of DateTime. Something like this: DateTime date = new DateTime(2014,5,20); and then: int numberOfDays = newDate.Subtract(date).Days; will give you the correct output. – Amir Popovich May 20 '14 at 11:10
  • Thanks. DateTime date = new DateTime(2014,5,20); the date could be anything apart from 2014,5,20).So what to do in such a case? – user2614235 May 20 '14 at 11:17
  • 1
    I dont understand you. You have the "newDate" which is dynamic. You want to check the number of dates between X and newDate. Now what is X? It could be now(DateTime.Now) or it could be any other date time(for example: Datetime date = new DateTime(2014,5,12);). You then subtract the newDate from the date and retrieve the number of days that passed. – Amir Popovich May 20 '14 at 11:20
  • Thanks and you're right Sir.But my doubt was that once I execute the application for the first time ,Installed Date: 20-05-2014 16:01:42 should be set to that particular Date and should not change even if I tamper with the system clock and try changing the date and the remaining days should be calculated on the basis of Installed Date.I hope my Doubt is clear – user2614235 May 20 '14 at 11:31
  • So you should save that date in the datebase\file etc. and dont let anyone change it. – Amir Popovich May 20 '14 at 11:39
  • I am saving the date to .txt file and Planning to encrypt it,for that the program that I have written has to save the date properly to that text file – user2614235 May 20 '14 at 11:43
0

you can used below menioned code

var Remainingdays=(LicenseExpires-InstalledDate).TotalDays

and if you want in int days then

var Remainingdays=(LicenseExpires-InstalledDate).Days
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
0

I assume you want to do time limited version of software?

While code posted in previous answers is technically correct, you probably shouldn't use DateTime.Now as user can tamper with system clock and circumvent your measure. Get current date from another source, like:

http://www.worldtimeserver.com/

This has its disadvantages though, like increased startup time and what if a page or user's connection is down?

Better and easier solution would be to forget about time limitation and limit amount of times application can be started instead. Then you simply load number on opening program and write decreased one on closing.

Also, storing relevant values as plaintext (with helpful user-friendly descriptions no less!) is probably not a good idea as any halfway savvy user may just snoop through files and edit them in any text editor. You may want to use some kind of encryption algorithm like AES:

http://msdn.microsoft.com/pl-pl/library/system.security.cryptography.aes%28v=vs.110%29.aspx

user3613916
  • 232
  • 1
  • 10
  • I was focusing on offline licensing without the user having to connect to a server – user2614235 May 20 '14 at 11:37
  • 1
    You cannot enforce that the date set locally on a system clock is actually correct, hence you have to go online. While offline, you can make tampering a little harder, though still possible: http://stackoverflow.com/questions/6328052/how-to-prevent-usage-of-expired-license-through-system-clock-tampering, see Ira Baxter's response. But I still think doing it by amount of startups is much easier to do and more reliable, should you not store the number as plaintext. – user3613916 May 20 '14 at 11:45