欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > golang基于WMI获取所有外接硬盘(USB,移动硬盘)信息

golang基于WMI获取所有外接硬盘(USB,移动硬盘)信息

2024/10/25 14:26:56 来源:https://blog.csdn.net/a1309525802/article/details/141190184  浏览:    关键词:golang基于WMI获取所有外接硬盘(USB,移动硬盘)信息

golang基于WMI获取所有外接硬盘(USB,移动硬盘)信息

package mainimport ("fmt""regexp""github.com/StackExchange/wmi""github.com/shirou/gopsutil/v3/disk"
)// 定义 WMI 类结构体
type Win32_LogicalDiskToPartition struct {Antecedent stringDependent  string
}// 逻辑盘
type Win32_LogicalDisk struct {DeviceID    string `json:"deviceId"`FreeSpace   string `json:"freeSpace"`FileSystem  string `json:"fileSystem"`Name        string `json:"name"`Size        string `json:"size"`Description string `json:"description"`DriveType   int    `json:"driveType"`VolumeName  string `json:"VolumeName"`
}
type Win32_DiskDriveToDiskPartition struct {Antecedent stringDependent  string
}
type Win32_DiskDrive struct {DeviceID  stringMediaType string
}type DiskInfo struct {DeviceID    string `json:"deviceId"`FreeSpace   string `json:"freeSpace"`FileSystem  string `json:"fileSystem"`Name        string `json:"name"`Size        string `json:"size"`Description string `json:"description"`VolumeName  string `json:"VolumeName"`
}func GetDiskInfo() map[string]string {var drives []Win32_DiskDrivewmi.Query("SELECT DeviceID, MediaType FROM Win32_DiskDrive", &drives)DiskInfo := make(map[string]string)for _, disk := range drives {var deviceID stringfmt.Sscanf(disk.DeviceID, `\\.\%s`, &deviceID)if disk.MediaType == "Fixed hard disk media" {DiskInfo[deviceID] = "本地磁盘"} else {DiskInfo[deviceID] = "外接设备"}}return DiskInfo
}
func getDriveInfo(drive string) DiskInfo {var logicalList []Win32_LogicalDisksqlStr := fmt.Sprintf(`Select * from Win32_LogicalDisk where Name="%s"`, drive)wmi.Query(sqlStr, &logicalList)deviceInfo := DiskInfo{Name:        drive,Size:        "0",FreeSpace:   "0",Description: "外接硬盘",FileSystem:  logicalList[0].FileSystem,DeviceID:    logicalList[0].DeviceID,VolumeName:  logicalList[0].VolumeName,}Usag, err := disk.Usage(drive)if err == nil {deviceInfo.Size = fmt.Sprintf("%d", Usag.Total/1024/1024)deviceInfo.FreeSpace = fmt.Sprintf("%d", Usag.Free/1024/1024)}return deviceInfo
}
func GetUSBList() []DiskInfo {var logicalToPartitions []Win32_LogicalDiskToPartitionvar diskToPartitions []Win32_DiskDriveToDiskPartitionvar USBList []DiskInfo// 查询 Win32_LogicalDiskToPartitionwmi.Query("SELECT Antecedent, Dependent FROM Win32_LogicalDiskToPartition", &logicalToPartitions)// 查询 Win32_DiskDriveToDiskPartitionwmi.Query("SELECT Antecedent, Dependent FROM Win32_DiskDriveToDiskPartition", &diskToPartitions)DiskInfo := GetDiskInfo()// 输出关联的结果for _, ltp := range logicalToPartitions {for _, dtp := range diskToPartitions {if ltp.Antecedent == dtp.Dependent {re := regexp.MustCompile(`DeviceID="([^"]+)"`)match := re.FindStringSubmatch(dtp.Antecedent)if len(match) > 1 {var deviceID stringfmt.Sscanf(match[1], `\\\\.\\%s`, &deviceID)if DiskInfo[deviceID] == "外接设备" {re := regexp.MustCompile(`DeviceID="([^"]+)"`)match = re.FindStringSubmatch(ltp.Dependent)if len(match) > 1 {USBList = append(USBList, getDriveInfo(match[1]))}}}}}}return USBList
}
func main() {fmt.Printf("%+v\n", GetUSBList())
}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com