我正在尝试做一些非常简单的事情。
在查找与StreamWriter(字符串路径)构造函数关联的默认缓冲区大小对于我正在记录的信息而言,我试图使用以下构造函数:
public StreamWriter(
string path,
bool append,
Encoding encoding,
int bufferSize
)
生成的日志文件完全是空的 - 这更糟糕。
注意:我的原始帖子引用了错误“错误:尝试读取流的末尾”,但这与该方法后面的功能有关,由于此日志文件问题,我无法记录信息。
以下是我的代码中的旧构造函数用法:
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()...");}
使用接受path,append,encoding和buffersize的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]
默认缓冲区大小就在那里。
答案 0 :(得分: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();
}
答案 1 :(得分:1)
请更改此行
drawGameBoardLog = new StreamWriter(debugFileName, false,
System.Text.Encoding.Default, 65535);
in
using(StreamWriter drawGameBoardLog = new StreamWriter(debugFileName, false,
System.Text.Encoding.Default, 65535))
{
.... write all the stuff in your log
}
using
语句块将确保关闭流写器并将其写入磁盘
使用没有缓冲区的StreamWriter的第一次尝试可能部分工作,但是,当您将缓冲区更改为较大值时,只有当您到达该维度时,文件才会写入磁盘。