在Vb中新建工程,创建一个DriveListBox控件,并加入如下代码,运行程序,点击DriveListBox,程序会根据DriveListBox里的盘符在桌面上打印出分区号,如1.2的形式,意思是第一个硬盘的第二个分区,你可以更具自己的需要更改下面的代码。 Public DL_R As String Public DL_PN As String Public DL_PD As String Private Sub Get_PN(DL As String) WQL = "Associators of {win32_LogicalDisk='" & DL & "'} where resultClass = Win32_DiskPartition" Set Pattions = GetObject("winmgmts:").ExecQuery(WQL) For Each Pat In Pattions DL_PN = Pat.Name DL_PD = Pat.DiskIndex DL_R = CInt(DL_PD + 1) & ":" & CInt(Right(DL_PN, 1) + 1) Next End SubPrivate Sub Drive1_Change() Get_PN (Left(Drive1.Drive, 2)) Print DL_R End SubPrivate Sub Form_Load() Get_PN (Left(Drive1.Drive, 2)) 'Print DL_PD Print DL_R End Sub
Public DL_R As String
Public DL_PN As String
Public DL_PD As String
Private Sub Get_PN(DL As String)
WQL = "Associators of {win32_LogicalDisk='" & DL & "'} where resultClass = Win32_DiskPartition"
Set Pattions = GetObject("winmgmts:").ExecQuery(WQL)
For Each Pat In Pattions
DL_PN = Pat.Name
DL_PD = Pat.DiskIndex
DL_R = CInt(DL_PD + 1) & ":" & CInt(Right(DL_PN, 1) + 1)
Next
End SubPrivate Sub Drive1_Change()
Get_PN (Left(Drive1.Drive, 2))
Print DL_R
End SubPrivate Sub Form_Load()
Get_PN (Left(Drive1.Drive, 2))
'Print DL_PD
Print DL_R
End Sub