Click here to Skip to main content
15,896,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code writes out a string of data to a file.
If the same file name is to be written out, I would like the file to be deleted or overwritten. What is is happening is the file is being appended.

Please look at the snippet of code below.
C#
if (!Directory.Exists(@"C:\LABREPORTS\" + patnumber))
{
    Directory.CreateDirectory(@"C:\LABREPORTS\" + patnumber);
}
if (File.Exists("c:\\LABREPORTS\\" + patnumber + "\\" + orderno + ".txt"))
{
    File.Delete("c:\\LABREPORTS\\" + patnumber + "\\" + orderno + ".txt");
}
StreamWriter writer = new StreamWriter(new FileStream("c:\\LABREPORTS\\" + patnumber + "\\" + orderno + ".txt",FileMode.Create,FileAccess.Write ));
writer.Write(report.ToString());
writer.Flush();
writer.Dispose();
writer.Close();


Can someone please tell me why the file is being appended to instead of overwritten?
Posted
Updated 12-Feb-13 9:02am
v3
Comments
Sergey Alexandrovich Kryukov 12-Feb-13 16:35pm    
[OP commented:]

Problem Solved.

I verified that the file was not being appended. There was a row counter in my program that I was not resetting to 0 so the arrays just kept growing.

I will however, take the advice provided in the two solutions to code the streamwriter in a better fashion.

Thank you.
Sergey Alexandrovich Kryukov 12-Feb-13 16:35pm    
You are welcome.
Will you then accept the answer(s) formally (green button)? — thanks.
—SA
Sergey Alexandrovich Kryukov 12-Feb-13 16:36pm    
And please, don't post your comments as solution. I removed your Solution 3, as it is not a solution. Please add comments to any posts, reply existing comments or use "Improve question" instead.
—SA

I don't think it will make any real difference, but I would streamline that a little:
C#
string folder = @"C:\LABREPORTS\" + patnumber;
string file = string.Format(@"{0}\{1}.txt", folder, orderno):
if (!Directory.Exists(folder))
    {
    Directory.CreateDirectory(folder);
    }
File.Delete(file);
using (StreamWriter writer = new StreamWriter(file, false))
    {
    writer.Write(report.ToString());
    }
The File.Delete documentation says it doesn't throw an exception if the file doesn't exist, so why check? You shouldn't need to delete it either, but to be sure...:laugh:
The StreamWriter has an overload that takes the path and a "Append" option, so that is a little clearer.
If you are just writing the one set of info to the file, I would use
C#
File.Delete(file);
File.WriteAllText(report.ToString());


But I can't see anything absolutely wrong with your code - are you sure it is being appended? I would append the data and the folder/file names to a log file to be sure I was writing the correct file as well, (or use the debugger to check the file immediately before the stream writer creation and again after the dispose.)
 
Share this answer
 
Look more thoroughly at documentation — it's simper than that, as you are not setting anything special. Here is what you should better use:
http://msdn.microsoft.com/en-us/library/36b035cb.aspx[^].

The second parameters defines if the data is appended; otherwise the file is overridden. The stream instance is created internally; you don't have to do that. Don't forget to use the StreamWriter instance under the using statement. This is shown in the code sample. You are not doing it, but you should. This way, you make sure that the instance is disposed even if exception is thrown insider this statement block. Among other things, it guarantees closed file buffers and files.

—SA
 
Share this answer
 
Comments
fjdiewornncalwe 12-Feb-13 15:28pm    
+5. Exactly.
Sergey Alexandrovich Kryukov 12-Feb-13 16:32pm    
Thank you, Marcus.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900