我正在制作一个程序,用于格式化用户选择的闪存驱动器。为此,我使用format.com
进程并发送"输入"关键所以这个过程将是全自动的。为了使其正常工作,我推迟了按下"输入"键入半秒钟以确保将其发送到命令提示符。由于某种原因,一个命令提示符窗口短暂打开然后关闭,这似乎产生了我正在尝试执行的第二个命令的问题,然后文件将从另一个闪存驱动器复制到格式化的文件。 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