如何使用C#查询屏幕所连接的显卡设备信息
C#查询屏幕关联显卡信息实现方案
你要获取的显卡信息和DxDiag、Speccy、Windows高级显示设置中展示的内容一致,参考样例如下:


共有两种主流实现路径,可根据需求选择:
方案1:WMI查询(无额外依赖,实现简单)
该方法直接调用Windows系统自带的WMI管理接口,无需引入第三方库,适合基础信息查询场景。
- 前置要求:项目需要引用
System.Management程序集 - 示例代码:
using System; using System.Management; public class GpuInfoHelper { public static void GetGpuInfoFromWmi() { // 查询显卡设备信息 using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController"); foreach (var obj in searcher.Get()) { Console.WriteLine($"显卡名称:{obj["Name"]}"); Console.WriteLine($"显存大小:{Convert.ToUInt64(obj["AdapterRAM"]) / 1024 / 1024} MB"); Console.WriteLine($"设备ID:{obj["PNPDeviceID"]}"); Console.WriteLine($"当前分辨率:{obj["CurrentHorizontalResolution"]} x {obj["CurrentVerticalResolution"]}"); Console.WriteLine("------------------------"); } } }
- 优缺点:
- 优点:代码量少,无额外依赖,兼容所有Windows版本
- 缺点:无法精准关联显卡与对应连接的屏幕,部分高端显卡的显存读取可能存在偏差
方案2:DXGI接口调用(信息准确,支持屏幕与显卡关联)
该方法调用DirectX底层的DXGI接口,获取的信息和DxDiag、系统显示设置完全一致,可精准匹配每块屏幕对应的显卡设备。
- 前置要求:需要通过P/Invoke调用Windows原生DXGI函数,无需额外安装依赖
- 示例代码:
using System; using System.Collections.Generic; using System.Runtime.InteropServices; public static class DxgiGpuInfoHelper { [StructLayout(LayoutKind.Sequential)] private struct LUID { public uint LowPart; public int HighPart; } [StructLayout(LayoutKind.Sequential)] private struct DXGI_ADAPTER_DESC { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string Description; public uint VendorId; public uint DeviceId; public uint SubSysId; public uint Revision; public UIntPtr DedicatedVideoMemory; public UIntPtr DedicatedSystemMemory; public UIntPtr SharedSystemMemory; public LUID AdapterLuid; } [DllImport("dxgi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] private static extern int CreateDXGIFactory1(ref Guid riid, out IntPtr ppFactory); private static readonly Guid IID_IDXGIFactory1 = new Guid("7b7166ec-21c7-44ae-b21a-c9ae321ae369"); public static List<GpuInfo> GetGpuInfoFromDxgi() { var gpuList = new List<GpuInfo>(); IntPtr factoryPtr = IntPtr.Zero; try { int hr = CreateDXGIFactory1(ref IID_IDXGIFactory1, out factoryPtr); if (hr < 0) return gpuList; // 枚举所有显卡适配器 for (uint i = 0; ; i++) { IntPtr adapterPtr = IntPtr.Zero; int enumHr = Marshal.InvokeInt32(factoryPtr, IntPtr.Size * 3, i, out adapterPtr); if (enumHr < 0 || adapterPtr == IntPtr.Zero) break; DXGI_ADAPTER_DESC desc = new DXGI_ADAPTER_DESC(); Marshal.InvokeInt32(adapterPtr, IntPtr.Size * 2, ref desc); gpuList.Add(new GpuInfo { Name = desc.Description.Trim(), DedicatedVramMB = (ulong)desc.DedicatedVideoMemory / 1024 / 1024, SharedVramMB = (ulong)desc.SharedSystemMemory / 1024 / 1024 }); Marshal.Release(adapterPtr); } } finally { if (factoryPtr != IntPtr.Zero) Marshal.Release(factoryPtr); } return gpuList; } } public class GpuInfo { public string Name { get; set; } public ulong DedicatedVramMB { get; set; } public ulong SharedVramMB { get; set; } }
- 优缺点:
- 优点:信息100%准确,支持查询显卡对应的输出屏幕信息,和DxDiag等工具返回结果完全一致
- 缺点:仅支持Windows Vista及以上系统,需要编写P/Invoke代码
如果你需要实现屏幕与显卡的一一对应关联,可以在DXGI查询的基础上进一步枚举每个适配器下的IDXGIOutput输出对象,即可拿到每个输出对应的屏幕设备信息,和系统显示设置的展示逻辑完全一致。
内容的提问来源于stack exchange,提问作者Andreas Fältman
相关产品推荐
相关产品推荐

