Formatting a drive in Visual Basic?

1.9k views Asked by At

I have a ListBox that is being populated only with removable drives. The user selects the drives to be formatted and then the program should format those drives. However, I get the message that the specified file cannot be found. Here is my code.

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each Drive In My.Computer.FileSystem.Drives
        'Gets drive letter and type
        Dim DriveInfo As String = Drive.Name & " (" & Drive.DriveType.ToString & ")"

        'Checks to see if drive is a removable drive
        Dim removable = "Removable"
        Dim isFlashDrive As Boolean = DriveInfo.Contains(removable)

        'Adds only removable drives to the list
        If isFlashDrive Then
            ListBox1.Items.Add(DriveInfo)
        End If
    Next


End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs)
    'Variables initialized
    Dim i, j As Integer
    Dim s, DrvsToFormat As String

    'Stores all selected drives in an array named "drives" and creates string with drive letter
    Dim drives(ListBox1.SelectedItems.Count) As String
    For i = 0 To ListBox1.SelectedItems.Count - 1
        s = ListBox1.SelectedItems(i).ToString.Substring(0, 2)
        drives(i) = s
        DrvsToFormat = DrvsToFormat & " " & ListBox1.SelectedItems(i).ToString.First()
    Next

    Dim response = MessageBox.Show("Are you sure you want to format drive(s) " & DrvsToFormat & "? All data will be lost.", "WARNING!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
    If response = MsgBoxResult.Yes Then
        For j = 0 To drives.Length() - 1
            Console.WriteLine(drives(j))
            Process.Start("format " & drives(j))
        Next
        MessageBox.Show("Format Complete!")
    End If



End Sub
End Class

1

There are 1 answers

3
MicroVirus On BEST ANSWER

The problem is that Process.Start does not take command line arguments in the first parameter. Use the overload that allows command line arguments.

For example:

Process.Start("format.com", "H:");