如果没有,请创建一个.txt文件';t存在,并且如果它确实附加了一个新行

本文关键字:一个 如果 新行 txt 创建 文件 如果没有 存在 | 更新日期: 2023-09-27 18:29:11

我想创建一个.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#完全陌生。

如果没有,请创建一个.txt文件';t存在,并且如果它确实附加了一个新行

使用正确的构造函数:

else if (File.Exists(path))
{
    using(var sw = new StreamWriter(path, true))
    {
        sw.WriteLine("The next line!");
    }
}
string path = @"E:'AppServ'Example.txt";
File.AppendAllLines(path, new [] { "The very first line!" });

另请参阅File.AppendAllText()。AppendAllLines将在每行中添加一行新行,而不必自己放在那里。

如果文件不存在,这两种方法都会创建该文件,所以你不必这样做。

  • 文件附件全部文本
  • 文件附件所有行
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!");
   }
}

您实际上不必检查文件是否存在,因为StreamWriter会帮您检查。如果您在追加模式下打开它,那么文件将被创建,如果它不存在,那么您将始终追加,并且永远不会重写。所以你的初次检查是多余的。

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

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

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

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

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

您只需使用File.AppendAllText()方法即可解决您的问题。如果"文件创建"不可用,此方法将负责打开和关闭文件。

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

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

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

试试这个。

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!");
    }
}

您可以使用FileStream。这为你做了所有的工作。

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

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

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

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

语法C#C++F#VB公共静态void AppendAllText(字符串路径,字符串内容)参数路径类型:System.String要将指定字符串附加到的文件。目录类型:System.String要附加到文件的字符串。

AppendAllText

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

.NET核心控制台应用程序:

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

      
请注意,AppendAllLines和AppendAllText方法只创建文件,而不创建路径。因此,如果您试图在";C: ''文件夹";,请确保此路径存在。
相关文章: