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

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

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

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

函数示意:

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

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

nBufferLength 欲接收缓冲区尺寸

lpBuffer 欲接收缓冲区指针

示例代码

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
原文链接:https://www.f2er.com/vb/257214.html

猜你在找的VB相关文章