本文介绍了文件被附加而不是被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码将一串数据写入文件。
如果要写出相同的文件名,我希望删除或覆盖该文件。发生了什么事情正在追加文件。
请查看以下代码片段。
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();
有人可以告诉我为什么要追加文件而不是覆盖?
解决方案
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?
解决方案
这篇关于文件被附加而不是被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!