Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a form to unzipping file and place into given path

Here is my code :

VB
Imports System.IO
Imports System.Windows.Forms
Imports System.Data
Imports Shell32

 Private Sub btnbrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrow.Click
        OpenFileDialog1.InitialDirectory = "D:\TempUZip"
        OpenFileDialog1.Filter = "Zip Files (*.zip)|*.zip"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtfn.Text = OpenFileDialog1.FileName
        End If
    End Sub

 Sub UnZip()

        Dim fn As String
        fn = txtfn.Text
        Dim sc As New Shell32.Shell()
        'Declare the folder where the files will be extracted
        Dim output As Shell32.Folder = sc.NameSpace("D:\TempImp")
        'Declare your input zip file as folder  .
        Dim input As Shell32.Folder = sc.NameSpace(" " & fn & " ")
        'Extract the files from the zip file using the CopyHere command .
        output.CopyHere(input.Items, 4)

    End Sub

When i run the form
I get error :

Object reference not set to an instance of an object.
Null Reference exception was unhalndled

Any Help will be highly appreciated.
Thanks in Advance...
Posted
Updated 3-Mar-12 23:04pm
v2
Comments
Keith Barrow 4-Mar-12 5:08am    
Two things:
1) Please tell us which line is throwing the error, the compiler tells you and will make spotting the problem easier
2) I have formatted your code- please do this in the future as it makes questions *much* easier to read.

1 solution

I'm guessing (because you don't tell us which line throws the error), but I suspect that you haven't created an instance of the OpenFileDialog, so the error occurs when you try to set the InitialDirectory property.
Try adding:
VB
OpenFileDialog1 = New OpenFileDialog()
at the start of your btnbrow_Click routine.

If it isn't that, then try looking in the debugger at the line which does cause the error - either it will be obvious that one of the parts is nothing / null, or you can tell us which line it is...
 
Share this answer
 
Comments
gufran90 5-Mar-12 4:58am    
Thanks for your response @Permalink / Keith Barrow

Ok, i have make some changes, now its not unzipping its place the same zip file in the
given path ( dest path ).

The code is :
Sub UnZip()
Dim fn As String
fn = txtfn.Text
Dim sc As New Shell32.Shell()
'Declare the folder where the files will be extracted
Dim output As Shell32.Folder = sc.NameSpace("D:\TempImp\")
'Declare your input zip file as folder .
Dim input As Shell32.Folder = sc.NameSpace("D:\TempUZip\")
'Extract the files from the zip file using the CopyHere command .
output.CopyHere(input.Items, 4)

End Sub

Private Sub btnbrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrow.Click
OpenFileDialog1 = New OpenFileDialog()
OpenFileDialog1.InitialDirectory = "D:\TempUZip"
OpenFileDialog1.Filter = "Zip Files (*.zip)|*.zip"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
txtfn.Text = OpenFileDialog1.FileName
End If
End Sub
======

Now when i run the above said code its copy the original file from TempUzip folder and
paste it into the destination folder which is TempImp

i want extract all file in TempImp folder not with the folder only files.

waiting for your reply...
gufran90 5-Mar-12 6:56am    
Ok , now i am using one function and its extracting all files in the dest path.

But , the problem is when completing jobs it displays a error Message.

Error : Index was outside the bounds of the array.

here is my corrction code.

Private Function ExpandZipFile(ByVal ZipFile As String, ByVal DestFolder As String) As String()

Try
Dim sh As New Shell()
Dim sf As Folder = sh.[NameSpace](ZipFile)
Dim df As Folder = sh.[NameSpace](DestFolder)
Dim files As String() = New String(sf.Items().Count - 1) {}
Dim i As Integer = 0
For Each f As FolderItem In sf.Items()
df.CopyHere(f, 0)
files(System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)) = f.Name
Next
Return files
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Function

====

Sub UnZip()
Dim des As String
des = "D:\TempImp"
ExpandZipFile(Me.txtfn.Text, des)
End Sub
=====

Private Sub btnbrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrow.Click
OpenFileDialog1 = New OpenFileDialog()
OpenFileDialog1.InitialDirectory = "D:\TempUZip"
OpenFileDialog1.Filter = "Zip Files (*.zip)|*.zip"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
txtfn.Text = OpenFileDialog1.FileName
End If
End Sub
===

Private Sub Btnuzip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnuzip.Click
UnZip()
End Sub
=====

Any help will be highly appreciated .
Thanks in advance.....
waiting for reply?????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900