c#獲取機器硬體資訊
- using System;
- using System.Management;
- namespace Soyee.Comm
- {
- /// <summary>
- /// Computer Information
- /// </summary>
- public class Computer
- {
- public string CpuID;
- public string MacAddress;
- public string DiskID;
- public string IpAddress;
- public string LoginUserName;
- public string ComputerName;
- public string SystemType;
- public string TotalPhysicalMemory; //單位:M
- private static Computer _instance;
- public static Computer Instance()
- {
- if (_instance == null)
- _instance = new Computer();
- return _instance;
- }
- protected Computer()
- {
- CpuID=GetCpuID();
- MacAddress=GetMacAddress();
- DiskID=GetDiskID();
- IpAddress=GetIPAddress();
- LoginUserName=GetUserName();
- SystemType=GetSystemType();
- TotalPhysicalMemory=GetTotalPhysicalMemory();
- ComputerName=GetComputerName();
- }
- 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";
- }
- finally
- {
- }
- }
- 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";
- }
- finally
- {
- }
- }
- 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";
- }
- finally
- {
- }
- }
- 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";
- }
- finally
- {
- }
- }
- /// <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";
- }
- finally
- {
- }
- }
- /// <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";
- }
- finally
- {
- }
- }
- /// <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";
- }
- finally
- {
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- string GetComputerName()
- {
- try
- {
- return System.Environment.GetEnvironmentVariable("ComputerName");
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
- }
- }
相關文章
- 用C#獲取電腦硬體資訊C#
- VbScript獲取本地機器的硬體配置
- js獲取瀏覽器資訊和客戶端硬體資訊(一)JS瀏覽器客戶端
- Android獲取硬體裝置資訊Android
- C++獲取硬體相關資訊C++
- vmi:獲取 windows 系統硬體資訊Windows
- Request.ServerVariables,C#獲取伺服器資訊,C#獲取訪問資訊ServerC#伺服器
- 【VMware ESXi】使用 smbiosDump 命令獲取伺服器硬體資訊。iOS伺服器
- python獲得本機硬體資訊Python
- c# 獲取電腦硬體資訊通用查詢類[測試通過]C#
- golang gopsutil 程式 系統硬體資訊 獲取Golang
- C# url資訊獲取C#
- Inxi:獲取Linux系統和硬體資訊的神器Linux
- Linux檢視CPU資訊、機器型號等硬體資訊Linux
- hyperic-sigar-1.6.4 java獲取軟硬體相關資訊Java
- ipmitool獲取伺服器資訊MIT伺服器
- javascript 獲取瀏覽器資訊JavaScript瀏覽器
- 智慧硬體的商機——資訊圖
- shell指令碼整合json數值輸出從而獲取硬體資訊指令碼JSON
- Android硬體點陣圖填坑之獲取硬體畫布Android
- 獲取windows 作業系統下的硬體或作業系統資訊等Windows作業系統
- 【Python】獲取機器使用資訊Python
- Android獲取手機配置資訊Android
- linux下面獲取主機資訊Linux
- js獲取ip,瀏覽器資訊JS瀏覽器
- php-MediaInfo 獲取媒體資訊PHPAI
- C#獲取IP和主機名C#
- C#使用ManagementObjectSearcher獲取本計算機CPU,硬碟,記憶體條等相關裝置資訊C#Object計算機硬碟記憶體
- C#獲取CPU佔用率、記憶體佔用、磁碟佔用、程式資訊C#記憶體
- C# 通過ConfigurationManager獲取配置資訊C#
- iOS 之獲取APP與手機 資訊iOSAPP
- Java解析微信獲取手機號資訊Java
- ASP.Net 獲取伺服器資訊ASP.NET伺服器
- asp.net獲取伺服器資訊ASP.NET伺服器
- PHP獲取客戶端、PHP獲取伺服器相關資訊PHP客戶端伺服器
- [Android]獲取整體APP CPU資訊AndroidAPP
- C# 獲取當前伺服器域名C#伺服器
- 使用 C# 獲取 Kubernetes 叢集資源資訊C#