Mounting ISO Files and Virtual Drives in Visual Basic

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Isn't it amazing how many things there are that we use on a daily basis but do not exactly know what makes these things tick? The other day, I had to mount an ISO file on my laptop, just to install the software because my DVD-ROM decided to say farewell. By doing this simple task, the curiosity in me took over and I just had to know how to create a similar program. Today, I will show you how you can make a program that mounts ISO files.

ISO Images

ISO image is a term usually associated with DVD and CD burning. An ISO image is an archive file of an optical disk. This means that the ISO image file is an exact copy of an existing file system. An ISO image can contain the entire contents of a DVD disk or CD disk. ISO images can be created from disks by disk imaging software, or from a collection of files by disk authoring software. The name "ISO" is taken from the ISO 9660 file system used with CD-ROM media, but an ISO image also might contain a UDF (Universal Disk Format), ISO/IEC 13346, file system which is commonly used by DVDs and Blu-ray disks.

Virtual Drives

A virtual drive is a software component that emulates an actual disk drive, such as a floppy disk drive, a hard disk drive, or an optical disk drive. To other programs, a virtual drive looks and behaves like an actual physical device.

.NET DiscUtils

The project that you will do today is a bit small, but it packs a punch. A small disclaimer: Although I am clever, I won't mention my I.Q., ;) I am not so clever as to summon enough brainpower to do a project such as DiscUtils on Codeplex. I guess my whole problem is that I do not have much spare time to think of brilliant programs. Doing three jobs is not easy.

On the other hand, in the corporate environment, I have learned that, instead of re-inventing the wheel, there is almost always a chance that the particular wheel your are trying to re-invent has already been invented. This is the case with DiscUtils.

DiscUtils is a .NET library to read and write ISO files and Virtual Machine disk files, for example: VHD, VDI, XVA, VMDK.

The program that you will create today will be using DiscUtils to aid in creating ISO images from supplied files, as well as create a virtual disk drive.

Our Project

Start Visual Studio and create a new Visual Basic Windows Forms project. Once the form has loaded, add one button to your form with the Caption of "Mount Files." You may name it anything you want.

Before continuing with the code, you first need to have the DiscUtil libraries linked to your application. To do this, follow the next few steps:

  1. Navigate to CodePlex in your browser.
  2. Download the APIs.
  3. After the library has been downloaded to your PC, unzip it.
  4. In Visual Studio, add a Reference to DiscUtils.MSBuild.dll.
  5. Add a reference to DiscUtils.dll as well as DiscUtils.Common.dll.

Code

Add the necessary Imports so that we can use the libraries present in the DiscUtils library:

Imports DiscUtils.Iso9660

Add the following code to create a Mounted disk image:

   Private Sub Button1_Click(sender As Object, e As EventArgs) _
         Handles Button1.Click
      Dim cdBuild As New CDBuilder()
      cdBuild.UseJoliet = True
      cdBuild.VolumeIdentifier = "DISK_1"

      cdBuild.AddDirectory("test")
      cdBuild.AddFile("Hello.txt", New Byte() {})
      cdBuild.Build("sample3.iso")
   End Sub

The preceding code creates a new mounted disk. The disk's name is "DISK_1." The mounted disk contains one folder named test and one file named Hello.txt—you should create this file yourself and specify the appropriate path for the file. The ISO Image's name is sample3.iso.

To extract data from the Mounted Disk image file, you can simply use a FileStream object and read the contents of the file:

   Private Sub Button3_Click(sender As Object, e As EventArgs) _
         Handles Button3.Click
      Using stISO As FileStream = _
            File.Open("sample3.iso", FileMode.Open)
         Dim cd As New CDReader(stISO, True)

         Dim fileStream As Stream = _
            cd.OpenFile("Hello.txt", FileMode.Open)
      End Using
   End Sub

To create a Virtual disk, you could use the following code:

      Dim lngDiskSize As Long = 15 * 1024 * 1024   '15MB'

      Using vhdStream As Stream = _
            File.Create("C:\EXAMPLE\mydisk.vhd")
         Dim dskDisk As Disk = Disk.InitializeDynamic(vhdStream, _
            lngDiskSize)
         BiosPartitionTable.Initialize(dskDisk, _
            WellKnownPartitionType.WindowsFat)
         Using fs As FatFileSystem = _
            FatFileSystem.FormatPartition(dskDisk, 0, Nothing)

            fs.CreateDirectory("TestFolder\TestChild")
         End Using
      End Using
   End Sub

This creates a virtual disk 15 Megabytes in size with a few files and folders.

Conclusion

Understanding what makes certain PC features tick is pivotal for acquiring new knowledge. Do not be afraid of trying out new things, and most importantly, learning new things about your PC's features.



About the Author

Hannes DuPreez

Hannes du Preez has been a Microsoft MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date