0

How would I replace the text in a file with c#. I can write to the file, but if I write to the file a second time it appends the new data to the old rather than replace it. Here is a copy of what I'm using

fsFile = new FileStream("test.txt",FileMode.Open);
StreamWriter swOut = new StreamWriter(fsFile);
swOut.Write(strOut);
swOut.Close();

The output looks like this:
TestTestTestTestTest

And I want it to look like this:
Test
Any suggestions?

3
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by DenisOxon
0

Hi,

I'm not C expert but you need to open file as New (C may call it something else, but principle is correct), not Open.

Denis

0

Denis is correct. Use FileMode.Create.
With this mode, if the file doesn't exist it is created, or else overwritten (I believe this is what you want).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.