VB代码读取指定盘符的物理磁盘盘号和分区号的示例:
Sub GetDiskInfo(driveLetter As String)
Dim wmi As Object
Dim objFSO As Object
Dim objDrive As Object
Dim strComputer As String
Dim strQuery As String
Dim colItems As Object
Dim objItem As Object
' 获取计算机名称
strComputer = "."
' 创建WMI对象
Set wmi = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' 创建FSO对象
Set objFSO = CreateObject("Scripting.FileSystemObject")
' 通过WMI查询指定驱动器的分区信息
strQuery = "SELECT * FROM Win32_DiskPartition WHERE Name LIKE """ & driveLetter & "%"""
Set colItems = wmi.ExecQuery(strQuery)
' 遍历查询结果
For Each objItem In colItems
' 获取物理磁盘盘号
Dim diskNumber As Integer
diskNumber = objItem.DiskIndex
' 获取分区号
Dim partitionNumber As Integer
partitionNumber = objItem.Index
' 输出结果
MsgBox "磁盘" & diskNumber & ",分区" & partitionNumber
Next objItem
' 清理对象
Set objItem = Nothing
Set colItems = Nothing
Set objFSO = Nothing
Set wmi = Nothing
End Sub
Sub Test()
' 调用函数,传入需要查询的驱动器盘符
GetDiskInfo "C:"
End Sub
Option Explicit
Sub WriteIsoToDisk(isoFilePath As String, driveLetter As String)
Dim objShell As Object
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim objArguments As Object
Dim command As String
' 创建Shell对象
Set objShell = CreateObject("WScript.Shell")
' 创建FSO对象
Set objFSO = CreateObject("Scripting.FileSystemObject")
' 检查指定ISO文件是否存在
If Not objFSO.FileExists(isoFilePath) Then
MsgBox "ISO文件不存在!"
Exit Sub
End If
' 构造执行命令
command = "cmd /c ""C:\Program Files\7-Zip\7z.exe"" e " & Chr(34) & isoFilePath & Chr(34) & " -o" & driveLetter
' 执行命令并等待完成
objShell.Run command, 0, True
' 清理对象
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
Set objShell = Nothing
MsgBox "ISO文件已成功写入磁盘 " & driveLetter
End Sub
Sub Test()
' 调用函数,传入ISO文件路径和目标磁盘盘符
WriteIsoToDisk "C:\path\to\your\iso\file.iso", "D:"
End Sub
在`WriteIsoToDisk`函数中,使用了7-Zip程序(默认安装在C:\Program Files\7-Zip\7z.exe)。函数会将指定的ISO文件解压缩到目标磁盘盘符(例如:"D:")。请根据实际情况修改函数中的7-Zip程序路径和ISO文件路径。运行`Test`子程序即可进行测试。请注意,需要已经安装并配置好7-Zip才能成功运行。