我发现了很多VB6示例和一些C#示例,但在VB.NET中没有具体的解决方案。简而言之,我需要尽可能少的代码行来获取下一个可用的驱动器号。有人有一个很好的例子吗?
答案 0 :(得分:1)
尝试这样的事情:
Public Function FindNextAvailableDriveLetter() As String
' build a string collection representing the alphabet
Dim alphabet As New StringCollection()
Dim lowerBound As Integer = Convert.ToInt16("a"C)
Dim upperBound As Integer = Convert.ToInt16("z"C)
For i As Integer = lowerBound To upperBound - 1
Dim driveLetter As Char = ChrW(i)
alphabet.Add(driveLetter.ToString())
Next
' get all current drives
Dim drives As DriveInfo() = DriveInfo.GetDrives()
For Each drive As DriveInfo In drives
alphabet.Remove(drive.Name.Substring(0, 1).ToLower())
Next
If alphabet.Count > 0 Then
Return alphabet(0)
Else
Throw New ApplicationException("No drives available.")
End If
End Function
来源:CodeKeep
答案 1 :(得分:0)
如果您有C#代码,那么请通过developerfusion coverter - 这应该会让您关闭(尽管您可能需要调整它)。
答案 2 :(得分:0)
列出逻辑驱动器试试这个....
包含完整的代码......
Imports System.Management
Public Class Form1
Dim WithEvents w As ManagementEventWatcher
Dim q As WqlEventQuery
Delegate Sub LoadList()
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
w.Stop()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
q = New WqlEventQuery
q.QueryString = "SELECT * FROM" & _
" __InstanceCreationEvent WITHIN 1 " & _
"WHERE TargetInstance isa ""Win32_LogicalDisk"""
w = New ManagementEventWatcher(q)
w.Start()
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
LoadDriveList()
End Sub
Private Sub LoadDriveList()
ListBox1.Items.Clear()
Dim moReturn As Management.ManagementObjectCollection
Dim moSearch As Management.ManagementObjectSearcher
Dim mo As Management.ManagementObject
moSearch = New Management.ManagementObjectSearcher("Select * from Win32_LogicalDisk")
moReturn = moSearch.Get
For Each mo In moReturn
ListBox1.Items.Add(mo("Name").ToString)
Next
End Sub
Private Sub w_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles w.EventArrived
ListBox1.Invoke(New LoadList(AddressOf LoadDriveList))
End Sub
End Class