ユーザーが選択したフラッシュ ドライブをフォーマットするプログラムを作成しています。このために、format.com
プロセスを使用して「Enter」キーを送信しているため、プロセスは完全に自動化されます。これが正しく機能するように、「Enter」キーを押すのを 0.5 秒遅らせて、コマンド プロンプトに確実に送信されるようにします。何らかの理由で、1 つのコマンド プロンプト ウィンドウが短時間開いてから閉じます。これにより、ファイルが別のフラッシュ ドライブからフォーマットされたものにコピーされる場所で実行しようとしている 2 番目のコマンドで問題が発生するようです。Visual Basic が完全にフリーズし、コントロールを取得する唯一の方法は Ctrl + Alt + Del を押すことです。書式設定プロセスが確実に完了するように、コピーを 20 秒遅らせることさえあります。これが私のコードです:
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