58.txt

来自「VB文章集(含API、窗口、数据库、多媒体、系统、文件、等等)」· 文本 代码 · 共 37 行

TXT
37
字号
如何获取硬盘卷标、序列号和文件系统类型?
 

   磁盘序列号在每次软盘或硬盘格式化后都重新生成,并且不回重复。许多程序员用此加密。其实也可以修改该函数,可以得到磁盘卷标和文件系统类型信息。

声明:
Private Declare Function GetVolumeInformation Lib "kernel32.dll" Alias _
  "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal _
  lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, _
  lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
  lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _
  ByVal nFileSystemNameSize As Long) As Long

代码:

Function GetSerialNumber(sRoot As String) As Long
  Dim lSerialNum As Long
  Dim R As Long
  Dim strLabel As String, strType As String
  strLabel = String$(255, Chr$(0))
  '磁盘卷标
  strType = String$(255, Chr$(0))
  '文件系统类型 一般为 FAT 
  R = GetVolumeInformation(sRoot, strLabel, Len(strLabel), _
    lSerialNum, 0, 0, strType, Len(strType))
  GetSerialNumber = lSerialNum
  '在 strLabel 中为 磁盘卷标
  '在 strType 中为 文件系统类型
End Function

用法:

当驱动器不存在时,函数返回 0。如果是个非根目录,也将返回 0:

lSerial = GetSerialNumber("c:\")
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?