VB中获取逻辑磁盘的信息

发表于:2007-05-25来源:作者:点击数: 标签:磁盘逻辑获取信息
我们在编程的时候有时会需要得到系统中逻辑磁盘的一些信息,如磁盘卷标、磁盘序列号、空间大
我们在编程的时候有时会需要得到系统中逻辑磁盘的一些信息,如磁盘卷标、磁盘序列号、空间大小、剩余空间等,这些信息直接使用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,
lpFileSystemFlags As Long, ByVal
lpFileSystemNameBuffer As String,
ByVal nFileSystemNameSize 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")
        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中运行通过。
  

 

转载自计算机世界日报 (文/严冬)

原文转自:http://www.ltesting.net