Append to the end of file when "write"

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

I want to write to an output file, "C:\\temp\\debug.txt": If the file
exists, append the new content to the end of the file (instead of
overwriting the current content). If the file doesn't exist, create
the file and write from the beginning.

Therefore, I want to code this like below:

FileInfo lFile = new FileInfo("C:\\temp\\debug.txt");

if (lFile.Exists)
{
// What's the syntax to append new content (instead of
overwriting the existing content?
FileStream lStream =
lFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);

}
else
{
FileStream lStream =
lFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
}

I believe the append mode is defined at the "lFile.Open" line with the
correct parameters. Thanks!
 
Curious said:
I want to write to an output file, "C:\\temp\\debug.txt": If the file
exists, append the new content to the end of the file (instead of
overwriting the current content). If the file doesn't exist, create
the file and write from the beginning.

Therefore, I want to code this like below:

FileInfo lFile = new FileInfo("C:\\temp\\debug.txt");

if (lFile.Exists)
{
// What's the syntax to append new content (instead of
overwriting the existing content?
FileStream lStream =
lFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);

}
else
{
FileStream lStream =
lFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
}

I believe the append mode is defined at the "lFile.Open" line with the
correct parameters. Thanks!


The FileInfo class exposes an AppendText method, which you may find useful:

http://msdn2.microsoft.com/en-us/library/system.io.fileinfo.appendtext(VS.80).aspx
 
Back
Top