views:

82

answers:

6

Sorry all, but there was some other problem. The code is now working correct. Thanks all.


I have the following code for creating a file if it does not exist and overwriting it if it already exists:

FileStream fsExe = File.Create(DestExePath, 4096);
 BinaryWriter bw = new BinaryWriter(fsExe);

What ever I write to the BinaryWriter, it is getting appended to the "DestExePath" instead of overwriting the original file.

Anybody have any idea, why it is happening ?

A: 

Do you experience the same behaviour with this code?

using (FileStream fileStream = new FileStream(DestExePath, FileMode.Create))
{
    BinaryWriter bw = new BinaryWriter(fsExe);
}
Kane
I have already tried this, but still the file was getting appended.
Puneet Dudeja
It shouldn't be necessary to delete the file first... Running my own test confirmed that just using File.Create will remove the original contents of the file.
Vincent McNabb
A: 

If you keep the bw open, and keep writing, of course it will append.

If you want to replace the file, you have to close the file, open it again, and make a new binary writer.

Vincent McNabb
In these two lines, I am opening the file for the first time, BinaryWriter is opened for the first time, how can it be already open?
Puneet Dudeja
Whatever the contents of the file at the time should be replaced, according to the docs, and to my own test I just ran. Are you sure you're not keeping the file open and writing extra?
Vincent McNabb
A: 

This shouldn't happen according to the documentation:

"If the specified file does not exist, it is created; if it does exist and it is not read-only, the contents are overwritten."

Source: http://msdn.microsoft.com/en-us/library/d62kzs03.aspx

compie
A: 

running this work without issue

Sub Main()
    Dim fsExe = File.Create("c:\test.txt", 4096)
    Dim bw = New BinaryWriter(fsExe)
    bw.Write("this is a test 123")
    fsExe.Close()
End Sub

it does overwrite

Fredou
A: 

Sorry all, but there was some other problem. The code is now working correct. Thanks all.

Puneet Dudeja
Would be nice to know what the "some other problem" is! :-)
Vincent McNabb
.. and please accept your own answer with description too, so someone else may find this more useful in six months time. :)
sarnold
Or even better, use the [delete] link under the question.
Henk Holterman
Actually the problem is in no way related to this issue. I was creating a self extracting archive file and I needed to delete the same file I am writing to in the BinaryWriter before being added to the archive.
Puneet Dudeja
Definitely delete the question then, though with 6 answers you might not be able to.
ChrisF
+4  A: 

The reason this happens is because you never close the underlying stream and reuse the same binary writer to write to the file which will append of course. I would recommend you close the stream once you've finished writing to the file:

using (var stream = File.Create(DestExePath, 4096))
using (var writer = new BinaryWriter(stream))
{
    // Use the writer here to append to the file.
}
Darin Dimitrov