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.