Append to the end of file when "write"

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!
 
P

PvdG42

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top