Click here to Skip to main content
16,017,307 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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(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());
            }
        }
    }
}


CSS
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 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
Posted
Comments
[no name] 20-May-14 8:19am    
Help you figure what out? I am not seeing where you have tried anything, asked a question or described a problem. Read the file, parse the dates, do your calculations, rewrite the file.

1 solution

check below documentation
http://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx[^]
http://msdn.microsoft.com/en-us/library/92e05ft3(v=vs.110).aspx[^]
http://msdn.microsoft.com/en-us/library/b873y76a.aspx[^]
http://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx[^]

steps:
read all lines from the given text file
get the line where you store install date
split by : and get the date part
parse it to date time
calculate the remaining days
replace the last line with new data
save all lines back to file
 
Share this answer
 
Comments
vivek murli 22-May-14 8:12am    
@DamithSL would you please help me in coding the above requirements?

Thanks

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