0

I need to extract the content of a zipfile to an SD-card.

ZipFile.ExtractToDirectory(@"D:\RcCardData.zip", this.selectedDrive);

Works like a charm. However I was wondering if this uses the Write-cache of the disk. If yes, is there a way to ensure that all of the data is written to the card before I tell the user he can remove it?

At first I've tried using code to "safe remove" the SD-card but the problem here was that it was ejecting the whole USB card-reader and not just the card.

UPDATE

I've done some extra research on this matter.

When looking at the code for ExtractToDirectory via dotPeek it boils down to the following method:

 public static void ExtractToFile(this ZipArchiveEntry source, string destinationFileName, bool overwrite)
    {
      if (source == null)
        throw new ArgumentNullException("source");
      if (destinationFileName == null)
        throw new ArgumentNullException("destinationFileName");
      FileMode mode = overwrite ? FileMode.Create : FileMode.CreateNew;
      using (Stream destination = (Stream) File.Open(destinationFileName, mode, FileAccess.Write, FileShare.None))
      {
        using (Stream stream = source.Open())
          stream.CopyTo(destination);
      }
      File.SetLastWriteTime(destinationFileName, source.LastWriteTime.DateTime);
    }

And since a Dispose() is executed on the stream when exiting the using-block, the stream is Close()-d and Flush()-ed.

Now I've recently read somewhere that since .Net 4.0 the write cache is emptied when executing a flush (not sure about this)?

With the extra information given above, can someone tell me if it's safe to remove the SD-card when the extraction is completed?

Koen
  • 2,501
  • 1
  • 32
  • 43
  • This all depends on your system, how it is configured and what drivers you are using. Once your stream is disposed then its down to any caching at the hardware level. I suspect on most systems write-behind caching is disabled for SD-Cards. http://support.microsoft.com/kb/2442580 – Jodrell Sep 06 '13 at 11:12
  • That's what I was afraid for. Is there a way to disbale the write-behind cache from code? – Koen Sep 06 '13 at 11:30
  • try http://stackoverflow.com/a/383332/659190 or http://stackoverflow.com/questions/122362/how-to-empty-flush-windows-read-disk-cache-in-c – Jodrell Sep 06 '13 at 12:02
  • Thanks for your help. I'll have a look at those. If you like you may put your first comment in an answer, so that I can accept it. – Koen Sep 06 '13 at 12:33

0 Answers0