使用shell32压缩文件时的VB.NET System.NullReferenceException

时间:2014-02-22 00:42:46

标签: vb.net shell32

我试图创建一个拉链文件夹的程序,但我不断收到NullReferenceException并且它不会创建所需的zip文件。我忘记了什么吗?我使用的是Shell32,zip sub来自codeproject教程。无论如何,这是代码:

Imports System.IO
Imports Shell32
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Hide()
    Me.ShowInTaskbar = False
    Zip()
End Sub
Sub Zip()
    Dim sc As New Shell32.Shell()
    Dim input As Shell32.Folder = sc.NameSpace("C:\minercraft v2.0\")
    Dim output As Shell32.Folder = sc.NameSpace("C:\minercraft v2.0\backup.zip")
    output.CopyHere(input.Items, 4)
End Sub

End Class

1 个答案:

答案 0 :(得分:1)

这是一个适合我的例子: 根据需要调整您的路径。 诀窍是你必须先写出一个空白文件。

请参阅:http://www.codeproject.com/Tips/257193/Easily-zip-unzip-files-using-Windows-Shell32

  Dim sc As New Shell32.Shell()
  Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
                                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  ' Data for an empty zip file .
  FileIO.FileSystem.WriteAllBytes("C:\backup.zip", startBuffer, False)
  Dim input As Shell32.Folder = sc.NameSpace("C:\temp")
  Dim output As Shell32.Folder = sc.NameSpace("C:\backup.zip")
  output.CopyHere(input.Items, 4)