Search code examples
vb.netvisual-studio-2010esri

Use Shell32.dll on mix of XP, Vista, Win7 via XCOPY deploy


I'm trying to use Shell32.dll to unzip a simple compressed file. I have a reference to Shell32.dll which shows up as Interop.Shell.dll on my development PC (Win7 64.) I push exe updates out via file copy from a central location and I would like to do this for the .dll as well.

The program fails on XP using the same file as it installed on my PC. I tried using a Shell.dll from the XP \Windows\System renamed to Interop.Shell.dll but I guess that would be to simple - it gets an error that it cannot load when I try to invoke the dll.

I'm open too another way of doing unzip that uses a dll that is agnostic as to platform. I've used 7Zip via Process.Start() but I'd like to avoid having to install/update a program on the clients.

Is there a common denominator Shell32.dll I could use the would run on any Win OS on/after XP?

Or do I have to create a separate build for each OS if I use Shell32.dll?

Thanks!

Function Unzip(FilePathofZip As String, DestinationFolder As String) As Boolean
    Try
        Dim sFile As String = FilePathofZip
        ' requires referenc to windows.system.shell32.dll
        Dim sc As New Shell32.Shell()

        If IO.Directory.Exists(DestinationFolder) Then IO.Directory.Delete(DestinationFolder, True)
        Application.DoEvents()
        IO.Directory.CreateDirectory(DestinationFolder)
        'Declare the folder where the files will be extracted
        Dim output As Shell32.Folder = sc.NameSpace(DestinationFolder)

        If FilePathofZip.EndsWith(".kmz") Then
            sFile = FilePathofZip & ".zip"
            IO.File.Move(FilePathofZip, sFile)
            Application.DoEvents()
        End If

        'Declare your input zip file as folder 
        Dim input As Shell32.Folder = sc.NameSpace(sFile)
        'Extract the files from the zip file using the CopyHere command .
        output.CopyHere(input.Items, 4)
        Return True
    Catch ex As Exception
        MsgBox(ex.Message)
        Return False
    End Try
End Function

Solution

  • I switched to DotNetZip. Add Project Reference to the Ionic.Zip.Reduced.dll. Imports Ionic.Zip

    This code is expecting .kmz files downloaded from ESRI REST services but should work with .zip files as well.

    Function Unzip(FilePathofZip As String, DestinationFolder As String) As Boolean
        ' extract .kmz files downloaded from ESRI REST services
        Try
            Dim sFile As String = FilePathofZip
    
            If IO.Directory.Exists(DestinationFolder) Then IO.Directory.Delete(DestinationFolder, True)
            Application.DoEvents()
            IO.Directory.CreateDirectory(DestinationFolder)
    
            If FilePathofZip.EndsWith(".kmz") Then
                sFile = FilePathofZip & ".zip"
                IO.File.Move(FilePathofZip, sFile)
                Application.DoEvents()
            End If
    
            Try
                ' Using... required
                Using zip As ZipFile = ZipFile.Read(sFile)
                    Dim e As ZipEntry
                    For Each e In zip
                        e.Extract(DestinationFolder)
                    Next
                End Using
            Catch ex1 As Exception
                MsgBox("Problem with compressed file - notify programmers" & vbCrLf & ex1.Message)
                Return False
            End Try
    
            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
    End Function