Cannot access closed file

Mar 17, 2009 at 1:44 PM
The following highlighted line is generating error "Cannot access closed file"

public static void UpdatePageNode(XmlDocument objXmlDocument, string filename, ref ZipFile ZipCPDFile)
        { 
            using (MemoryStream ms = new MemoryStream())
            {
                objXmlDocument.Save(ms);
                objXmlDocument = null;
                GC.Collect();

                ms.Seek(0, SeekOrigin.Begin);
                
                ZipCPDFile.UpdateFileStream(filename, "", ms);
                ZipCPDFile.Save(ZipCPDFile.Name);
            }
        }
Coordinator
Mar 22, 2009 at 6:44 AM
I am guessing that the ZipCPDFile is a ZipFile instance that you have initialized somewhere else?

Have you added other entries to this ZipFile instance?

The rule is this:  if you call ZipFile.AddFile() (or AddItem, or UpdateItem or UpdateFile) , then the file specified in the call must be available at the time you call ZipFile.Save(). 

If I do this:
  using (var zip = new ZipFile())
  {
      zip.AddFile("Report.csv");
      System.IO.File.Delete("Report.csv");
      zip.Save("MyArchive.zip");
  }

...then I will surely get a "file does not exist" exception.

Have you done something like this?

If not, then can you produce a succint program that reproduces the problem you are seeing? Or, failing that, can you post the stack trace? I'll see what I can do.