2015-06-15 81 views
0

我有一个只能用可移动驱动器填充的ListBox。用户选择要格式化的驱动器,然后程序应格式化这些驱动器。但是,我收到无法找到指定文件的消息。这是我的代码。在Visual Basic中格式化驱动器?

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 

+0

的就是你得到确切的错误消息,到底是哪行导致的呢? (我在'Process.Start'上投注,我也打赌快速浏览一下调试器中的驱动器(j)'会清楚地告诉你是什么导致了这个问题。) –

+0

[你搜索了吗?](http://stackoverflow.com/questions/2271246/how-to-format-drive-in-fat-16-format-using-vb-net-without-user-interaction) – crashmstr

回答

0

的问题是,Process.Start不采取命令行参数中第一个参数。使用允许命令行参数的overload

例如:

Process.Start("format.com", "H:"); 
+1

'格式。 com' not'format.exe' –

+0

这似乎工作。反正有命令提示符在后台工作,并且在格式化完成后弹出一条消息? – Josh

+0

也可以指定快速格式的参数吗?我试图在format.com后执行/ Q,但又收到了相同的错误消息。 – Josh