与WriteAllLines更新文本文件 [英] Update text file with WriteAllLines

查看:96
本文介绍了与WriteAllLines更新文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code在一个文本文件中写入。我的问题是,每一个下面code执行它时清空txt文件,并创建一个新的。有没有办法将追加到这个txt文件?

 的String [] =行{DateTime.Now.Date.ToShortDateString(),DateTime.Now.TimeOfDay.ToString(),消息类型,模块};
System.IO.File.WriteAllLines(HttpContext.Current.Server.MapPath(〜/ logger.txt),行);


解决方案

File.AppendAllLines 应该可以帮助您:

 的String [] =行{DateTime.Now.Date.ToShortDateString(),DateTime.Now.TimeOfDay.ToString(),消息类型,模块};
System.IO.File.AppendAllLines(HttpContext.Current.Server.MapPath(〜/ logger.txt),行);

I am using the following code to write in a text file. My problem is that every time the following code is executed it empties the txt file and create a new one. Is there a way to append to this txt file?.

string[] lines = {DateTime.Now.Date.ToShortDateString(),DateTime.Now.TimeOfDay.ToString(), message, type, module };
System.IO.File.WriteAllLines(HttpContext.Current.Server.MapPath("~/logger.txt"), lines);

解决方案

File.AppendAllLines should help you:

string[] lines = {DateTime.Now.Date.ToShortDateString(),DateTime.Now.TimeOfDay.ToString(), message, type, module };
System.IO.File.AppendAllLines(HttpContext.Current.Server.MapPath("~/logger.txt"), lines);

这篇关于与WriteAllLines更新文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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