C#实现获取电脑硬件显卡核心代号信息
作者:全栈小5
背景
在上一篇文章已经通过C#获取到电脑显卡信息,在获取显卡核心代号信息时是空值,
因此此篇文章通过另外一种方式获取到核心代号信息。
第三方工具
GPU-Z 是一种常用的免费工具,用于获取和显示计算机中显卡(图形处理单元,GPU)的详细信息。它可以提供有关显卡型号、驱动版本、显存大小、传感器数据(如温度、风扇转速等)以及其他相关硬件信息的实时读数。
显卡信息
GPU-Z 显示有关显卡的详细信息,包括显卡制造商、型号、显存类型和大小、时钟速度等。
传感器监控
它可以读取和显示显卡的各种传感器数据,如温度、负载、风扇速度、功耗等。这些数据有助于你了解显卡的性能和状态。
即时读取和更新
GPU-Z 可以实时读取硬件信息,并在显卡或驱动程序更改时自动更新。
导出和共享
你可以将 GPU-Z 的输出导出为文本文件,方便与他人共享或保存以供后续分析。
支持多款显卡
GPU-Z 可以识别和显示各种显卡品牌和型号的信息,包括 NVIDIA、AMD 和 Intel 等生产商的显卡。
实现代码
核心代号的获取可能因显卡厂商和驱动程序的不同而有所差异。
以下是一种获取核心代号的备选方法,它使用了GPU-Z工具的命令行版本:
1.首先,下载并安装GPU-Z工具的命令行版本(GPU-Z CLI)。你可以从GPU-Z官方网站(https://www.techpowerup.com/gpuz/)上找到该工具,并确保将其路径添加到系统环境变量中。
2.在C#代码中,使用System.Diagnostics.Process类来执行GPU-Z命令并捕获输出。
using System; using System.Diagnostics; class Program { static void Main() { // 执行GPU-Z命令行,并捕获输出结果 Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "GPU-Z.exe", // GPU-Z命令行的路径 Arguments = "-dump=nogpu -txt=gpuz.txt", // 指定输出格式为文本文件 RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); // 读取输出文件中的核心代号 string outputFilePath = "gpuz.txt"; // 指定输出文件的路径 string[] lines = System.IO.File.ReadAllLines(outputFilePath); string coreName = ""; foreach (string line in lines) { if (line.StartsWith("GPU")) { // 核心代号所在行的格式为:GPU: [核心代号] coreName = line.Split(':')[1].Trim(); break; } } Console.WriteLine("核心代号: " + coreName); } }
知识补充
除了上文的方法,小编还为大家整理了C#获取计算机硬件的参数信息的相关方法,希望对大家有所帮助
下面是读取硬件参数的代码:
public class OSInformation { public string OSName { get; set; } public string OSType { get; set; } public string OSSerialNumber { get; set; } public string ComputerName { get; set; } public string LogonUserName { get; set; } } public class HInformaiton { public string MainboardName { get; set; } public string MemorySize { get; set; } public string HarddiskSize { get; set; } public string VideocardName { get; set; } public string SoundcardName { get; set; } public string NetworkcardName { get; set; } public string ManufacturerName { get; set; } public string ManufactureDate { get; set; } } public class SInformation { public string VRV { get; set; } public string QAX { get; set; } public string WPS { get; set; } } public class WInformation { public string IP { get; set; } public string Subnetmask { get; set; } public string Gateway { get; set; } } class HardwareInformation { public static string GetJsonStr() { string ReturnStrJson = ""; var HInfor = new HInformaiton { MainboardName ="未知", MemorySize = "未知", HarddiskSize = "未知", VideocardName = "未知", SoundcardName = "未知", NetworkcardName = "未知",ManufacturerName= "未知", ManufactureDate= "未知" }; //主板信息 try { ManagementObjectSearcher myMainboard = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard"); foreach (ManagementObject board in myMainboard.Get()) { HInfor.MainboardName = board["Product"].ToString(); //break; } } catch (Exception ex) { HInfor.MainboardName = "未知"; } //内存信息 try { ManagementObjectSearcher myMemory = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory"); //定义内存总大小变量 double MemorySize = 0; foreach (ManagementObject obj in myMemory.Get()) { //将内存大小换算成G单位 MemorySize += Convert.ToDouble(obj["Capacity"]) / 1024 / 1024 / 1024; } HInfor.MemorySize = MemorySize.ToString()+"G"; } catch (ManagementException e) { HInfor.MemorySize = "未知"; } //硬盘信息 try { //获取本机所有硬盘信息 ManagementObjectSearcher myHarddisk = new ManagementObjectSearcher("select * from Win32_DiskDrive"); foreach (ManagementObject drive in myHarddisk.Get()) { //获取硬盘容量 var capacity = (Convert.ToDouble(drive["Size"]) / 1024 / 1024 / 1024).ToString("0.00") + "G"; //获取硬盘类型 var mediaType = drive["MediaType"]; HInfor.HarddiskSize = capacity.ToString() + "|" + mediaType.ToString(); } } catch (Exception ex) { HInfor.HarddiskSize = "未知"; } //显卡信息 try { ManagementObjectSearcher myVideoObject = new ManagementObjectSearcher("select * from Win32_VideoController"); foreach (ManagementObject obj in myVideoObject.Get()) { HInfor.VideocardName = obj["Name"].ToString(); } } catch (ManagementException e) { //e.Message HInfor.VideocardName = "未知"; } //声卡信息 try { // 创建WMI搜索对象 ManagementObjectSearcher mySoundcard = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SoundDevice"); // 遍历搜索结果 foreach (ManagementObject mo in mySoundcard.Get()) { // 获取声卡名称 HInfor.SoundcardName = mo.GetPropertyValue("Name").ToString(); } } catch (ManagementException e) { HInfor.SoundcardName = "未知"; } //网卡信息(Mac地址) try { // 获取本地网络接口信息 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { // 如果是RJ45网卡 if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { string S1 = (string)adapter.Description; if (S1.Contains("PCI")){ HInfor.NetworkcardName = S1; } } } } catch (ManagementException e) { HInfor.NetworkcardName = "未知"; } //获取生产商和生产日期 try { //获取生产商 ManagementObjectSearcher ManufacturerInfo = new ManagementObjectSearcher("select * from Win32_ComputerSystem"); foreach (ManagementObject obj in ManufacturerInfo.Get()) { HInfor.ManufacturerName = obj["Manufacturer"].ToString(); } //获取生产日期 ConnectionOptions options = new ConnectionOptions(); ManagementScope scope = new ManagementScope("\\\\.\\root\\cimv2", options); ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_BIOS"); ManagementObjectSearcher BoisInfo = new ManagementObjectSearcher(scope, query); foreach (ManagementObject mo in BoisInfo.Get()) { string StrManufactureDate = mo["ReleaseDate"].ToString().Substring(0, 8); DateTime DT = DateTime.ParseExact(StrManufactureDate, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); HInfor.ManufactureDate = String.Format("{0:d}", DT); } } catch (Exception e) { } ReturnStrJson = JsonSerializer.Serialize(HInfor); return ReturnStrJson; } }
需要添加引用:
using Microsoft.Win32; using System.Management; using System.Text.Json; using System.Net.NetworkInformation;
调用比较简单:
string JsonStr; //硬件信息 JsonStr = HardwareInformation.GetJsonStr(); var ObjHardware = JsonSerializer.Deserialize<Dictionary<string, object>>(JsonStr); ListBox listbox1 = tabControl1.TabPages[0].Controls["listbox1"] as ListBox; listbox1.Items.Add("主板:"+ ObjHardware["MainboardName"]); listbox1.Items.Add("内存:" + ObjHardware["MemorySize"]); listbox1.Items.Add("硬盘:" + ObjHardware["HarddiskSize"]); listbox1.Items.Add("显卡:" + ObjHardware["VideocardName"]); listbox1.Items.Add("声卡:" + ObjHardware["SoundcardName"]); listbox1.Items.Add("网卡:" + ObjHardware["NetworkcardName"]); listbox1.Items.Add("计算机生产商:" + ObjHardware["ManufacturerName"]); listbox1.Items.Add("计算机生产日期:" + ObjHardware["ManufactureDate"]);
到此这篇关于C#实现获取电脑硬件显卡核心代号信息的文章就介绍到这了,更多相关C#获取电脑显卡信息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!