using System; using System.Collections.Generic; using System.Linq; using System.Management; using System.Text; using System.Threading.Tasks; namespace LB_SmartVisionCommon { public class DiskInfoHelper { public static double GetFreeSpaceGB(string driveName) { try { DriveInfo drive = new DriveInfo(driveName); return drive.IsReady ? Math.Round((double)drive.AvailableFreeSpace / (1024 * 1024 * 1024), 2) : -1; } catch { return -1; } } public static Dictionary GetDiskDetails(string driveName) { var result = new Dictionary(); try { DriveInfo drive = new DriveInfo(driveName); if (drive.IsReady) { result["DriveName"] = drive.Name; result["TotalSizeGB"] = Math.Round((double)drive.TotalSize / (1024 * 1024 * 1024), 2); result["FreeSpaceGB"] = Math.Round((double)drive.AvailableFreeSpace / (1024 * 1024 * 1024), 2); result["DriveFormat"] = drive.DriveFormat; result["DriveType"] = drive.DriveType.ToString(); } } catch (Exception ex) { result["Error"] = ex.Message; } return result; } /// /// 获取GPU信息,包含系统索引和CUDA标准的NVIDIA索引 /// /// GPU信息列表 public static List GetGpuInfoWithCudaIndex() { var gpuList = new List(); int systemIndex = 0; // 系统显示适配器索引(包含所有显卡) int cudaIndex = 0; // CUDA专属索引(仅NVIDIA GPU,从0开始) // WMI查询所有显示适配器 ManagementObjectSearcher searcher = new ManagementObjectSearcher( "root\\CIMV2", "SELECT Name, PNPDeviceID FROM Win32_VideoController"); foreach (ManagementObject queryObj in searcher.Get()) { string gpuName = queryObj["Name"]?.ToString() ?? "未知设备"; string pnpId = queryObj["PNPDeviceID"]?.ToString() ?? string.Empty; bool isNvidia = gpuName.Contains("NVIDIA", StringComparison.OrdinalIgnoreCase) || pnpId.Contains("VEN_10DE", StringComparison.OrdinalIgnoreCase); var gpuInfo = new GpuInfo { GpuName = gpuName, SystemIndex = systemIndex, IsNvidia = isNvidia, CudaIndex = -1 // 非NVIDIA GPU默认-1 }; // 仅NVIDIA GPU分配CUDA索引 if (isNvidia) { gpuInfo.CudaIndex = cudaIndex; cudaIndex++; } gpuList.Add(gpuInfo); systemIndex++; } return gpuList; } } /// /// GPU信息模型 /// public class GpuInfo { public string GpuName { get; set; } // GPU名称 public int SystemIndex { get; set; } // 系统显示适配器索引(WMI枚举) public int CudaIndex { get; set; } // CUDA设备索引(软件显示的索引) public bool IsNvidia { get; set; } // 是否为NVIDIA GPU } }