使用WriteAllLines附加到文本文件

use*_*656 5 c# asp.net

我使用以下代码写入文本文件.我的问题是,每次执行以下代码时,它都会清空txt文件并创建一个新文件.有没有办法附加到这个txt文件?

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

Shr*_*abh 11

File.AppendAllLines 应该帮助你:

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


nun*_*cal 5

使用File.AppendAllLines.应该这样做

System.IO.File.AppendAllLines(
       HttpContext.Current.Server.MapPath("~/logger.txt"), 
       lines);
Run Code Online (Sandbox Code Playgroud)