0

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

0 回答 0