SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers

How to Unrar .RAR Files using VB.NET Extract Code

In this VB.NET UnRar tutorial, developers can find sample VB.NET extract code project which will unrar .rar files.
This sample VB.NET extract code will show how to unrar .rar files and extract contents of the compress file to a folder on your hard disk automatically.
After reading this step-by-step guide vb.net developers can unzip or unrar multiple files located in a given file folder automatically using VB.NET codes given in the unrar example.

I required to unrar in VB.NET and VB.NET code to unrar files script actually for extracting my comics book downloads.
First of all, I renamed the downloaded comic books collection files from .CBR files extension to .RAR file extension format. Although this is a must for unrar .cbr files.
Because .CBR files are archived .RAR comic book files.
I did not want to extract every .rar file in the download folder manually so I develop the following VB.NET code to use to unrar multiple .rar files automatically.

Unrar .RAR Archived File using VB.NET

Here is the source code in VB.NET unrar a .rar file in a given folder path. Or you can think as an unzip VB.Net code script.
You can use the following VB.NET code to extract multiple files within a loop and call the subroutine UnRar to extract one file in each loop.

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)


  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

End Sub

Please note that if you get the following error message from .NET compiler :
The Process object must have the UseShellExecute property set to false in order to use environment variables.
You can solve this bug by setting the UseShellExecute property of the StartInfo object to False.

Visual Studio

Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.