整理了一個關於計算機相關係統的資料數組
須要引入命名空間:框架
1. 在'解決方案資源管理器' 窗口中->右擊項目-> '添加' -> '引用' 彈出引用管理器spa
2. 在引用處理器中,程序集-> 框架 -> 選中 'System.Management' -> 確認操作系統
using System; using System.Management;
using System.IO;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Management; using System.IO; namespace WindowsFormsApp1 { /// <summary> /// 計算機信息類 /// </summary> public class GetComputerInfo { public string CpuID;//Cpu編號 public string MacAddress;//Mac地址 public string DiskID;//磁盤ID public string IpAddress;//IP地址 public string LoginUserName;//系統用戶名 public string ComputerName;//計算機名稱 public string SystemType;//系統類型 public string TotalPhysicalMemory; //單位:M private static GetComputerInfo _instance; public static GetComputerInfo GetInstance() { if (_instance == null) _instance = new GetComputerInfo(); return _instance; } public GetComputerInfo() { CpuID = GetCpuID(); MacAddress = GetMacAddress(); DiskID = GetDiskID(); IpAddress = GetIPAddress(); LoginUserName = GetUserName(); SystemType = GetSystemType(); TotalPhysicalMemory = GetTotalPhysicalMemory(); ComputerName = GetComputerName(); } /// <summary> /// 獲取CPU的個數 /// </summary> /// <returns></returns> public static int GetCpuCount() { try { using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) { ManagementObjectCollection cpus = mCpu.GetInstances(); return cpus.Count; } } catch { } return -1; } /// <summary> /// 獲取CPU的頻率 這裏之因此使用string類型的數組,主要是由於cpu的多核 /// </summary> /// <returns></returns> 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; } /// <summary> /// 獲取本機硬盤的大小 /// </summary> /// <returns></returns> 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"; } /// <summary> /// 獲取本機內存的大小: /// </summary> /// <returns></returns> public static string GetSizeOfMemery() { ManagementClass mc = new ManagementClass("Win32_OperatingSystem"); ManagementObjectCollection moc = mc.GetInstances(); double sizeAll = 0.0; foreach (ManagementObject m in moc) { if (m.Properties["TotalVisibleMemorySize"].Value != null) { sizeAll += Convert.ToDouble(m.Properties["TotalVisibleMemorySize"].Value.ToString()); } } mc = null; moc.Dispose(); return sizeAll.ToString(); } /// <summary> /// 獲取磁盤剩餘空間 /// </summary> /// <param name="str_HardDiskName"></param> /// <returns></returns> long GetHardDiskFreeSpace(string str_HardDiskName) { long num = 0L; str_HardDiskName = str_HardDiskName + @":\"; foreach (DriveInfo info in DriveInfo.GetDrives()) { if (info.Name.ToUpper() == str_HardDiskName.ToUpper()) { num = info.TotalFreeSpace / 0x100000L; } } return num; } //得到CPU編號 string GetCpuID() { try { //獲取CPU序列號代碼 string cpuInfo = "";//cpu序列號 ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); } moc = null; mc = null; return cpuInfo; } catch { return "unknow"; } } //得到Mac地址 string GetMacAddress() { try { //獲取網卡硬件地址 string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { mac = mo["MacAddress"].ToString(); break; } } moc = null; mc = null; return mac; } catch { return "unknow"; } } //得到Ip地址 string GetIPAddress() { try { //獲取IP地址 string st = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { //st=mo["IpAddress"].ToString(); System.Array ar; ar = (System.Array)(mo.Properties["IpAddress"].Value); st = ar.GetValue(0).ToString(); break; } } moc = null; mc = null; return st; } catch { return "unknow"; } } //得到磁盤Id string GetDiskID() { try { //獲取硬盤ID String HDid = ""; ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { HDid = (string)mo.Properties["Model"].Value; } moc = null; mc = null; return HDid; } catch { return "unknow"; } } /// <summary> /// 操做系統的登陸用戶名 /// </summary> /// <returns></returns> string GetUserName() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["UserName"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } /// <summary> /// PC類型 /// </summary> /// <returns></returns> string GetSystemType() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["SystemType"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } /// <summary> /// 物理內存 /// </summary> /// <returns></returns> string GetTotalPhysicalMemory() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["TotalPhysicalMemory"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } /// <summary> /// 獲取計算機名稱 /// </summary> /// <returns></returns> string GetComputerName() { try { return System.Environment.GetEnvironmentVariable("ComputerName"); } catch { return "unknow"; } } } }
ok,今天分享到這了,若是有疑問的能夠留言,講的不對的歡迎指出!!!code