官方API:Win32_LogicalDisk 類 - Win32 apps | Microsoft Docs
import (
"fmt"
"github.com/StackExchange/wmi"
"strings"
"time"
)
// Win32_LogicalDisk 本地磁盤類類型育瓜、名稱不可更改
type Win32_LogicalDisk struct {
Access uint16
Availability uint16
BlockSize uint64
Caption string
Compressed bool
ConfigManagerErrorCode uint32
ConfigManagerUserConfig bool
CreationClassName string
Description string
DeviceID string
DriveType uint32
ErrorCleared bool
ErrorDescription string
ErrorMethodology string
FileSystem string
FreeSpace uint64
InstallDate string
LastErrorCode uint32
MaximumComponentLength uint32
MediaType uint32
Name string
NumberOfBlocks uint64
PNPDeviceID string
PowerManagementCapabilities []uint16
PowerManagementSupported bool
ProviderName string
Purpose string
QuotasDisabled bool
QuotasIncomplete bool
QuotasRebuilding bool
Size string
Status string
StatusInfo uint16
SupportsDiskQuotas bool
SupportsFileBasedCompression bool
SystemCreationClassName string
SystemName string
VolumeDirty bool
VolumeName string
VolumeSerialNumber string
}
// GetRemoveDisk 獲取可移動磁盤
func GetRemoveDisk() {
// 創(chuàng)建wmi客戶端
s, err := wmi.InitializeSWbemServices(wmi.DefaultClient)
defer s.Close()
if err != nil {
log.Fatalf("InitializeSWbemServices: %s", err)
}
// 查詢的數(shù)據(jù)類型
var dst []Win32_LogicalDisk
// 查詢的條件
q := wmi.CreateQuery(&dst, "WHERE DriveType=2")
// 查詢信息
errQuery := s.Query(q, &dst)
if errQuery != nil {
fmt.Println("err", errQuery)
}
for _, value := range dst {
fmt.Printf("%+v", value)
}
}