vb获取系统信息:VB中获取逻辑磁盘的信息



  我们在编程时候有时会需要得到系统中逻辑磁盘些信息如磁盘卷标、磁盘序列号、空间大小、剩余空间等这些信息直接使用VB提供显然是无法得到但是借助于VB对WINDOWS API支持使用GetVolumeInformation和 GetDiskFreeSpace这两个API我们就可以很容易得到磁盘相关信息

  先来谈谈这两个GetVolumeInformation用于获取和个磁盘卷有关信息包括磁盘卷标、磁盘序列号、文件全路径名中“\\”和“\\”的间部分长度、文件系统名称以及文件系统某些特性GetDiskFreeSpace用于获取和个磁盘组织有关信息以及了解剩余空间容量包括磁盘上总簇数、剩余簇数、个簇内扇区数和个扇区内字节数

  接下来看看具体例子

  进入VB中在窗体上加入个驱动器列表框(DriveListBox)和个列表框(ListBox)然后加入以下脚本:

Option Explicit
Private Declare Function GetVolumeInformation
Lib \"kernel32\" Alias
\"GetVolumeInformationA\" (ByVal lpRootPathName As
String, ByVal lpVolumeNameBuffer As
String, ByVal nVolumeNameSize As Long,
lpVolumeSerialNumber As Long,
lpMaximumComponentLength As Long,
lpFileFlags As Long, ByVal
lpFileNameBuffer As String,
ByVal nFileNameSize As Long) As Long
Private Declare Function GetDiskFreeSpace
Lib \"kernel32\" Alias \"GetDiskFreeSpaceA\"
(ByVal lpRootPathName As String, lpSectorsPerCluster
As Long, lpBytesPerSector As Long,
lpNumberOfFreeClusters As Long,
lpTotalNumberOfClusters As Long) As Long
Private Const FS_CASE_IS_PRESERVED = &H2
Private Const FS_CASE_SENSITIVE = &H1
Private Const FS_UNICODE_STORED_ON_
DISK = &H4
Private Const FS_PERSISTENT_ACLS = &H8
Private Const FS_FILE_COMPRESSION = &H10
Private Const FS_VOL_IS_COMPRESSED =
&H8000

Private Sub Drive1_Change
Dim Volume As String, SysName As String
Dim SerialNum As Long, SysFlags As Long,
ComponentLength As Long, Res As Long
Dim SectorsPerCluster As Long, BytesPerSector
As Long, NumberOfFreeClustors As
Long, TotalNumberOfClustors As Long
Dim FreeBytes As Long, TotalBytes As Long,
PercentFree As Long, Dl As Long
Dim DrvName As String
List1.Clear
Volume = String(256, 0)
SysName = String(256, 0)
DrvName = Left(Drive1.Drive, 2) & \"\\\"
Res = GetVolumeInformation(DrvName,
Volume, 255, SerialNum, _
ComponentLength, SysFlags, SysName, 255)
If Res = 0 Then
List1.AddItem \"不能得到磁盘信息\"
Else
List1.AddItem \"卷标: \" & Trim(Volume)
List1.AddItem \"序列号: \" & SerialNum
List1.AddItem \"成分长度: \" & ComponentLength
List1.AddItem \"文件系统: \" & Trim(SysName)
Dl = GetDiskFreeSpace(DrvName,
SectorsPerCluster, BytesPerSector,
NumberOfFreeClustors, TotalNumberOfClustors)
List1.AddItem \"每簇中扇区数: \"
& Format(SectorsPerCluster, \"#,0\")[Page]
List1.AddItem \"每扇区中字节数: \"
& Format(BytesPerSector, \"#,0\")
List1.AddItem \"总簇数: \"
& Format(TotalNumberOfClustors, \"#,0\")
List1.AddItem \"剩余簇数: \"
& Format(NumberOfFreeClustors, \"#,0\")
TotalBytes = TotalNumberOfClustors *
SectorsPerCluster * BytesPerSector
List1.AddItem \"总字节数:
\" & Format(TotalBytes, \"#,0\")
FreeBytes = NumberOfFreeClustors
* SectorsPerCluster * BytesPerSector
List1.AddItem \"剩余字节数: \"
& Format(FreeBytes, \"#,0\")
If SysFlags And FS_CASE_IS_PRESERVED Then
List1.AddItem \"文件名大小写记录于文件系统\"
End If
If SysFlags And FS_CASE_SENSITIVE Then
List1.AddItem \"文件名要区分大小写\"
End If
If SysFlags And FS_UNICODE_STORED_
ON_DISK Then
List1.AddItem \"文件名保存为 Unicode 格式\"
End If
If SysFlags And FS_PERSISTENT_ACLS Then
List1.AddItem \"文件系统支持文件访问
控制列表(ACL)安全机制\"
End If
If SysFlags And FS_FILE_COMPRESSION Then
List1.AddItem \"文件系统支持逐文件地进行文件压缩\"
End If
If SysFlags And FS_VOL_IS_COMPRESSED Then
List1.AddItem \"整个磁盘卷都是压缩\"
End If
End If
End Sub

Private Sub Form_Load


Call Drive1_Change
End Sub

  运行后选择驱动器列表框中区别驱动器列表框中就会显示出该驱动器相应信息以上在VB5.0、VB6.0及WINDOWS 98中运行通过
Tags:  逻辑磁盘管理器 逻辑磁盘 vb获取网卡信息 vb获取系统信息

延伸阅读

最新评论

发表评论