1. Process.Start 不可混入参数,否则系统会当成是路径的一部分
要加参数要用 ProcessStartInfo 设置,参考范例
2. 编程可以用很多方法来压缩/解压
a. SDK: ex. 7zip 提供的 LZMA
b. 函式库 (library): ex. 范例中使用 DotNetZip
c. 外部呼叫: 直接呼叫执行档帮忙解压
操纵性: a > b > c
效率: a > b > c
稳定性: a > b > c
方便性: a < b < c
范例中实现了 b 和 c, 如果是快速开发,建议选 c (也就是你原本用的方式)
3. 不建议用 WinRar, 因为是商业软体,建议用 7zip
范例程式码:
复制程式
Imports System.IO
Imports Ionic.Zip
Module Module1
' from QuickTime official website
Private Const SAMPLE_FILE As String = "sample_mpeg4.mp4.zip"
Public Sub Main()
' delete extracted file
File.Delete("sample_mpeg4.mp4")
' use DotNetZip
'ExtractByDotNetZip()
' use 7zip command line version
'ExtractByCall()
' use Process.Start the wrong way
ExtractWrong()
' pause
Console.WriteLine("Finished.")
Console.ReadKey()
End Sub
Private Sub ExtractByDotNetZip()
Using zip As ZipFile = ZipFile.Read(SAMPLE_FILE)
For Each entry As ZipEntry In zip
Try
entry.Extract(ExtractExistingFileAction.OverwriteSilently)
Catch ex As Exception
Console.WriteLine("Error {0}", ex.Message)
End Try
Next
End Using
End Sub
Private Sub ExtractByCall()
Dim _
psi As _
New ProcessStartInfo _
With {.FileName = "7za.exe",
.Arguments = String.Format("x {0} -aoa", SAMPLE_FILE)
}
Dim ret As Integer
Using proc As Process = Process.Start(psi)
proc.WaitForExit()
ret = proc.ExitCode
End Using
If 0 = ret Then
Console.WriteLine("OK!")
Else
Console.WriteLine("Error = {0}", ret)
End If
End Sub
Private Sub ExtractWrong()
Using _
proc As Process =
Process.Start(String.Format("7za.exe x {0} -aoa",
SAMPLE_FILE))
proc.WaitForExit()
End Using
End Sub
End Module
1. 测试档案可从
http://support.appl.../ht1425 取得
2. DotNetZip:
http://dotnetzip....x.com/3. 7Zip 命令列 (7-Zip Command Line Version):
http://www.7-zip.or...ad.html4. 外部呼叫方式:
复制程式
Dim psi As New ProcessStartInfo With {.FileName = "档案名", .Arguments = "参数"}
Process.Start(psi)
4. 使用 proc.WaitForExit() 等待程序结束,为同步的方式,接下来才能用 proc.ExitCode 取得 ExitCode,可得知程式执行结果,ex. 7za 回传 0 代表没错