VB.NET 取驱动器列表 / 磁盘

前端之家收集整理的这篇文章主要介绍了VB.NET 取驱动器列表 / 磁盘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在本文中通过GetLogicalDriveStrings函数获取到“逻辑磁盘驱动根路径字串”@H_502_2@

然后我们再把返回的缓冲区中的值进行处理、@H_502_2@

函数示意:@H_502_2@

GetLogicalDriveStrings @H_502_2@取逻辑磁盘驱动根路径字串@H_502_2@@H_502_2@

成功返回“逻辑磁盘驱动根路径字串”大小、否则返回缺省值0@H_502_2@

nBufferLength 欲接收缓冲区尺寸@H_502_2@

lpBuffer 欲接收缓冲区指针@H_502_2@

示例代码:@H_502_2@

Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions

Module MainModule

    Public Declare Function GetLogicalDriveStrings Lib "kernel32.dll" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Integer,ByVal lpBuffer As String) As Integer

    Sub Main()
        Console.Title = "取磁盘驱动器列表"
        Dim len As Integer = GetLogicalDriveStrings(0,Nothing)
        If (len <= 0) Then
            Throw New Exception("Unable to get buffer length.")
        End If
        Dim buffer As String = Space(len)
        If (GetLogicalDriveStrings(len,buffer) <= 0) Then
            Throw New Exception("Unable to get the list of disks.")
        End If
        Dim matchs = Regex.Matches(buffer,"[A-Z|a-z]+\:\\")
        For Each match As Match In matchs
            Console.WriteLine(match.Value)
        Next
        Console.ReadKey(False)
    End Sub

End Module

猜你在找的VB相关文章