C# StreamWriter构造函数问题

C# StreamWriter构造函数问题,c#,buffer,streamwriter,C#,Buffer,Streamwriter,我正在尝试做一些非常简单的事情 在发现与StreamWriter(字符串路径)构造函数关联的默认buffersize对于我正在记录的信息来说太小时,我尝试使用以下构造函数: public StreamWriter( string path, bool append, Encoding encoding, int bufferSize ) 生成的日志文件完全为空-这更糟 注意:我最初的帖子引用了错误“error:trusted to read over end o

我正在尝试做一些非常简单的事情

在发现与StreamWriter(字符串路径)构造函数关联的默认buffersize对于我正在记录的信息来说太小时,我尝试使用以下构造函数:

public StreamWriter(
    string path,
    bool append,
    Encoding encoding,
    int bufferSize
)
生成的日志文件完全为空-这更糟

注意:我最初的帖子引用了错误“error:trusted to read over end of stream”,但这与该方法后面的功能有关,由于这个日志文件问题,我无法记录相关信息

以下是我的代码中的旧构造函数用法:

drawGameBoardLog = new StreamWriter(debugFileName);
这是一个新的构造函数,讽刺的是它让事情变得更糟:

drawGameBoardLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default, 65535);
我对此完全感到困惑

更新:更多详细信息:

这是我为其记录活动的方法的开始:

    public void DrawGameBoard()
    {
        StreamWriter drawGameBoardLog;
        bool debug = true;

        // Logging---------------------------------------------------------------------------------
        // Build timestamp string
        DateTime currentDateTime = DateTime.Now;
        string timeStampString = currentDateTime.ToString("ddMMyyyy_HHmmss");

        // Build filename, concat timestamp and .txt extension.
        string debugFileName = "D:\\Programming\\PacmanLogs\\DrawGameBoard"+timeStampString+".txt";

        // Create filestream and pass this to a stream writer, setting a nice big buffer.
        drawGameBoardLog = new StreamWriter(debugFileName, false, System.Text.Encoding.Default, 65535);
        //drawGameBoardLog = new StreamWriter(debugFileName);

        // Write to the file:
        drawGameBoardLog.WriteLine(DateTime.Now);
        drawGameBoardLog.WriteLine("timeStampString = {0}",timeStampString);
        // -------------------------------------------------------------------------------------------

        if(debug){drawGameBoardLog.WriteLine("DrawGameBoard()...");}
使用StreamWriter构造函数时甚至没有出现行“DrawGameBoard()…”,StreamWriter构造函数接受path、append、encoding和buffersize。而在我得到内容之前,文件大小达到了1K。这是到目前为止的日志文件(顺便说一下,我从图形编程开始):


默认缓冲区大小就在这里。

您没有关闭StreamWriter。在末尾类型:

drawGameBoardLog.Close()

或者使用
块使用

using(StreamWriter sw = new StreamWriter(path))
{
     sw.WriteLine("Sth");
}
或者使用try finally:

StreamWriter sw;

try
{
   sw = new StreamWriter(path);
   sw.WriteLine("sth");

}
finally
{
   sw.Close();
}

请换这一行

 drawGameBoardLog = new StreamWriter(debugFileName, false, 
                      System.Text.Encoding.Default, 65535); 

using
语句块将确保streamwriter关闭并写入磁盘


也许,您第一次尝试使用没有缓冲区的StreamWriter时部分奏效,但是,当您将缓冲区更改为较大值时,只有当您达到该维度时,文件才会写入磁盘。

您能显示异常调用堆栈吗?是的,您是对的,我没有接受任何答案。Soz-已更正。能否显示错误周围的代码?您确定这是错误的正确版本吗?“错误:试图读取超过流结尾的内容”是否尝试读取?您不是在使用StreamWriter吗?这可能是打字错误
System.Text.Default
。不知道。你的意思是系统、文本、编码、默认值吗?啊,我想我有进展了。基本上,由于在此之前的错误,close()调用无法到达。我在函数前面添加了一个premature Close()调用,现在在使用更大的StreamWriter构造函数时,我获得了日志信息。这是使用
using
语句的原因之一。您可能会忘记Close(),因为代码中的每个路径(包括异常)都将由using语句的右大括号处理。是的-问题肯定与文件未关闭有关,因为它未被访问直接导致该文件未关闭。我现在可以跟踪失败点,因为我可以轻松地在方法中移动Close()调用。
 drawGameBoardLog = new StreamWriter(debugFileName, false, 
                      System.Text.Encoding.Default, 65535); 
 using(StreamWriter drawGameBoardLog = new StreamWriter(debugFileName, false, 
                      System.Text.Encoding.Default, 65535))
 {


  .... write all the stuff in your log

 }