最近四天附加文本文件

时间:2014-01-13 04:45:16

标签: c# parsing text-files

我有一个小程序,可以创建一个简单的文本文件,如下所示:

 string filename = "recdata.txt";

 using (StreamWriter sw = new StreamWriter(File.Open(filename, FileMode.Append)))
                {
                    string cmdTxt = String.Format("{0},{1},{3}", CustomerName, CustomerPhone, TodayDateTime));
                    sw.WriteLine(cmdTxt);
                }

我想要做的是始终保留此文件中最后四天的数据。例如,文件的内容如下:

Four days recdata.txt file today (01/12/2014) is (please notice that on Jan 10 there were no data. Some days can or cannot contain data:

John Powell, 12154584435,01092014 10:30:27.33
Carol Powell, 12154584215,01092014 10:32:27.33
Peter Smith, 12154541411,01092014 10:34:27.33
John Powell, 12154584435,01112014 11:30:07.03
Albert Smith, 12124252211,01122014 07:33:02.22
Humphrey Powell, 17183544110,01122014 08:10:32.14
Jason Smith, 17183994741,01122014 10:00:07.01
Oliver Powell, 12123664435,01122014 10:11:21.21

Four Days recdata.txt file tomorrow should be (01/13/2014) :

John Powell, 12154584435,01112014 11:30:07.03
Albert Smith, 12124252211,01122014 07:33:02.22
Humphrey Powell, 17183544110,01122014 08:10:32.14
Jason Smith, 17183994741,01122014 10:00:07.01
Oliver Powell, 12123664435,01122014 10:11:21.21
Jimmy Memphis, 12142544111,01132014 14:15:10.01

每天,此文件将自动通过ftp发送,并将recdata.txt复制到存档文件夹,例如: recdata_01132014.txt

我可以使用File.GetCreationTime检查文件的创建日期,但是我是否需要每天读取整个文件,然后从文件中删除一天或者有更好的方法? recdata.txt中的数据不存在于归档文件夹中复制文件的任何其他位置。任何想法都会很高兴地感谢它。

0 个答案:

没有答案