go-wmi获取可移动磁盘信息

官方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() {
    // 创建wmi客户端
    s, err := wmi.InitializeSWbemServices(wmi.DefaultClient)
    defer s.Close()
    if err != nil {
        log.Fatalf("InitializeSWbemServices: %s", err)
    }
    // 查询的数据类型
    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)
    }
}

你可能感兴趣的:(go-wmi获取可移动磁盘信息)