【问题标题】:StreamWriter.Write doesn't write to file; no exception thrownStreamWriter.Write 不写入文件;没有抛出异常
【发布时间】:2012-06-13 21:53:15
【问题描述】:

我的 C# 代码 (asp.net MVC)

StreamWriter tw = new StreamWriter("C:\\mycode\\myapp\\logs\\log.txt");

// write a line of text to the file
tw.Write("test");

文件已创建但为空。不会抛出异常。我以前从未见过这种情况,我被困在这里;我只需要写一些调试输出。

请指教。

【问题讨论】:

  • 以防万一它有助于人们有洞察力,什么语言? (大概是 C++ 或 C#)

标签: c# asp.net-mvc file-io streamwriter.write


【解决方案1】:

StreamWriter 默认是缓冲的,这意味着它在收到 Flush() 或 Close() 调用之前不会输出。

如果您愿意,您可以通过设置AutoFlush 属性来更改它。否则,就这样做:

StreamWriter tw = new StreamWriter("C:\\mycode\\myapp\\logs\\log.txt");

// write a line of text to the file
tw.Write("test");
tw.Close();  //or tw.Flush();

【讨论】:

  • 我遇到了同样的问题——即使一切都正确,我也无法弄清楚为什么它没有写入。不知道冲洗/关闭。我认为这是总体上最好的答案。 (我知道这是旧的)
【解决方案2】:

既没有冲洗,也没有关闭,也没有处置。 试试这个

using (StreamWriter tw = new StreamWriter(@"C:\mycode\myapp\logs\log.txt"))
{
    // write a line of text to the file
    tw.Write("test");
    tw.Flush();
}

或者我的喜好

using (FileStream fs = new FileStream( @"C:\mycode\myapp\logs\log.txt"
                                     , FileMode.OpenOrCreate
                                     , FileAccess.ReadWrite)           )
{
    StreamWriter tw = new StreamWriter(fs);
    tw.Write("test");
    tw.Flush();
}

【讨论】:

  • 您不需要在您的第一个代码段中运行“tw.Flush()”。 StreamWriter 实现 IDisposable 意味着当 using 块结束时,它会自动释放。
  • 同样在你的第二段代码中,我也会在 using 语句中创建 tw,所以我不必担心刷新/关闭它。
  • 只有在自动刷新时才会发生刷新,而当我开始大量使用这种模式时,它并不存在。只是我在过去养成的习惯一直困扰着我。
【解决方案3】:

您需要在完成写入后关闭或刷新 StreamWriter。

tw.Close();

tw.Flush();

但最好的做法是将输出代码包装在 using 语句中,因为 StreamWriter 实现了 IDisposable:

using (StreamWriter tw = new StreamWriter("C:\\mycode\\myapp\\logs\\log.txt")){
// write a line of text to the file
tw.Write("test");
}

【讨论】:

    【解决方案4】:

    使用

    System.IO.File.WriteAllText(@"path\te.txt", "text");
    

    【讨论】:

    • 这是最好的答案——如果你知道输出内容不会那么大..
    【解决方案5】:
    FileStream fs = new FileStream("d:\\demo.txt", FileMode.CreateNew,
                                   FileAccess.Write);
    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);
    int data;
    
    sw.Write("HelloWorld");
    
    sw.Close();
    fs.Close();
    
    • 问题是当 StreamWriter 对象是参照 FileStream 对象创建时,SW 对象将一直期待一些数据,直到 SW 对象关闭。
    • 所以在使用sw.Close();之后 您打开的文件将关闭并准备好显示输出。

    【讨论】:

      【解决方案6】:

      是的,在 VB.net 中,这不是必需的,但对于 CSharp,您似乎需要一个 Writer.Flush 调用来强制写入。当然 Writer.Close() 也会强制刷新。

      我们还可以设置 StreamWriter 实例的 AutoFlush 属性:

      sw.AutoFlush = true;
      // Gets or sets a value indicating whether the StreamWriter 
      // will flush its buffer to the underlying stream after every  
      // call to StreamWriter.Write.
      

      发件人:http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush(v=vs.110).aspx

      【讨论】:

        【解决方案7】:

        另一种选择

        FileStream mystream = new FileStream("C:\\mycode\\myapp\\logs\\log.txt",    
        FileMode.OpenOrCreate, FileAccess.Write);           
        StreamWriter tw = new StreamWriter(mystream); 
        tw.WriteLine("test");
        tw.close();
        

        【讨论】:

          【解决方案8】:

          尝试关闭文件或将\n添加到如

          tw.WriteLine("test");
          tw.Close();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多