C# 使用 WriteAllLines 附加到文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15379136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:34:32  来源:igfitidea点击:

Append to a text file using WriteAllLines

c#asp.net

提问by user1292656

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 creates a new one. Is there a way to append to this txt file?

我正在使用以下代码写入文本文件。我的问题是,每次执行以下代码时,它都会清空 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);

采纳答案by Shrivallabh

File.AppendAllLinesshould help you:

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);

回答by nunespascal

Use File.AppendAllLines. That should do it

使用File.AppendAllLines. 那应该这样做

System.IO.File.AppendAllLines(
       HttpContext.Current.Server.MapPath("~/logger.txt"), 
       lines);

回答by CodeGuru

Do something like this :

做这样的事情:

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

So if file is not exists it will create and write in file and if file exists it will append on file.

因此,如果文件不存在,它将创建并写入文件,如果文件存在,它将附加到文件中。

回答by j.s.banger

three function are available ..File.AppendAllLine ,FileAppendAllText and FileAppendtext..you can try as u like...

三个函数可用 ..File.AppendAllLine ,FileAppendAllText 和 FileAppendtext ..你可以试试看...

回答by Roman.Potapkin

Use

public static void AppendAllLines( string path, IEnumerable contents )

public static void AppendAllLines(字符串路径,IEnumerable内容)

回答by jacob aloysious

You could use StreamWriter; if the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.

您可以使用StreamWriter;如果文件存在,它可以被覆盖或附加到。如果文件不存在,则此构造函数创建一个新文件。

string[] lines = { DateTime.Now.Date.ToShortDateString(), DateTime.Now.TimeOfDay.ToString(), message, type, module };

using(StreamWriter streamWriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/logger.txt"), true))
{
    streamWriter.WriteLine(lines);
}

回答by Kurkula

In all the above cases I prefer to use using to make sure that open and close file options will be taken care of.

在上述所有情况下,我更喜欢使用 using 来确保打开和关闭文件选项将得到处理。