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())
}
相关推荐
梦想科研社1 分钟前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期1 分钟前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
千穹凌帝4 分钟前
SpinalHDL之结构(二)
开发语言·前端·fpga开发
AlexMercer10127 分钟前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
friklogff8 分钟前
【无标题】云端之C#:全面解析6种云服务提供商的SDK
开发语言·flask·c#
Reese_Cool21 分钟前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言
海里真的有鱼22 分钟前
Spring Boot 项目中整合 RabbitMQ,使用死信队列(Dead Letter Exchange, DLX)实现延迟队列功能
开发语言·后端·rabbitmq
zxctsclrjjjcph38 分钟前
【C语言】常见的C语言概念
c语言·开发语言
小灰灰爱代码43 分钟前
C++——求3个数中最大的数(分别考虑整数、双精度数、长整数的情况),用函数模板来实现。
开发语言·c++·算法
Eiceblue1 小时前
Python 复制Excel 中的行、列、单元格
开发语言·python·excel