Tuesday, July 26, 2011

VB.net code to Extract the .rar file


Private Sub UnRar(ByVal WorkingDirectory As String, ByVal filepath As String)

        ' Microsoft.Win32 and System.Diagnostics namespaces are imported


        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 = WorkingDirectory & "\"


        Dim objProcess As New Process

        objProcess.StartInfo = objStartInfo


       objProcess.Start()


End Sub

Hope you find this article useful..)


No comments:

Post a Comment