Why does StreamWriter overwrite my file?
Solution 1
If you use FileMode.Create
, according to MSDN:
Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. This requires FileIOPermissionAccess.Write permission. FileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate. If the file already exists but is a hidden file, an UnauthorizedAccessException exception is thrown.
So you need to use FileMode.Append
instead, if you want to add content to the end of the file:
Opens the file if it exists and seeks to the end of the file, or creates a new file. This requires FileIOPermissionAccess.Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.
FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
Solution 2
To append to the file you can update the position of the stream to the end.
...
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
fs.Seek(0, SeekOrigin.End);
using (StreamWriter writer = new StreamWriter(fs))
{
// To append, update the stream's position to the end of the file
writer.WriteLine("test");
}
}
...
On a side note, if you are intending to overwrite the file, and the length of the original file is longer than the length of the new data you need to use SetLength() on the underlying file stream when you have finished writing out the file. In other words,
...
{
writer.WriteLine("test");
fs.SetLength(fs.Position);
}
...
Bxcb Sbdsb
Updated on June 10, 2022Comments
-
Bxcb Sbdsb almost 2 years
I'm using
SteamWriter
to write something to a text file, however its overwriting the old data with the new data. How can I make sure it will just add the new data instead of overwriting the old data?
my code:class Program { const string filename = @"C:\log.txt"; static void Main() { FileStream fs; fs = new FileStream(filename, FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(fs); writer.WriteLine("test"); writer.Close(); fs.Close(); } }
-
Steffan Donal over 11 yearsThe flush here is unnecessary - close flushes.
-
Steffan Donal over 11 yearsAlthough I do have to say I disagree with your method for opening a file. I would have used a
using(StreamWriter sW = new StreamWriter("FILE", true)){
-
Steffan Donal over 11 yearsNow that code is broken. Remove the two ifs inside the
using
and add a)
aftertrue)
-
Karthik over 11 years@Ruirize now? i dont have a IDE to test it :( sorry
-
Dai almost 7 yearsThe documentation is unclear if "it will be overwritten" means that existing bytes in the file will be preserved, just overwritten by each new write (i.e. overwriting current data from the start of the file) or if the entire file will be truncated before writing new data.
-
Brett Caswell over 5 yearsI think you mean
FileMode.OpenOrCreate
, asFileMode.Create
will overwrite the file, and yourfs.Seek
should be done before you create aStreamWriter
using theFileStream
-
Wes over 5 yearsThanks for pointing it out, I was following the OP's code so hadn't stopped to think about that! Cheers.