First I declared the follwing variables:
Public sTotalVolumes As Integer = 0
Public iRemovable As Integer = 0
Public sLocalDisks As Integer = 0
Public iDvd As Integer = 0
Public sLastDvdVolumeNumber As Integer = 0
Public sFirstLocalDiskVolumeNumber As Integer = 0
Then I got the machine's volumes' information:
Private Sub GetVolumeInfo()
Dim objMOC As Management.ManagementObjectCollection
Dim objMOS As Management.ManagementObjectSearcher = New Management.ManagementObjectSearcher("SELECT * FROM Win32_Volume")
Dim objMO As Management.ManagementObject
objMOC = objMOS.Get()
sTotalVolumes = CInt(objMOC.Count)
For Each objMO In objMOC
Dim sDriveName As String = objMO("Name")
Dim sDriveType As Integer = CInt(objMO("DriveType"))
If sDriveType = 5 Then
iDvd = iDvd + 1
ElseIf sDriveType = 2 Then
iRemovable = iRemovable + 1
ElseIf sDriveType = 3 Then
sLocalDisks = sLocalDisks + 1
End If
Next
End Sub
Then last I used this
Sub
to delete the existing partitions:
Private Sub DelExistingPartitions()
If Me.InvokeRequired Then
Me.Invoke(New DelegateDelExistingPartitions(AddressOf DelExistingPartitions))
Else
GetVolumeInfo()
Dim tmp As String
Dim p As New Process
p.StartInfo.FileName = sSystem32 & "\diskpart.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardInput = True
p.StartInfo.CreateNoWindow = True
p.Start()
Dim SR As System.IO.StreamReader = p.StandardOutput
Dim SW As System.IO.StreamWriter = p.StandardInput
sFirstLocalDiskVolumeNumber = iDvd - 1
For i As Integer = 1 To sLocalDisks - 1 Step 1
sFirstLocalDiskVolumeNumber = sFirstLocalDiskVolumeNumber + 1
SW.WriteLine("sel vol " & sFirstLocalDiskVolumeNumber)
SW.WriteLine("Delete vol override")
Next
SW.WriteLine("exit")
Do While p.HasExited = False
tmp = SR.ReadLine()
If tmp <> "" Then
Me.txtResults.AppendText(tmp & vbCrLf)
End If
Loop
SR.Close()
SW.Close()
p.Dispose()
End If
End Sub