使用c#仅将日期存储到.txt文件一次 [英] Store Date to a .txt file only once using c#

查看:108
本文介绍了使用c#仅将日期存储到.txt文件一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class 计划
{
静态 void Main( string [] args)
{

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

尝试
{

// 创建新文件
Directory.CreateDirectory(Path.GetDirectoryName(fileName));

使用(StreamWriter sw = File.CreateText(fileName))
{

sw.WriteLine ( Thermo Licensing System file);
sw.WriteLine( ----------------- -------------------);
sw.WriteLine( 已安装日期:{0},DateTime.Now.ToString ());


DateTime newDate = DateTime.Now.AddDays( 30 );
DateTime date = DateTime.Now;
sw.WriteLine( 许可证在 + + newDate);

int numberOfDays = newDate.Subtract(date).Days;
sw.WriteLine( 剩余天数: + + numberOfDays.ToString());
sw.Close();


}

// 写入文件内容在控制台上。
使用(StreamReader sr = File.OpenText(fileName))
{
string s = ;
while ((s = sr.ReadLine())!= null
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
catch (Ex Ex)
{
Console.WriteLine(Ex.ToString( ));
}
}
}





输出(.txt文件)



Thermo许可系统文件

---------------------------- --------

安装日期:20-05-2014 16:01:42

许可证到期后20-06-2014 16:01:42

剩余天数





我写了上面的代码,将日期存储到.txt文件如图所示。我能够存储当前的日期和时间。我希望第一次执行应用程序时,日期应该只设置一次。



安装日期:设置为第一次执行应用程序的日期时间不应该改变而不管应用程序的执行时间



我正在尝试实施30天的许可。我希望当应用程序第一次执行时,执行应用程序的日期应存储到.txt文件中,不应更改,以便可以在此基础上计算剩余天数。我的主要目的是阻止用户在30天后使用我的应用程序。



我希望我的疑问是明确的,如果不是请让我知道



谢谢

解决方案





几个月前,我需要一个许可证系统。我开始知道这个帖子,它给了我很多帮助。



应用试用版制作工具 [ ^ ]



我最终得到了一个系统完全不同,但链接中的那个激动人心。



我的应用程序始终连接到互联网,其他方面,版本是演示。



如果您需要更多详细信息(如果您的应用也连接到互联网)请立即让我。



希望它有所帮助。


实际上,最简单的方法是在尝试写入时检查文件是否存在。如果它不存在你可以设置日期,否则你忽略写。



但是,我同意理查德在这里说这不是一个好方法。

查看此处 [ ^ ]进行一些阅读。



希望这有帮助。


保存文件后,您可以将保存的日期与当前日期进行比较,如果不匹配则停止使用代码。

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

解决方案

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.


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.


After saving the file , you may compare the saved date to the current date if that does not match then discontinue with the code.


这篇关于使用c#仅将日期存储到.txt文件一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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