2017-08-08 109 views
0

我需要C#控制台应用程序中的代码,在应用程序运行时保存输出数据,并且不会覆盖以前的数据,并且一行一行地连续保存如何在C#控制台应用程序中将数据写入文本文件而不覆盖当前数据

+0

嗨,你有没有想过在你寻求帮助之前自己尝试一些东西?如果是这样,告诉我们你已经得到的位置,我们将帮助你了解问题所在。 – kuskmen

+0

如果你也在这里发布你的代码,这很好,因为没有我们无法提供帮助。 –

回答

0

你必须使用StreamWriter类重载的构造: using (StreamWriter output = new StreamWriter(pathToFile, true)), 第二个布尔参数表示,如果文件应该被追加(true)或覆盖(false)。

0
using (System.IO.StreamWriter file = 
      new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt")) 
     { 
      foreach (string line in lines) 
      { 
       // If the line doesn't contain the word 'Second', write the line to the file. 
       if (!line.Contains("Second")) 
       { 
        file.WriteLine(line); 
       } 
      } 
1

您可以使用文件:

File.AppendAllText("test.txt", "mytext"+ Environment.NewLine); 

如果你有一个字符串列表,你可以这样做在一个太行。

List<string> lists = new List<string> {"111","222","333" }; 
    File.AppendAllLines("test.txt",lists); 
相关问题