在本文中通过,“GetdiskFreeSpaceEx / 取磁盘空闲空间EX”获取到关于磁盘的容量信息
如“磁盘总容量”、“磁盘空闲容量”、“磁盘已用容量”、“调用方可用容量”,有必要著名下
“调用方可用容量”与“磁盘空闲容量”两者近乎相同,但不代表完全相同 两者只是说反馈的
值相近或相同,不代表一定说含义完全等价、
函数示意:
GetdiskFreeSpaceEx 取磁盘空闲空间扩展,如果成功返回非0,否则返回0
lpRootPathname 欲取信息盘符根路径
lpFreeBytesAvailabletoCaller 调用方可用容量
lpTotalNumberOfBytes 磁盘总容量
lpTotalNumberOfFreeBytes 磁盘空闲容量
StrFormatByteSize64 格式化字符串(字节),成功返回真,否则返回假
qDW 64位二进制数据块
szBuf 字符缓冲区
uiBufSize 缓冲区尺寸
示例代码:
imports System.Runtime.InteropServicesimports System.Text.RegularExpressionsModule 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 SubEnd Module总结
以上是内存溢出为你收集整理的VB.NET 取磁盘容量信息全部内容,希望文章能够帮你解决VB.NET 取磁盘容量信息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)