小编use*_*070的帖子

如何写入现有的txt文件c#

我创建了一些代码来创建一个带有初始文本的 txt 文件,但是当我尝试使用新的 msg 再次调用该方法时,它不会将其添加到 txt 文件中。下面是我的代码:

string example = "test";
WriteToLgo(example);

public static void WriteToLog(String inputtext)
{
   string location= @"C:\Users\";
   string NameOfFile = "test.txt";
   string fileName= String.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now, NameOfFile);
   string path= Path.Combine(location, fileName);
   using (StreamWriter sr= File.CreateText(path))
   {
      sr.WriteLine(inputtext);
   }
}
Run Code Online (Sandbox Code Playgroud)

如果我再次尝试调用该方法,则不会添加新的 msg。任何帮助将不胜感激。

c# streamwriter

1
推荐指数
1
解决办法
1696
查看次数

标签 统计

c# ×1

streamwriter ×1