文件被附加而不是被覆盖 [英] file gets appended instead of overwritten

查看:76
本文介绍了文件被附加而不是被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码将一串数据写入文件。

如果要写出相同的文件名,我希望删除或覆盖该文件。发生了什么事情正在追加文件。



请查看以下代码片段。

  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();





有人可以告诉我为什么要追加文件而不是覆盖?

解决方案

更全面地了解文档 - 它比这更简单,因为你没有设置任何特别的东西。以下是您应该更好地使用的内容:

http://msdn.microsoft。 com / en-us / library / 36b035cb.aspx [ ^ ]。



第二个参数定义是否附加数据;否则文件被覆盖。流实例在内部创建;你不必这样做。不要忘记在下使用语句使用 StreamWriter 实例。这在代码示例中显示。你没有这样做,但你应该这样做。这样,即使在此语句块内部抛出异常,也要确保实例处置。除此之外,它还保证关闭文件缓冲区和文件。



-SA


我不知道我认为它会产生任何真正的不同,但我会简化一点:

  string  folder =  @  C:\ LABREPORTS \ + patnumber; 
string file = string .Format( @ {0} \ {1} .txt,文件夹,orderno):
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
File.Delete(file);
使用(StreamWriter writer = new StreamWriter(file, false ))
{
writer.Write(report.ToString());
}

File.Delete文档说如果文件不存在,它不会抛出异常,为什么要检查?你也不需要删除它,但是要确定......:笑:

StreamWriter有一个带路径和附加选项的重载,所以这是一点点更清楚。

如果您只是将一组信息写入文件,我会使用

 File.Delete(file); 
File.WriteAllText(report.ToString());





但是我看不出你的任何问题。代码 - 你确定它被追加?我会将数据和文件夹/文件名附加到日志文件中,以确保我也正在编写​​正确的文件,(或者使用调试器在创建流编写器之前立即检查文件,并在处理之后再次检查文件。)


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?

解决方案

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


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:
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.)


这篇关于文件被附加而不是被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆