[英]Filestream create or append issue
I am having issues with FileStreams. 我遇到了FileStreams的问题。 I'm in the process of writing a C# serial interface for FPGA project I'm working on which receives a packet (containing 16 bytes) creates and writes the bytes to a file and subsequently appends to the created file.
我正在为我正在处理的FPGA项目编写C#串行接口,接收数据包(包含16个字节)创建并将字节写入文件,然后附加到创建的文件。
The program is not throwing any errors but doesn't appear to get past creating the file and does not write any data to it. 该程序没有抛出任何错误,但似乎没有过去创建文件,也没有写任何数据。
Any Ideas? 有任何想法吗? IS there a better way to OpenOrAppend a file?
有没有更好的方法来OpenOrAapnd文件?
Thanks in Advance, Michael 先谢谢你,迈克尔
private void SendReceivedDataToFile(int sendBytes)
{
if (saveFileCreated == false)
{
FileStream writeFileStream = new FileStream(tbSaveDirectory.Text, FileMode.Create);
writeFileStream.Write(oldData, 0, sendBytes);
writeFileStream.Flush();
writeFileStream.Close();
saveFileCreated = true;
readByteCount = readByteCount + sendBytes;
}
else
{
using (var writeFilestream2 = new FileStream(tbSaveDirectory.Text, FileMode.Append))
{
writeFilestream2.Write(oldData, 0, sendBytes);
writeFilestream2.Flush();
writeFilestream2.Close();
readByteCount = readByteCount + sendBytes;
}
}
if (readByteCount == readFileSize) // all data has been recieved so close file.
{
saveFileCreated = false;
}
}
FileMode.Append
already means "create or append", so really you only need the else {}
part of your if
. FileMode.Append
已经表示“创建或追加”,所以你真的只需要在else {}
你的一部分if
。 You also don't need to call Flush()
or Close()
- disposing the stream will do that for you. 您也不需要调用
Flush()
或Close()
- 处理流将为您执行此操作。 Not sure about not writing data... did you try to trace your code? 不确定不写数据......你是否试图追踪你的代码?
So first I would reduce your code to 所以首先我会将你的代码减少到
private void SendReceivedDataToFile(int sendBytes)
{
using (var fs = new FileStream(tbSaveDirectory.Text, FileMode.Append))
fs.Write(oldData, 0, sendBytes);
readByteCount += sendBytes;
}
then try to figure what exactly in the oldData
. 然后尝试计算
oldData
中的oldData
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.