C# 如果不存在,则创建一个 .txt 文件,如果它确实附加了一个新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9907682/
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
Create a .txt file if doesn't exist, and if it does append a new line
提问by Berker Yüceer
I would like to create a .txt file and write to it, and if the file already exists I just want to append some more lines:
我想创建一个 .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();
}
But the first line seems to always get overwritten... how can I avoid writing on the same line (I'm using this in a loop)?
但是第一行似乎总是被覆盖......我怎样才能避免在同一行上写(我在循环中使用它)?
I know it's a pretty simple thing, but I never used the WriteLinemethod before. I'm totally new to C#.
我知道这是一件非常简单的事情,但我以前从未使用过这种WriteLine方法。我对 C# 完全陌生。
采纳答案by Daniel Hilgarth
Use the correct constructor:
使用正确的构造函数:
else if (File.Exists(path))
{
using(var tw = new StreamWriter(path, true))
{
tw.WriteLine("The next line!");
}
}
回答by Ed Manet
You just want to open the file in "append" mode.
您只想以“追加”模式打开文件。
回答by Tom Ceuppens
You could use a FileStream. This does all the work for you.
您可以使用 FileStream。这将为您完成所有工作。
回答by Matan Shahar
When you start StreamWriter it's override the text was there before. You can use append property like so:
当您启动 StreamWriter 时,它会覆盖之前存在的文本。您可以像这样使用 append 属性:
TextWriter t = new StreamWriter(path, true);
回答by Smack
else if (File.Exists(path))
{
using (StreamWriter w = File.AppendText(path))
{
w.WriteLine("The next line!");
w.Close();
}
}
回答by drch
string path = @"E:\AppServ\Example.txt";
File.AppendAllLines(path, new [] { "The very first line!" });
See also File.AppendAllText(). AppendAllLines will add a newline to each line without having to put it there yourself.
另请参见 File.AppendAllText()。AppendAllLines 会为每一行添加一个换行符,而不必自己把它放在那里。
Both methods will create the file if it doesn't exist so you don't have to.
如果文件不存在,两种方法都将创建该文件,因此您不必这样做。
回答by Aek
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!");
}
}
回答by John Boling
You don't actually have to check if the file exists, as StreamWriter will do that for you. If you open it in append-mode, the file will be created if it does not exists, then you will always append and never over write. So your initial check is redundant.
您实际上不必检查文件是否存在,因为 StreamWriter 会为您执行此操作。如果您以追加模式打开它,如果文件不存在,则将创建该文件,然后您将始终追加并且永远不会覆盖。所以你的初始检查是多余的。
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine("The next line!");
tw.Close();
回答by David Fawzy
From microsoft documentation, you can create file if not exist and append to it in a single call File.AppendAllText Method (String,?String)
从微软文档中,如果文件不存在,您可以创建文件并在一次调用 File.AppendAllText Method (String,?String) 中附加到它
.NET Framework (current version) Other Versions
.NET Framework(当前版本)其他版本
Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file. Namespace: System.IO Assembly: mscorlib (in mscorlib.dll)
打开文件,将指定的字符串附加到文件,然后关闭文件。如果文件不存在,此方法会创建一个文件,将指定的字符串写入文件,然后关闭文件。命名空间:System.IO 程序集:mscorlib(在 mscorlib.dll 中)
Syntax C#C++F#VB public static void AppendAllText( string path, string contents ) Parameters path Type: System.String The file to append the specified string to. contents Type: System.String The string to append to the file.
语法 C#C++F#VB public static void AppendAllText( string path, string contents ) 参数 path 类型:System.String 将指定字符串追加到的文件。contents 类型:System.String 要附加到文件的字符串。
回答by R.Cha
File.AppendAllText adds a string to a file. It also creates a text file if the file does not exist. If you don't need to read content, it's very efficient. The use case is logging.
File.AppendAllText 将字符串添加到文件中。如果文件不存在,它还会创建一个文本文件。如果您不需要阅读内容,则非常高效。用例是日志记录。
File.AppendAllText("C:\log.txt", "hello world\n");

