文本文件写入和附加数据 [英] text file writing in and appending data

查看:466
本文介绍了文本文件写入和附加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用vb.net中的datagridview制作员工从一个数据库转移到另一个数据库的程序,我能够更新并将转移员工插入其他数据库,但............. ..



现在,我想把这些传输记录,即EmpId,EmpName,TransferFrom和TransferTo字段写入transfer.txt文件,但是在这里我做了一些修改,我的意思是,我使用(当前日期和.txt)创建文件名,如dd-mm-yyyy(01-04-2013)。如果我今天转移员工并且文件不在文件位置退出,那么它将创建并写入所有值,如果文件存在于文件位置,则将新转移的员工附加到该文件....

I make a program for employee transfers from one database to other, using datagridview in vb.net, I am capable to update and insert transfer employee to other database, but...............

Now, I want to write those transfer record i.e. EmpId, EmpName, TransferFrom and TransferTo fields to in transfer.txt file, but here I make some changes, I mean, I make filename with (current date & .txt) like as dd-mm-yyyy (01-04-2013). If I transfer Employee today and file not exits on file location then, it would be create and write all values and if file exist on file location then append new transfered employee to that file....

推荐答案

首先,反过来做你的日期文件名:

First off, do your date filename the other way round:
2012-04-01.txt

这样,它们会按日期自动排序,而不是按月份的所有月份排序,全月的第二个,依此类推 - 到最新和最旧的变得非常简单。



这很简单,你甚至不需要检查文件是否存在:

That way, they are automatically sorted by date rather than all the first of the month, all the second of the month, as so on - getting to the newest and oldest becomes extremely simple.

It''s pretty simple, you don''t even need to check if the file exists:

List<string> lines = new List<string>();
lines.Add("user 1, " + DateTime.Now);
lines.Add("user 2, " + DateTime.Now);
File.AppendAllLines(@"D:\Temp\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", lines);

各种File.AppendXXX方法创建文件(如果它不存在),并添加数据(如果存在)。

The various File.AppendXXX methods create the file if it doesn''t exist, and add the data if it does.


这篇关于文本文件写入和附加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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