0

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

xpda
  • 15,585
  • 8
  • 51
  • 82
Josh
  • 718
  • 2
  • 15
  • 38
  • What is the exact error message you get, and exactly which line causes it? (I'm betting on the line with `Process.Start`, and I'm also betting that a quick look at `drives(j)` in the debugger at that point shows you clearly what is causing the problem.) – Ken White Jun 15 '15 at 19:04
  • [Did you search?](http://stackoverflow.com/questions/2271246/how-to-format-drive-in-fat-16-format-using-vb-net-without-user-interaction) – crashmstr Jun 15 '15 at 19:30

1 Answers1

0

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:");
MicroVirus
  • 5,324
  • 2
  • 28
  • 53
  • That seemed to work. Is there anyway for the command prompt to work in the background and have a message popup when the formatting is complete? – Josh Jun 15 '15 at 20:25
  • Also can I specify parameters for a quick format? I tried to just do /Q after format.com but got the same error message again. – Josh Jun 15 '15 at 20:34
  • Additional arguments should be specified in the second parameter, like "H: /Q". The first may only contain the executable file name. To hide the command window is a whole different story, but there are posts on StackOverflow and guides on the internet to help you achieve this. @TonyHinkle: Thanks, I corrected it. – MicroVirus Jun 15 '15 at 20:41