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 <[email protected]> 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 <[email protected]> > > > > > 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.
