在 Visual Basic 中格式化驱动器?
Formatting a drive in 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
问题是 Process.Start
没有在第一个参数中使用命令行参数。使用允许命令行参数的overload。
例如:
Process.Start("format.com", "H:");
我有一个只用可移动驱动器填充的列表框。用户选择要格式化的驱动器,然后程序应格式化这些驱动器。但是,我收到找不到指定文件的消息。这是我的代码。
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
问题是 Process.Start
没有在第一个参数中使用命令行参数。使用允许命令行参数的overload。
例如:
Process.Start("format.com", "H:");