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

本文介绍如何使用GetLogicalDriveStrings函数从Windows系统中获取逻辑磁盘驱动器的根路径字符串,并对其进行处理以显示磁盘驱动器列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在本文中通过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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值