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())
}
相关推荐
MessiGo24 分钟前
Javascript 编程基础(5)面向对象 | 5.1、构造函数实例化对象
开发语言·javascript·原型模式
大霞上仙27 分钟前
nonlocal 与global关键字
开发语言·python
galaxy_strive32 分钟前
绘制饼图详细过程
开发语言·c++·qt
黑客老李1 小时前
JavaSec | SpringAOP 链学习分析
java·运维·服务器·开发语言·学习·apache·memcached
开开心心就好1 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
特立独行的猫a2 小时前
Nuxt.js 中的路由配置详解
开发语言·前端·javascript·路由·nuxt·nuxtjs
勤奋的知更鸟2 小时前
Java编程之原型模式
java·开发语言·原型模式
珂朵莉MM2 小时前
2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家
java·开发语言·人工智能·算法·职场和发展·机器人
香蕉炒肉2 小时前
Java优化:双重for循环
java·开发语言
傍晚冰川3 小时前
FreeRTOS任务调度过程vTaskStartScheduler()&任务设计和划分
开发语言·笔记·stm32·单片机·嵌入式硬件·学习