I need to write to a very large text file by adding content to it as I interate over a very large set of records in c# 3.5. This file could be several GBs in size. I thought that I would use File.AppendAllText to write to the file after each record is processed. That way I don't keep the file in memory. However, will the AppendAllText read the entire contents of the file into memory when called or will it just add the new content to the end of it?
Apppending doesn't mean it loads the existing content, so yes you can use AppendAllText for this. Note, however, that a StreamWriter could also be used, perhaps more cleanly, to write gradually to a file (plus without having to keep open/close the file.
From the documentation for File.AppendAllText it looks like it does not read the file into memory.
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.
From http://msdn.microsoft.com/en-us/library/ms143356.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With