File.WriteAllText() does not overwrite?

3,045 views
Skip to first unread message

Benj Nunez

unread,
Oct 5, 2009, 10:09:11 PM10/5/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hello everyone,


I would just like to share with you my observation with the use of the
File.WriteAllText().
I have code like this:

public bool dumpToFile(ref StringBuilder AData, String AFileNameStr)
{
...
string AppPath = Application.StartupPath;

AppPath += "\\";
try
{
File.WriteAllText(@AppPath + AFileNameStr,
AData.ToString());
IsOk = true;
}
catch (System.ArgumentNullException)
{
throw;
}

...
}


I'm expecting that every time I run the dumpToFile() method it
overwrites the file
that already exists. Well, apparently in my case it did not (although
the documentation
says it can.).

Any comments on this? I'm aware that I can use StreamWriter as a
substitute. Your inputs
are welcome.





Processor Devil

unread,
Oct 6, 2009, 3:21:01 AM10/6/09
to dotnetde...@googlegroups.com
WriteAllText overwrites the file. Butyou are using StringBuilder and it looks like you are appending new and new text to the stringbuilder somewhere...
Just try to replace WriteAllText method with AppendAllText, I think your data will be duplicated each time you use it...

2009/10/6 Benj Nunez <benj...@gmail.com>

sitaram koundinya potula

unread,
Oct 6, 2009, 7:43:01 AM10/6/09
to dotnetde...@googlegroups.com
I think it has to overwrite the file.There should not be any problem.
According to me, the only way it can give an exception is, when the existed file is read-only

Thanks,
Ram.

--
S hrewd
 I mpartial
  T alented
    A lluring
     R esolute
       A ggressive
         M odern.

Mobile:-9885291971

Benj Nunez

unread,
Oct 6, 2009, 8:37:52 PM10/6/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hmm..that's strange. Because I'm expecting that regardless if I have
the same or different content in my StringBuilder, File.WriteAllText()
should overwrite the contents of the file it has created. On the first
pass (the first time the dumpToFile() is called), it creates the file.
On the second and succeeding calls, it should always overwrite the
file.





On Oct 6, 3:21 pm, Processor Devil <processor.de...@gmail.com> wrote:
> WriteAllText overwrites the file. Butyou are using StringBuilder and it
> looks like you are appending new and new text to the stringbuilder
> somewhere...
> Just try to replace WriteAllText method with AppendAllText, I think your
> data will be duplicated each time you use it...
>
> 2009/10/6 Benj Nunez <benjnu...@gmail.com>

Peter Smith

unread,
Oct 8, 2009, 1:23:47 PM10/8/09
to dotnetde...@googlegroups.com
On Mon, Oct 5, 2009 at 10:09 PM, Benj Nunez <benj...@gmail.com> wrote:

I'm expecting that every time I run the dumpToFile() method  it overwrites the file
that already exists. Well, apparently in my case it did not (although the documentation says it can.).

The documentation actually says it WILL overwrite a file that exists, not that it can. 'can' implies that there are times that it 'cannot'.

public static void WriteAllText(string path, string contents)
    Member of System.IO.File

Summary:
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

So I guess the question is, how did it 'apparently not' in your case. It did in all my test cases:
          http://dotnetdevelopment.pastebin.com/f17b0514b
I stepped thru each write, and after the debug text came up, I checked the file.








Benj Nunez

unread,
Oct 10, 2009, 6:04:16 AM10/10/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks for responding. I read your source code. My expected output is
that since
your code has overwritten the same file twice, it should return the
last line only:

"File #1 testing.Oooh, you make me live."


Btw, if I have a file Name that is not a const, would that make a
difference? I happen to
save the file with a the current date *for each type it encounters*
like this:


StringBuilder sb = new StringBuilder();
sb.Append("CVM for ").Append(AType).Append(" ").
Append(Utils.FormattedDate(DateTime.Now.ToString())).
Append(".txt");

try
{
IsOk = service.dumpToFile(ref AData, sb.ToString
()); // ouput is "cvm for <type> mmddyyyy.txt".
}
...

<type> denotes some status (e.g. "new entry", "new shipment",
"replacement", etc.).

The seconds part is not included because obviously each time you call
dumpToFile(), you'll be
creating a lot of *.txt files which I don't want. That's the reason
why I need the File.WriteAllText() to overwrite
whatever's in the file and replace it with the new one. Instead, it's
appending them. :(

Any tips?














On Oct 9, 1:23 am, Peter Smith <psmith.w...@gmail.com> wrote:
> On Mon, Oct 5, 2009 at 10:09 PM, Benj Nunez <benjnu...@gmail.com> wrote:
>
> > I'm expecting that every time I run the dumpToFile() method  it overwrites
> > the file
> > that already exists. Well, apparently in my case it did not (although the
> > documentation says it can.).
>
> The documentation actually says it WILL overwrite a file that exists, not
> that it can. 'can' implies that there are times that it 'cannot'.
>
> *public static void WriteAllText(string path, string contents)
>     Member of System.IO.File
>
> Summary:
> Creates a new file, writes the specified string to the file, and then closes
> the file. If the target file already exists, it is overwritten.
> *

Benj Nunez

unread,
Oct 11, 2009, 4:15:17 AM10/11/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Never mind. I figured it out already. It was the stringbuilder causing
the problem. I had to use its
.Remove() routine to remove the last collection used prior to sending
them off to dumpToFile().


Thanks for your help everyone!



Cheers!



Benj

Peter Smith

unread,
Oct 14, 2009, 7:00:51 PM10/14/09
to dotnetde...@googlegroups.com
Ah, so since my code (that I just deleted) used a 'new' StringBuilder each time, I didn't see your problems.

Yeah, I tend to avoid StringBuilder just because it has no easy .Clear method, and the lack of THAT makes me question the whole thing. Then again, I haven't written any serious string manip routines in C# yet.

-- Peter Smith

Processor Devil

unread,
Oct 15, 2009, 11:53:39 AM10/15/09
to dotnetde...@googlegroups.com
I am happy that you have already understood my first answer and pointing to the stringbuilder :)

2009/10/11 Benj Nunez <benj...@gmail.com>
Reply all
Reply to author
Forward
0 new messages