C # Removing .ZIP file after unpacking

I am using Ionic.Zip.dll from the DotNetZip library and I am trying to delete the ZIP file after the decompression is complete, but I cannot do this.

Here is the code I have:

    using (ZipFile zip = ZipFile.Read(nextVersion + ".zip"))
{
    zip.ExtractAll(Directory.GetCurrentDirectory(), ExtractExistingFileAction.OverwriteSilently);

    try
    {
        File.Delete(nextVersion + ".zip");
    }
    catch (Exception)
    {
        MessageBox.Show("Could not delete ZIP!");
        Environment.Exit(1);
    }
}

What am I doing wrong here?

+5
source share
4 answers

You get an exception because the file is still open when you try to delete. Move File.Deleteto the block using.

+6
source

Try it?

try {
    using (ZipFile zip = ZipFile.Read(nextVersion + ".zip"))
    {
        zip.ExtractAll(Directory.GetCurrentDirectory(), ExtractExistingFileAction.OverwriteSilently);
    }
    File.Delete(nextVersion + ".zip");
}
catch (Exception) {
   MessageBox.Show("Could not delete ZIP!");
   Environment.Exit(1);
}
+3
source

! , . Zip - , .

 using (ZipFile zip = ZipFile.Read(nextVersion + ".zip"))
            {
                zip.ExtractAll(Directory.GetCurrentDirectory(), ExtractExistingFileAction.OverwriteSilently);
                zip.Dispose();
                try
                {
                    File.Delete(nextVersion + ".zip");
                }
                catch (Exception)
                {
                    MessageBox.Show("Could not delete ZIP!");
                    Environment.Exit(1);
                }
            }
+2

. , , (ZipFile zip = ZipFile.Read(nextVersion + ".zip" ))

0

All Articles