Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does File.AppendAllText close the file after the operation

Tags:

c#

file-io

Does the following close the file after the operation is performed? :

System.IO.File.AppendAllText(path, text);

A yes, no will suffice?

like image 470
JL. Avatar asked Dec 04 '09 10:12

JL.


2 Answers

Yes, it does.

If it didn't, there'd be no way of closing it afterwards as it doesn't return anything to dispose.

From the docs:

Given a string and a file path, this method opens the specified file, appends the string to the end of the file, and then closes the file.

The other utility methods (ReadAllText, WriteAllBytes etc) work the same way.

like image 108
Jon Skeet Avatar answered Oct 22 '22 15:10

Jon Skeet


This is the code of the method:

public static void AppendAllText(string path, string contents, Encoding encoding)
{
    using (StreamWriter writer = new StreamWriter(path, true, encoding))
    {
        writer.Write(contents);
    }
}

Therefore, yes.

like image 41
Marat Faskhiev Avatar answered Oct 22 '22 13:10

Marat Faskhiev