1

Hi I'm doing some testing on unzipping files using Shell32. In this code there's a several files in "C:\Temp" and I unzip those files by creating a new folder name Unzip which will store the unzipped files.

So the question is how to check if the file already exists, the program will hold or continue to the next function. Here's the code.

    Dim di As New IO.DirectoryInfo("C:\Temp")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo
    Dim sc As New Shell32.Shell()
    For Each dra In diar1    
            IO.Directory.CreateDirectory("C:\Temp\Unzip\" & dra.Name & "")
            Dim output As Shell32.Folder = sc.NameSpace("C:\Temp\Unzip\" & dra.Name & "")
            Dim input As Shell32.Folder = sc.NameSpace("C:\Temp\" & dra.Name & "")
            output.CopyHere(input.Items, 4)
    Next

Thanks In advance :)

plokuun
  • 51
  • 8

1 Answers1

0

File.Exists() is bad for this kind of thing.

What you want to do instead is attempt to open your output stream in a way that will fail (throw an exception) if the file is already there, and handle the exception. Unfortunately, the Shell32 process you're using right now doesn't allow for that. However, it's not that hard to unzip files using .Net directly. Especially if you can use .Net 4.5, this is pretty easy: there is new ZipArchive support built-in. In prior versions of .Net, you may want to look at SharpZipLib. It's in C#, but you will be able use the library from VB.Net.

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Is there a way to open the file in SharpZipLibrary since I'm using visual studio 2013 it cannot open the file. Yea It's easy if I'm using .Net 4.5 but this program requires me to on .Net 3.5 – plokuun Jul 11 '14 at 19:54