views:

285

answers:

2

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

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

A yes, no will suffice?

+4  A: 

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.

Jon Skeet
Thanks I was inclined to use a StreamWriter, but this seems so much easier.
JL
+2  A: 

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.

Unholy
I don't think it's a good idea to rely on the implementation details unless you absolutely have to. Implementations can change. Fortunately in this case it's explicitly documented.
Jon Skeet