开发者

VB.NET get next available drive letter

开发者 https://www.devze.com 2023-03-16 17:46 出处:网络
I find a lot of VB6 examples out there, and a few C# examples, but no concrete solution in VB.NET. Simply put, I need to get the next available drive lett开发者_StackOverflow社区er in as few lines of

I find a lot of VB6 examples out there, and a few C# examples, but no concrete solution in VB.NET. Simply put, I need to get the next available drive lett开发者_StackOverflow社区er in as few lines of code as possible. Anyone have a good example?


Try something like this:

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

Source: CodeKeep


If you've got C# code, then put it through the developerfusion coverter - that should get you close (although you'll probably have to tweak it).


To list logical drives Try this....

Includes full code....

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
0

精彩评论

暂无评论...
验证码 换一张
取 消