Cannot access closed file during Reading/Writing operation C#

I have searched the internet and viewed many posts, but none has helped me. So I decided to ask my own question over here.

I am writing some data to binary file when the save button on my form is clicked, and reading that data from the file when the file is opened from my form. The data includes text from some textboxes and two Lists. I have created a save method which has the following code:

BinaryWriter writer = new BinaryWriter(new FileStream(savedFileName, FileMode.Create, FileAccess.Write));
        writer.Write(Database.firstName);
        writer.Write(Database.middleName);
        writer.Write(Database.lastName);
        writer.Close();

I have another general method to write a list which has the following code:

private void writeDGVrowListToBinaryFile(List<DGVrow> list, BinaryWriter R)
        {
            Nrows = list.Count;
            R.Write(Nrows);
            for (int i = 0; i < Nrows; i++)
            {
                DGVrow dgvr = list[i];
                R.Write(dgvr.Day);
                R.Write(dgvr.FromTime);
                R.Write(dgvr.ToTime);
            }
            R.Close();
        }

Because I have two lists, I added these two lines to the Save method, before the writer.Close() line, changing the first code to this:

BinaryWriter writer = new BinaryWriter(new FileStream(savedFileName, FileMode.Create, FileAccess.Write));
writer.Write(Database.firstName);
writer.Write(Database.middleName);
writer.Write(Database.lastName);
writeDGVrowListToBinaryFile(Database.officeHoursList, writer);
writeDGVrowListToBinaryFile(Database.classScheduleList, writer);
writer.CLose();

The problem is, that when the code reaches the R.Write(Nrows);, it throws the ObjectDisposedException and says that it cannot access a closed file. What is causing the file to be closed at this point? And how can I prevent the file from closing?

The same problem occurs while reading as reading uses the same pattern as writing.

Jon Skeet
people
quotationmark

What is causing the file to be closed at this point?

Your call to R.Close() in writeDGVrowListToBinaryFile. You shouldn't be closing the writer in that method. You almost never want to close a file handle (or other disposable resource) which is passed into a method - typically you acquire a handle, use it (possibly passing it to other methods), and then close it, all in the same method. I'd also encourage you to use a using statement to ensure the file is closed even if an exception is thrown:

using (var writer = new BinaryWriter(...))
{
    writer.Write(Database.firstName);
    writer.Write(Database.middleName);
    writer.Write(Database.lastName);
    writeDGVrowListToBinaryFile(Database.officeHoursList, writer);
    writeDGVrowListToBinaryFile(Database.classScheduleList, writer);
}

I'd also strongly encourage you to revisit your naming - of both your parameter (R is unconventional and has no obvious meaning) and the method name (writeDGVrowListToBinaryFile is unconventional - how about just WriteRowList? It's taking a BinaryWriter, so it should be obvious that it's going to write to it..)

people

See more on this question at Stackoverflow