Filestream create or append issue

12,738

FileMode.Append already means "create or append", so really you only need the else {} part of your if. You also don't need to call Flush() or Close() - disposing the stream will do that for you. 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.

Share:
12,738
Ken Keenan
Author by

Ken Keenan

I am a contract programmer working in Dublin, Ireland. I started programming back in the 80s on an old Sinclair ZX Spectrum and haven't stopped since. I started working as a professional programmer in 1998 and became a contractor in 2005. For most of my career, I've mostly worked on line-of-business type applications for organisations of various sizes, initially in languages like Visual Basic 6 and Java and more recently in C# and Visual Basic.NET, with some PHP thrown in for good measure along the way. I've also worked on the database side, with MySQL, Oracle and Microsoft SQL Server. I tend to be fairly agnostic in these things... LinkedIn Profile: ie.linkedin.com/in/kenkeenan

Updated on June 23, 2022

Comments

  • Ken Keenan
    Ken Keenan almost 2 years

    I am having issues with 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.

    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?

    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;
            }
        }
    
  • Admin
    Admin about 12 years
    Thank you, That's useful to know. I've been tracing the code and it seems the problem could be the integer I'm passing to function. I'll investigate further.
  • Admin
    Admin about 12 years
    Ok, the issue it turns out was silly/trivial. I had set an integer to wrong value (specified it in hex by put what I wanted it to be in decimal.) Thank you for the code reduction, Much appreciated.