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

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

golang 复制代码
package main

import (
	"fmt"
	"regexp"

	"github.com/StackExchange/wmi"
	"github.com/shirou/gopsutil/v3/disk"
)

// 定义 WMI 类结构体
type Win32_LogicalDiskToPartition struct {
	Antecedent string
	Dependent  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 string
	Dependent  string
}
type Win32_DiskDrive struct {
	DeviceID  string
	MediaType 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_DiskDrive
	wmi.Query("SELECT DeviceID, MediaType FROM Win32_DiskDrive", &drives)
	DiskInfo := make(map[string]string)
	for _, disk := range drives {
		var deviceID string
		fmt.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_LogicalDisk
	sqlStr := 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_LogicalDiskToPartition
	var diskToPartitions []Win32_DiskDriveToDiskPartition
	var USBList []DiskInfo
	// 查询 Win32_LogicalDiskToPartition
	wmi.Query("SELECT Antecedent, Dependent FROM Win32_LogicalDiskToPartition", &logicalToPartitions)

	// 查询 Win32_DiskDriveToDiskPartition
	wmi.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 string
					fmt.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())
}
相关推荐
Fairy_sevenseven1 分钟前
[1]python爬虫入门,爬取豆瓣电影top250实践
开发语言·爬虫·python
珹洺1 小时前
Java-Spring入门指南(二十一)Thymeleaf 视图解析器
java·开发语言·spring
Predestination王瀞潞1 小时前
类的多态(Num020)
开发语言·c++
Predestination王瀞潞1 小时前
类的继承(Num019)
开发语言·c++
万粉变现经纪人1 小时前
如何解决 pip install -r requirements.txt 无效可编辑项 ‘e .‘(-e 拼写错误)问题
开发语言·python·r语言·beautifulsoup·pandas·pip·scipy
say_fall1 小时前
精通C语言(2.结构体)(内含彩虹)
c语言·开发语言·windows
潇凝子潇1 小时前
在使用Nacos作为注册中心和配置中心时,如何解决服务发现延迟或配置更新不及时的问题
开发语言·python·服务发现
纵横八荒2 小时前
Java基础加强13-集合框架、Stream流
java·开发语言
欣然~3 小时前
百度地图收藏地址提取与格式转换工具 说明文档
java·开发语言·dubbo
William_cl3 小时前
C# MVC 修复DataTable时间排序以及中英文系统的时间筛选问题
开发语言·c#·mvc