在本文中通过,“GetDiskFreeSpaceEx / 取磁盘空闲空间EX”获取到关于磁盘的容量信息
如“磁盘总容量”、“磁盘空闲容量”、“磁盘已用容量”、“调用方可用容量”,有必要著名下
“调用方可用容量”与“磁盘空闲容量”两者近乎相同,但不代表完全相同 两者只是说反馈的
值相近或相同,不代表一定说含义完全等价、
函数示意:
GetDiskFreeSpaceEx 取磁盘空闲空间扩展,如果成功返回非0,否则返回0
lpRootPathName 欲取信息盘符根路径
lpFreeBytesAvailableToCaller 调用方可用容量
lpTotalNumberOfBytes 磁盘总容量
lpTotalNumberOfFreeBytes 磁盘空闲容量
StrFormatByteSize64 格式化字符串(字节),成功返回真,否则返回假
qdw 64位二进制数据块
szBuf 字符缓冲区
uiBufSize 缓冲区尺寸
示例代码:
Imports System.Runtime.InteropServices Imports System.Text.RegularExpressions Module MainModule Public Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String,ByRef lpFreeBytesAvailableToCaller As Long,ByRef lpTotalNumberOfBytes As Long,ByRef lpTotalNumberOfFreeBytes As Long) As Boolean Public Declare Function StrFormatByteSize64 Lib "shlwapi.dll" Alias "StrFormatByteSize64A" (ByVal qdw As Long,ByVal szBuf As String,ByVal uiBufSize As Integer) As Boolean Public Const NULL As Long = 0L Function GetTotalDiskSize(strRootPathName As String) As Long ' 取磁盘总容量 Dim lngRsltSize As Long = NULL If (GetDiskFreeSpaceEx(strRootPathName,NULL,lngRsltSize,NULL)) Then Return lngRsltSize End If Throw New Exception("Unable to get toal disk size.") End Function Function GetFreeDiskSize(strRootPathName As String) As Long ' 取磁盘空闲容量 Dim lngRsltSize As Long = NULL If (GetDiskFreeSpaceEx(strRootPathName,lngRsltSize)) Then Return lngRsltSize End If Throw New Exception("Unable to get free disk size.") End Function Function GetUsedDiskSpace(strRootPathName As String) As Long ' 取已使用磁盘容量 Dim lngRsltSize As Long = GetTotalDiskSize(strRootPathName) - GetFreeDiskSize(strRootPathName) If (lngRsltSize < 0) Then Throw New Exception("Unable to get used disk space.") End If Return lngRsltSize End Function Function GetAvailableToCallerSize(strRootPathName As String) ' 取可用磁盘容量 Dim lngRsltSize As Long = NULL If (GetDiskFreeSpaceEx(strRootPathName,NULL)) Then Return lngRsltSize End If Throw New Exception("Unable to get available to caller size.") End Function Function StrFormatByteSize64(ByVal value As Long) As String Dim str = Space(64) If (StrFormatByteSize64(value,str,64) = False) Then Return 0 End If Return str.TrimEnd() End Function Sub Main() Console.Title = "磁盘容量信息" Console.WriteLine("总容量:" + StrFormatByteSize64(GetTotalDiskSize("C:\"))) Console.WriteLine("空闲容量:" + StrFormatByteSize64(GetFreeDiskSize("C:\"))) Console.WriteLine("已用容量:" + StrFormatByteSize64(GetUsedDiskSpace("C:\"))) Console.WriteLine("可用容量:" + StrFormatByteSize64(GetAvailableToCallerSize("C:\"))) Console.ReadKey(False) End Sub End Module原文链接:https://www.f2er.com/vb/257213.html