Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
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 12-Feb-13 9:58am
Edited 12-Feb-13 10:02am
v3
Comments
Sergey Alexandrovich Kryukov at 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 at 12-Feb-13 16:35pm
   
You are welcome.
Will you then accept the answer(s) formally (green button)? — thanks.
—SA
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Marcus Kramer at 12-Feb-13 15:28pm
   
+5. Exactly.
Sergey Alexandrovich Kryukov at 12-Feb-13 16:32pm
   
Thank you, Marcus.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I don't think it will make any real difference, but I would streamline that a little:
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 | :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
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.)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100