博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 获取机器硬件信息 (硬盘,cpu,内存等)
阅读量:6457 次
发布时间:2019-06-23

本文共 6227 字,大约阅读时间需要 20 分钟。

using System;using System.Collections.Generic;using System.Globalization;using System.Management;using System.IO;using System.Net;using System.Net.NetworkInformation;using System.Net.Sockets;using System.Runtime.InteropServices;using System.Text;namespace HuaTong.General.Utility{    internal enum WmiType    {        Win32_Processor,        Win32_PerfFormattedData_PerfOS_Memory,        Win32_PhysicalMemory,        Win32_NetworkAdapterConfiguration,        Win32_LogicalDisk    }    ///     /// 获取硬盘号和CPU号    ///     public class MachineNumber    {        static Dictionary
WmiDict = new Dictionary
(); static MachineNumber() { var names = Enum.GetNames(typeof(WmiType)); foreach (string name in names) { WmiDict.Add(name, new ManagementObjectSearcher("SELECT * FROM " + name).Get()); } } ///
/// 获取硬盘号码 /// ///
public static string GetHardDiskNumber() { var query = WmiDict[WmiType.Win32_LogicalDisk.ToString()]; //var collection = query.Get(); string result = string.Empty; foreach (var obj in query) { result = obj["VolumeSerialNumber"].ToString(); break; } return result; } ///
/// 获取CPU号码 /// ///
public static string GetCPUNumber() { var query = WmiDict[WmiType.Win32_Processor.ToString()]; //var collection = query.Get(); string result = string.Empty; foreach (var obj in query) { result = obj["Processorid"].ToString(); break; } return result; } ///
/// 获取内存编号 /// ///
public static string GetMemoryNumber() { var query = WmiDict[WmiType.Win32_PhysicalMemory.ToString()]; //var collection = query.Get(); string result = string.Empty; foreach (var obj in query) { result = obj["PartNumber"].ToString(); break; } return result; } ///
/// 获取硬盘信息 /// ///
public static string HardDiskInfo() { DriveInfo[] drives = DriveInfo.GetDrives(); StringBuilder sr = new StringBuilder(); foreach (DriveInfo drive in drives) { if (drive.IsReady) { var val1 = (double)drive.TotalSize / 1024 / 1024; var val2 = (double)drive.TotalFreeSpace / 1024 / 1024; sr.AppendFormat("{0}:{2}/{3}MB/{4}MB/{1}%可用;", drive.Name, string.Format("{0:F2}", val2 / val1 * 100), drive.DriveFormat, (long)val1, (long)val2); } } return sr.ToString(); } ///
/// 获取操作系统信息 /// ///
public static string OSInfo() { StringBuilder sr = new StringBuilder(); sr.AppendFormat("机器名:{0};操作系统:{1};系统文件夹:{2};语言:{3};.NET:{4};当前目录:{5};当前用户:{6};", Environment.MachineName, Environment.OSVersion, Environment.SystemDirectory, CultureInfo.InstalledUICulture.EnglishName, Environment.Version, Environment.CurrentDirectory, Environment.UserName); return sr.ToString(); } ///
/// 获取网卡信息 /// ///
//public static string NetworkInfo() //{ // StringBuilder sr = new StringBuilder(); // string host = Dns.GetHostName(); // IPHostEntry ipEntry = Dns.GetHostByName(host); // sr.Append("IPv4:" + ipEntry.AddressList[0] + "/"); // sr.Append("IPv6:"); // ipEntry = Dns.GetHostEntry(host); // sr.Append("IPv6:" + ipEntry.AddressList[0] + ";"); // sr.Append("MAC:"); // var query = WmiDict[WmiType.Win32_NetworkAdapterConfiguration.ToString()]; // foreach (var obj in query) // { // if (obj["IPEnabled"].ToString() == "True") // sr.Append(obj["MacAddress"] + ";"); // } // return sr.ToString(); //} ///
/// 获取内存信息 /// ///
public static string MemoryInfo() { StringBuilder sr = new StringBuilder(); long capacity = 0; var query = WmiDict[WmiType.Win32_PhysicalMemory.ToString()]; int index = 1; foreach (var obj in query) { sr.Append("内存" + index + "频率:" + obj["ConfiguredClockSpeed"] + ";"); capacity += Convert.ToInt64(obj["Capacity"]); index++; } sr.Append("总物理内存:"); sr.Append(capacity / 1024 / 1024 + "MB;"); query = WmiDict[WmiType.Win32_PerfFormattedData_PerfOS_Memory.ToString()]; sr.Append("总可用内存:"); long available = 0; foreach (var obj in query) { available += Convert.ToInt64(obj.Properties["AvailableMBytes"].Value); } sr.Append(available + "MB;"); sr.AppendFormat("{0:F2}%可用; ", (double)available / (capacity / 1024 / 1024) * 100); return sr.ToString(); } ///
/// 获取CPU信息 /// ///
public static string CpuInfo() { StringBuilder sr = new StringBuilder(); var query = WmiDict[WmiType.Win32_Processor.ToString()]; foreach (var obj in query) { sr.Append("厂商:" + obj["Manufacturer"] + ";"); sr.Append("产品名称:" + obj["Name"] + ";"); sr.Append("最大频率:" + obj["MaxClockSpeed"] + ";"); sr.Append("当前频率:" + obj["CurrentClockSpeed"] + ";"); } return sr.ToString(); } }}

 

转载地址:http://cenzo.baihongyu.com/

你可能感兴趣的文章
js获取当前时间的前一天/后一天
查看>>
[洛谷P3978][TJOI2015]概率论
查看>>
Python学习——编程语言介绍
查看>>
Python字符串的格式化
查看>>
C#反射---属性
查看>>
服务器常用的状态码及其对应的含义如下
查看>>
完美字符串
查看>>
我为什么要写LeetCode的博客?
查看>>
zoom和transform:scale的区别
查看>>
Plplot中line width 问题
查看>>
js中(function(){…})()立即执行函数写法理解
查看>>
QQ空间Python爬虫(2)---分析json
查看>>
NEU OJ 1651 Great number
查看>>
VSCode + PYQT5 + QtDesigner 环境搭建和测试
查看>>
The process could not execute 'sp_MSadd_replcmds'错误解决方案
查看>>
runas /user:administrator cmd 以管理员身份运行CMD
查看>>
配置tomcat允许跨域访问,cors跨域资源共享
查看>>
基本数据结构:链表(list)
查看>>
[LUOGU] P3275 [SCOI2011]糖果
查看>>
[LUOGU] P3469 [POI2008]BLO-Blockade
查看>>