如果不存在,则创建.txt文件,如果它确实附加了新行

时间:2012-03-28 12:39:58

标签: c# text-files

我想创建一个.txt文件并写入它,如果该文件已经存在,我只想添加更多行:

string path = @"E:\AppServ\Example.txt";
if (!File.Exists(path))
{
    File.Create(path);
    TextWriter tw = new StreamWriter(path);
    tw.WriteLine("The very first line!");
    tw.Close();
}
else if (File.Exists(path))
{
    TextWriter tw = new StreamWriter(path);
    tw.WriteLine("The next line!");
    tw.Close(); 
}

但第一行似乎总是被覆盖......我怎么能避免在同一行写作(我在循环中使用它)?

我知道这很简单,但之前从未使用过WriteLine方法。我对C#完全不熟悉。

14 个答案:

答案 0 :(得分:147)

使用correct constructor

else if (File.Exists(path))
{
    using(var tw = new StreamWriter(path, true))
    {
        tw.WriteLine("The next line!");
    }
}

答案 1 :(得分:54)

string path = @"E:\AppServ\Example.txt";
File.AppendAllLines(path, new [] { "The very first line!" });

另请参见File.AppendAllText()。 AppendAllLines将为每一行添加换行符,而不必自己将其放在那里。

如果文件不存在,两种方法都会创建文件,因此您不必这样做。

答案 2 :(得分:34)

string path=@"E:\AppServ\Example.txt";

if(!File.Exists(path))
{
   File.Create(path).Dispose();

   using( TextWriter tw = new StreamWriter(path))
   {
      tw.WriteLine("The very first line!");
   }

}    
else if (File.Exists(path))
{
   using(TextWriter tw = new StreamWriter(path))
   {
      tw.WriteLine("The next line!");
   }
}

答案 3 :(得分:16)

您实际上不必检查文件是否存在,因为StreamWriter会为您执行此操作。如果在append-mode中打开它,如果文件不存在则会创建该文件,然后您将始终追加并且永远不会过度写入。所以你的初步检查是多余的。

TextWriter tw = new StreamWriter(path, true);
tw.WriteLine("The next line!");
tw.Close(); 

答案 4 :(得分:6)

File.AppendAllText将字符串添加到文件中。如果文件不存在,它还会创建一个文本文件。如果您不需要阅读内容,那就非常有效。用例是记录。

File.AppendAllText("C:\\log.txt", "hello world\n");

答案 5 :(得分:5)

您只想以“追加”模式打开文件。

http://msdn.microsoft.com/en-us/library/3zc0w663.aspx

答案 6 :(得分:4)

 else if (File.Exists(path)) 
{ 
  using (StreamWriter w = File.AppendText(path))
        {
            w.WriteLine("The next line!"); 
            w.Close();
        }
 } 

答案 7 :(得分:3)

当您启动StreamWriter时,它会覆盖之前的文本。你可以像这样使用append属性:

TextWriter t = new StreamWriter(path, true);

答案 8 :(得分:2)

您可以使用FileStream。这为你完成了所有工作。

http://www.csharp-examples.net/filestream-open-file/

答案 9 :(得分:1)

从microsoft文档中,您可以创建文件(如果不存在)并在单个调用中附加到该文件 File.AppendAllText方法(字符串,字符串)

.NET Framework(当前版本)其他版本

打开文件,将指定的字符串附加到文件,然后关闭该文件。如果该文件不存在,则此方法创建文件,将指定的字符串写入文件,然后关闭该文件。 命名空间:System.IO 汇编:mscorlib(在mscorlib.dll中)

语法 C#C ++ F#VB public static void AppendAllText(     字符串路径,     字符串内容 ) 参数 路径 键入:System.String 要将指定字符串附加到的文件。 内容 键入:System.String 要附加到文件的字符串。

AppendAllText

答案 10 :(得分:1)

using(var tw = new StreamWriter(path, File.Exists(path)))
{
    tw.WriteLine(message);
}

答案 11 :(得分:1)

尝试一下。

string path = @"E:\AppServ\Example.txt";
if (!File.Exists(path))
{
    using (var txtFile = File.AppendText(path))
    {
        txtFile.WriteLine("The very first line!");
    }
}
else if (File.Exists(path))
{     
    using (var txtFile = File.AppendText(path))
    {
        txtFile.WriteLine("The next line!");
    }
}

答案 12 :(得分:1)

您可以仅使用File.AppendAllText()方法来解决您的问题。 如果不可用,此方法将负责“文件创建”,打开和关闭文件。

var outputPath = @"E:\Example.txt";
var data = "Example Data";
File.AppendAllText(outputPath, data);

答案 13 :(得分:0)

.NET Core 控制台应用程序:

public static string RootDir() => Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, @"..\..\..\"));


string _OutputPath = RootDir() + "\\Output\\" + "MyFile.txt";
if (!File.Exists(_OutputPath))
    File.Create(_OutputPath).Dispose();
using (TextWriter _StreamWriter = new StreamWriter(_OutputPath))
{
    _StreamWriter.WriteLine(strOriginalText);
}


      
相关问题