博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Management
阅读量:6292 次
发布时间:2019-06-22

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

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Management;namespace IPMACDemo{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("计算机名称:{0}",GetComputerName());            Console.WriteLine("操作系统:{0}", GetSystemType());            Console.WriteLine("用户名:{0}", GetUserName());            Console.WriteLine("计算机MAC地址:{0}", GetMacAddress());            Console.WriteLine("计算机IP地址:{0}", GetIpAddress());            Console.WriteLine("硬盘序列号:{0}", GetDiskSerialNumber());            Console.WriteLine("硬盘大小:{0}", Convert.ToDouble(GetSizeOfDisk()) /(1024*1024*1024));            Console.WriteLine("网卡地址:{0}", GetMacAddress());            Console.WriteLine("显卡PNPDeviceID:{0}", GetVideoPnpid());            Console.WriteLine("声卡PNPDeviceID :{0}", GetSoundPnpid());            Console.WriteLine("主板制造商:{0}", GetBoardManufacturer());            Console.WriteLine("主板编号:{0}", GetBoardId());            Console.WriteLine("主板型号:{0}", GetBoardType());            Console.WriteLine("CPU名称:{0}", GetCpuName());            Console.WriteLine("CPU数量:{0}", GetCpuCount());            Console.WriteLine("CPU编号:{0}", GetCpuid());            Console.WriteLine("CPU版本信息:{0}", GetCpuVersion());            Console.WriteLine("CPU制造商:{0}", GetCpuManufacturer());            Console.WriteLine("物理内存:{0}", Convert.ToDouble(GetPhysicalMemory()) / (1024 * 1024 * 1024));            Console.ReadLine();        }        #region CPU        ///         /// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核        ///         /// 
public static string[] GetCpuMHZ() { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection cpus = mc.GetInstances(); string[] mHz = new string[cpus.Count]; int c = 0; ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor"); foreach (ManagementObject mo in mySearch.Get()) { mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString(); c++; } mc.Dispose(); mySearch.Dispose(); return mHz; } /// /// 获取CPU的个数 /// ///
public static int GetCpuCount() { try { using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) { ManagementObjectCollection cpus = mCpu.GetInstances(); return cpus.Count; } } catch { } return -1; } /// /// 获得CPU编号 /// ///
public static string GetCpuid() { var cpuid = string.Empty; var mc = new ManagementClass("Win32_Processor"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; cpuid = mo.Properties["ProcessorId"].Value.ToString(); } return cpuid; } /// /// CPU版本信息 /// ///
public static string GetCpuVersion() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["Version"].ToString(); } return st; } /// /// CPU名称信息 /// ///
public static string GetCpuName() { var st = string.Empty; var driveId = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in driveId.Get()) { var mo = (ManagementObject)o; st = mo["Name"].ToString(); } return st; } /// /// CPU制造厂商 /// ///
public static string GetCpuManufacturer() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["Manufacturer"].ToString(); } return st; } #endregion #region 主板 /// /// 主板制造厂商 /// ///
public static string GetBoardManufacturer() { var query = new SelectQuery("Select * from Win32_BaseBoard"); var mos = new ManagementObjectSearcher(query); var data = mos.Get().GetEnumerator(); data.MoveNext(); var board = data.Current; return board.GetPropertyValue("Manufacturer").ToString(); } /// /// 主板编号 /// ///
public static string GetBoardId() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["SerialNumber"].ToString(); } return st; } /// /// 主板型号 /// ///
public static string GetBoardType() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["Product"].ToString(); } return st; } #endregion #region 网卡,声卡,显卡 /// /// 获取网卡硬件地址 /// ///
public static string GetMacAddress() { var mac = ""; var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; if (!(bool)mo["IPEnabled"]) continue; mac = mo["MacAddress"].ToString(); break; } return mac; } /// /// 显卡PNPDeviceID /// ///
public static string GetVideoPnpid() { var st = ""; var mos = new ManagementObjectSearcher("Select * from Win32_VideoController"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["PNPDeviceID"].ToString(); } return st; } /// /// 声卡PNPDeviceID /// ///
public static string GetSoundPnpid() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice"); foreach (var o in mos.Get()) { var mo = (ManagementObject)o; st = mo["PNPDeviceID"].ToString(); } return st; } #endregion /// /// 获取硬盘序列号 /// ///
public static string GetDiskSerialNumber() { //这种模式在插入一个U盘后可能会有不同的结果,如插入我的手机时 var hDid = string.Empty; var mc = new ManagementClass("Win32_DiskDrive"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; hDid = (string)mo.Properties["Model"].Value; //这名话解决有多个物理盘时产生的问题,只取第一个物理硬盘 break; } return hDid; } /// /// 获取硬盘的大小 /// ///
public static string GetSizeOfDisk() { ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moj = mc.GetInstances(); foreach (ManagementObject m in moj) { return m.Properties["Size"].Value.ToString(); } return "-1"; } /// /// 物理内存 /// ///
public static string GetPhysicalMemory() { var st = string.Empty; var mc = new ManagementClass("Win32_ComputerSystem"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; st = mo["TotalPhysicalMemory"].ToString(); } return st; } /// /// 获取IP地址 /// ///
public static string GetIpAddress() { var st = string.Empty; var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; if (!(bool)mo["IPEnabled"]) continue; var ar = (Array)(mo.Properties["IpAddress"].Value); st = ar.GetValue(0).ToString(); break; } return st; } /// /// 操作系统的登录用户名 /// ///
public static string GetUserName() { return Environment.UserName; } /// /// 获取计算机名 /// ///
public static string GetComputerName() { return Environment.MachineName; } /// /// 操作系统类型 /// ///
public static string GetSystemType() { var st = string.Empty; var mc = new ManagementClass("Win32_ComputerSystem"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject)o; st = mo["SystemType"].ToString(); } return st; } }}

 

转载于:https://www.cnblogs.com/fuxuyang/p/8565867.html

你可能感兴趣的文章
教程前言 - 回归宣言
查看>>
PHP 7.1是否支持操作符重载?
查看>>
Vue.js 中v-for和v-if一起使用,来判断select中的option为选中项
查看>>
Java中AES加密解密以及签名校验
查看>>
定义内部类 继承 AsyncTask 来实现异步网络请求
查看>>
VC中怎么读取.txt文件
查看>>
如何清理mac系统垃圾
查看>>
企业中最佳虚拟机软件应用程序—Parallels Deskto
查看>>
Nginx配置文件详细说明
查看>>
怎么用Navicat Premium图标编辑器创建表
查看>>
Spring配置文件(2)配置方式
查看>>
MariaDB/Mysql 批量插入 批量更新
查看>>
ItelliJ IDEA开发工具使用—创建一个web项目
查看>>
solr-4.10.4部署到tomcat6
查看>>
切片键(Shard Keys)
查看>>
淘宝API-类目
查看>>
virtualbox 笔记
查看>>
Git 常用命令
查看>>
驰骋工作流引擎三种项目集成开发模式
查看>>
SUSE11修改主机名方法
查看>>