我有这段代码可以将字节数组写入文本文件以进行调试:
string path = @"\LOG\WrittenData.txt";
. . .
if( bWriter != null )
{
if( bWriter.BaseStream.CanWrite )
{
bWriter.Write( readbuffer, 0, numberOfBytesRead );
//TODO: Remove after testing
WriteByteArrayToFile(path, readbuffer);
}
}
. . .
public static bool WriteByteArrayToFile(string fileName, byte[] readBuffer)
{
try
{
FileStream _FileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write);
_FileStream.Write(readBuffer, 0, readBuffer.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
...不幸的是,它在标题中显示错误消息而失败。
更新
好的,我尝试尝试在 finally 中关闭的建议。但是,它不会编译!下面显示的是,注释掉的不是!什么!?!
try
{
// Open file for reading. // changed from FileMode.Create to FileMode.Append
System.IO.FileStream _FileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write);
_FileStream.Write(readBuffer, 0, readBuffer.Length);
_FileStream.Close();
}
finally
{
;//_FileStream.Close(); <-- won't compile - "The type or namespace name '_FileStream' could not be found (are you missing a using directive or an assembly reference?)"
}