I am making a program that will format flash drives that are selected by the user. For this I am using the format.com
process and sending the "Enter" key so the process will be fully automatic. For this to work correctly, I delay the press of the "Enter" key by half a second to ensure it is being sent to the command prompt. For some reason, one command prompt window opens briefly and then closes which seems to create an issue with the second command I am trying to execute where files will then be copied from another flash drive to the formatted ones. Visual Basic completely freezes and the only way to gain control is by hitting Ctrl + Alt + Del. I even have the copying being delayed by 20 seconds to ensure the formatting process is complete. Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs)
'Variables initialized
Dim i As Integer
Dim 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
drives(i) = ListBox1.SelectedItems(i).ToString.Substring(0, 2)
If i = Not drives.Length Then
DrvsToFormat = DrvsToFormat & " " & drives(i) & ","
Else
DrvsToFormat = DrvsToFormat & " " & drives(i)
End If
Next
'Gets the current date and formats it as "mm-dd"
Dim currentDate As Date = Date.Today()
Dim formattedDate As String = currentDate.ToString("MM-dd")
'Prompts the user to ensure they wish to format the drives
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
'Iterates through all selected drive, performs quick format as NTFS, and names the drive with the current date
'Sends enter key in order to continue formatting in cmd prompt
For i = 0 To drives.Length - 1
Process.Start("format.com", drives(i) & "/Q /FS:NTFS /V:" & formattedDate)
Threading.Thread.Sleep(500)
SendKeys.Send("{ENTER}")
Threading.Thread.Sleep(20000)
Process.Start("cmd.exe", "Xcopy " & MasterFD.masterDrive & " " & drives(i) & "/e ")
Next
End If
End Sub