0

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

在查找与 StreamWriter(string path) 构造函数关联的默认缓冲区大小对于我正在记录的信息而言太小时,我尝试使用以下构造函数:

public StreamWriter(
    string path,
    bool append,
    Encoding encoding,
    int bufferSize
)

生成的日志文件完全是空的——这更糟。

注意:我的原始帖子引用了错误 "Error: Attempted to read past 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()...”行。而在我获得将文件大小变为 1K 的内容之前。这是到目前为止的日志文件(顺便说一下,我从图形编程开始):

19/08/2012 14:13:21
timeStampString = 19082012_141321
DrawGameBoard()...
noOfIndexes = [6], noOfVertices = [4]
...just set stremSize = [80]
...creating vertices DataStream...
...building Matrices...
...Scaling matrix DONE...
...Rotation matrix DONE...
...Translation matrix DONE...
...Orthogonal matrix DONE...
...Perspective matrix DONE...
...COMBINED matrix DONE...
...creating Vector3 and Vector2 arrays to hold positions and texture coords...
...Writing Texture coords....
...Building Index Array (order of vertices to be drawn for a quad)....
...Declaring indicesStream. Set size of stream to [24]....
...Created data stream for indices OK. Now writing indices array to it....
...DONE. Just set position back to ZERO...
...Created new index buffer OK....
...configure the Input Assembler....
...Getting Vectors for position [0,0]...
...Got Vectors into myVectorPositions....
myVectorPositions[0] = [X:0 Y:0 Z:0.5]
myVectorPositions[1] = [X:20 Y:0 Z:0.5]
myVectorPositions[2] = [X:0 Y:20 Z:0.5]

默认缓冲区大小就在那里启动。

4

2 回答 2

1

您没有关闭 StreamWriter。最后类型:

drawGameBoardLog.Close();

或使用using块:

using(StreamWriter sw = new StreamWriter(path))
{
     sw.WriteLine("Sth");
}

或者最后尝试一下:

StreamWriter sw;

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

}
finally
{
   sw.Close();
}
于 2012-08-19T13:29:13.373 回答
1

请更改此行

 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

 }  

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

可能,您第一次尝试使用没有缓冲区的 StreamWriter 部分工作,但是,当您将缓冲区更改为一个大值时,只有当您达到该尺寸时,文件才会写入磁盘。

于 2012-08-19T13:30:55.327 回答