How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Unzip files using Shell32 in C#

The .NET Framework didn’t have an easy way for developers to unzip files resulting to a lot of people having to use third party libraries to achieve that functionality. This was until the .NET Framework 4.5 was released which had a new class called ZipFile that simplified the process of unzipping files..

But since targeting the .NET Framework 4.5 might not be ideal in many cases, mainly because its adaptation rate is still fairly low I will demostrate another way to unzip .zip files with the use of Shell32.

Firstly you will need to reference in your project the COM library called Microsoft Shell Controls And Automation (Interop.Shell32.dll) in order to be able to access the Shell32 namespace.

The method below is a simple example that takes two parameters. The .zip file location and the folder destination where the files will be extracted to.

The usage is fairly simple:


You may have noticed the 4 | 16 values in the CopyHere method. Those are the copy options which you can change to make the copying behave differently in order to suit your needs. Here is the list of the additional options.

4 – Do not display a progress dialog box.
8 – Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
16 – Respond with “Yes to All” for any dialog box that is displayed.
64 – Preserve undo information, if possible.
128 – Perform the operation on files only if a wildcard file name (*.*) is specified.
256 – Display a progress dialog box but do not show the file names.
512 – Do not confirm the creation of a new directory if the operation requires one to be created.
1024 – Do not display a user interface if an error occurs.
2048 – Version 4.71. Do not copy the security attributes of the file.
4096 – Only operate in the local directory. Do not operate recursively into subdirectories.
8192 – Version 5.0. Do not copy connected files as a group. Only copy the specified files.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

10 Responses to Unzip files using Shell32 in C#

  1. Benjamin says:

    For all those with “no such interface supported” error. I fixed it like this:

    from jeronevw’s answer on this forum:
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/b25e2b8f-141a-4a1c-a73c-1cb92f953b2b/instantiate-shell32shell-object-in-windows-8?forum=clr
    Hope it helps!

  2. TBD says:

    This method works great (even referenced in stackoverflow: http://stackoverflow.com/questions/16052877/how-to-unzip-all-zip-file-from-folder-using-c-sharp-4-0-and-without-using-any-o ) but I found that it creates temp files that aren’t deleted in the local\temp directory.

  3. Martin Paliza says:

    No se puede convertir el objeto COM del tipo ‘System.__ComObject’ al tipo de interfaz ‘Shell32.Shell’. Ocurrió un error de operación debido a que la llamada QueryInterface en el componente COM para la interfaz con IID ‘{34936BA1-67AD-4C41-99B8-8C12DFF1E974}’ generó el siguiente error: Interfaz no compatible (Excepción de HRESULT: 0x80004002 (E_NOINTERFACE)).

    • Peter says:

      I have the same issue:
      + $exception {“Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘Shell32.Shell’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{34936BA1-67AD-4C41-99B8-8C12DFF1E974}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).”} System.Exception {System.InvalidCastException}

  4. vatsal says:

    using this code for asp.net application.
    ########
    ERROR
    ########

    ERror after executing Shell32.Folder sourceFile = objShell.NameSpace(zipFile);

    System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.

  5. Sandeep says:

    Thanks.. Works Great for me

  6. Narendhar Rao Soma says:

    Excellent! It works fine and amazing job with such a small piece of code…
    Thank you very much

  7. puneeth c says:

    how can i write this statement.

    public static void UnZip(string zipFile, string folderPath)
    {

    i have static void main(string[]args)

  8. Eugen says:

    Thank you. This really works!

Leave a Reply

Your email address will not be published. Required fields are marked *