Re: Append to the end of file when "write"

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Curious" <fir5tsight@xxxxxxxxx> wrote in message news:1192416922.311543.194130@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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

.



Relevant Pages

  • Re: Capture console output to file ?
    ... In article, Jeffrey Ross wrote: ... > If you want to append to these files rather than overwriting them use ... > If you want both stdout and stderr in the same file (noting that they may ...
    (linux.redhat)
  • Append to the end of file when "write"
    ... append the new content to the end of the file (instead of ... overwriting the current content). ... FileStream lStream = ... correct parameters. ...
    (microsoft.public.dotnet.general)
  • Re: WriteFile, insert data in the middle of the file.
    ... of the file without overwriting what's already there. ... What most programs do is create a temp file write everything they want to ... > all the data from the insertion point, and append my new data, and write ... Isn't this too inefficient? ...
    (microsoft.public.vc.mfc)
  • Re: path keeps bloating with repeated entries on invoking subshells
    ... I thought this was the right way to go since I only append to the ... systemwide path settings without overwriting anything as a user. ... duplicate entries (I guess a side-effect of appending the $path at the ...
    (comp.os.linux.misc)
  • Re: Append mode in write operation
    ... overwriting the current content). ... believe the append mode is defined with different parameters from the ... What are the correct parameters to define the "append" ...
    (microsoft.public.dotnet.general)