VB.NET使用WinRAR解壓縮檔案
參考
其中傳入的引數filepath為解壓縮檔案路徑, WorkingDirectory為解壓縮檔案至某資料夾路徑
' in the following format, 各引數以空格分隔
' " X G:\Downloads\samplefile.rar G:\Downloads\sampleextractfolder\"
objArguments = " X " & " " & filepath & " " + " " + WorkingDirectory
但是實際執行時, 若希望將壓縮檔解壓縮至特定資料夾
即WorkingDirectory傳入"./data/"
則會發生找不到解壓縮檔案的訊息
原因就出在這裡
objStartInfo.WorkingDirectory = WorkingDirectory & "\"
只要改為
objStartInfo.WorkingDirectory = "./"
就可以正常解壓縮檔案至目的資料夾
完整原始碼如下
Private Sub UnRar(ByVal filepath As String, ByVal WorkingDirectory As String)
' Microsoft.Win32 and System.Diagnostics namespaces are imported
Try
Dim objRegKey As RegistryKey
objRegKey = Registry.ClassesRoot.OpenSubKey("WinRAR\Shell\Open\Command")
' Windows 7 Registry entry for WinRAR Open Command
Dim obj As Object = objRegKey.GetValue("")
Dim objRarPath As String = obj.ToString()
objRarPath = objRarPath.Substring(1, objRarPath.Length - 7)
objRegKey.Close()
Dim objArguments As String
' in the following format
' " X G:\Downloads\samplefile.rar G:\Downloads\sampleextractfolder\"
objArguments = "X" & " " & filepath & " " & WorkingDirectory
Dim objStartInfo As New ProcessStartInfo()
' Set the UseShellExecute property of StartInfo object to FALSE
' Otherwise the we can get the following error message
' The Process object must have the UseShellExecute property
'set to false in order to use environment variables.
objStartInfo.UseShellExecute = False
objStartInfo.FileName = objRarPath
objStartInfo.Arguments = objArguments
objStartInfo.WindowStyle = ProcessWindowStyle.Hidden
objStartInfo.WorkingDirectory = "./"
Dim objProcess As New Process()
objProcess.StartInfo = objStartInfo
objProcess.Start()
Catch ex As Exception
MsgBox("UnRar error:" + ex.Message + Chr(10) + ex.ToString)
End Try
End Sub