将日期永久设置为.txt文件,并使用c#计算基于该日期的剩余天数 [英] Set the date permanently to a .txt file and calculate remaining days based on that date using c#

查看:117
本文介绍了将日期永久设置为.txt文件,并使用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());
            }
        }
    }
}







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





大家好,



我已经写了上面的代码将日期和时间信息存储到上面给出的.txt文件中。



我想要的当申请在特定日期执行时,日期应设置为该日期,即在安装日期,不应更改,剩余天数应根据安装日期计算


任何人都可以帮我解决这个问题吗?



谢谢



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

推荐答案

检查下面文档

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 [<一个href =http://msdn.microsoft.com/en-us/library/b873y76a.aspxtarget =_ blanktitle =新窗口> ^ ]

< a href =http://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx> http://msdn.microsoft.com/en-us/ library / system.datetime.parse(v = vs.110).aspx [ ^ ]



步骤:

读取给定文本文件中的所有行

获取存储安装日期的行

拆分并获取日期部分

解析它到日期时间

计算剩余天数

用新数据替换最后一行

将所有行保存回文件
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


这篇关于将日期永久设置为.txt文件,并使用c#计算基于该日期的剩余天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆