Format a Drive using VB.NET

This tutorial will teach you how to create a program that can format a drive such as C or D using VB.NET. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only one Button named Button1 and one combobox named ComboBox1 to get all the drives. You must design your interface like this: output 3. Now, we will do the coding. We will create first two enum named the FormatType and Capacity.
  1. Enum FormatType
  2. Quick = 0
  3. Normal = 1
  4. End Enum
  5. Enum Capacity
  6. DefaultCapcity = 0
  7. End Enum
Now, we will declare a function that will access the shell32 library because all the necessary files needed to format are there.
  1. Declare Function SHFormatDrive Lib "shell32" (ByVal hwnd As Int32, ByVal sDriveToFormat As String, ByVal s As Capacity, ByVal formattype As FormatType) As Int32
Then in the Form_Load, we will get all the drives available in your computer and will validate if it has administrator access.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'check if the current user is a member of the administrators
  3. If My.User.IsInRole("administrators") = True Then
  4. For Each drive In IO.DriveInfo.GetDrives
  5. 'get all removable and fixed drives
  6. If drive.DriveType = IO.DriveType.Removable Or drive.DriveType = IO.DriveType.Fixed Then
  7. 'add all found drives into the combobox
  8. ComboBox1.Items.Add(drive)
  9. End If
  10. Next
  11. Else
  12. MsgBox("You need administrator access to format a drive", MsgBoxStyle.Critical)
  13. End
  14. End If
  15. End Sub
Lastly, we will call the function to be able to format the drive in the click event of our button.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. 'Show the Format dialog
  3. SHFormatDrive(Me.Handle.ToInt32, ComboBox1.Text, CType(2, Capacity), FormatType.Normal)
  4. End Sub
Download the code and try it! For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment