lzc
lzc

Reputation: 1705

Using ZipFile compression: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

I'm unsure why this exception is happening after the folder contents are successfully zipped. Should this be right?

Error: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process cannot access the file 'c:\Temp\pack.zip' because it is being used by another process.

    private static string directoryPath = @"c:\Temp\";
    static void Main(string[] args)
    {
        zipFolder(directoryPath, directoryPath+@"pack.zip");
    }

    public static void zipFolder(string targetPath, string resultPath)
    {
        ZipFile.CreateFromDirectory(targetPath, resultPath,CompressionLevel.Optimal,true);
    }

Upvotes: 0

Views: 1860

Answers (1)

Nathan
Nathan

Reputation: 1317

What you are doing in your code is reading the content of C:\Temp while trying to create a zip file in the same directory.

Instead create a file in the app directory and copy the file to the Temp folder later on.

        var newFilePath = Path.Combine(directoryPath, "pack.zip");
        if(File.Exists(newFilePath))File.Delete(newFilePath); //Remove file if it exists
        if (File.Exists("pack.zip")) File.Delete("pack.zip"); //Remove file if it exists
        zipFolder(directoryPath, "pack.zip");
        File.Move("pack.zip", newFilePath);

Upvotes: 1

Related Questions