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:
3. Now, we will do the coding.
We will create first two enum named the FormatType and Capacity.
Enum FormatType
Quick = 0
Normal = 1
End Enum
Enum Capacity
DefaultCapcity = 0
End Enum
Now, we will declare a function that will access the shell32 library because all the necessary files needed to format are there.
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.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'check if the current user is a member of the administrators
If My.User.IsInRole("administrators") = True Then
For Each drive In IO.DriveInfo.GetDrives
'get all removable and fixed drives
If drive.DriveType = IO.DriveType.Removable Or drive.DriveType = IO.DriveType.Fixed Then
'add all found drives into the combobox
ComboBox1.Items.Add(drive)
End If
Next
Else
MsgBox("You need administrator access to format a drive", MsgBoxStyle
.Critical) End
End If
End Sub
Lastly, we will call the function to be able to format the drive in the click event of our button.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Show the Format dialog
SHFormatDrive(Me.Handle.ToInt32, ComboBox1.Text, CType(2, Capacity), FormatType.Normal)
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