本文实例为大家分享了C#获取计算机信息的具体代码,供大家参考,具体内容如下
usingSystem; usingSystem.Configuration; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Management; namespacetest { ///<summary> ///Computerinformercial ///</summary> classComputer { publicstringCpuID; publicstringMacAddress; publicstringDiskID; publicstringIpAddress; publicstringLoginUserName; publicstringComputerName; publicstringSystemType; publicstringTotalPhysicalMemory;//单位:M privatestaticComputer_instance; publicstaticComputerInstance() { if(_instance==null) _instance=newComputer(); return_instance; } publicComputer() { CpuID=GetCpuID(); MacAddress=GetMacAddress(); DiskID=GetDiskID(); IpAddress=GetIPAddress(); LoginUserName=GetUserName(); SystemType=GetSystemType(); TotalPhysicalMemory=GetTotalPhysicalMemory(); ComputerName=GetComputerName(); } ///<summary> ///获取CPUID ///</summary> ///<returns></returns> privatestringGetCpuID() { try { //获取CPU序列号代码 stringcpuInfo="";//cpu序列号 ManagementClassmc=newManagementClass("Win32_Processor"); ManagementObjectCollectionmoc=mc.GetInstances(); foreach(ManagementObjectmoinmoc) { cpuInfo=mo.Properties["ProcessorId"].Value.ToString(); } moc=null; mc=null; returncpuInfo; } catch { return"unknow"; } finally { } } ///<summary> ///获取Mac地址 ///</summary> ///<returns></returns> privatestringGetMacAddress() { try { //获取网卡硬件地址 stringmac=""; ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollectionmoc=mc.GetInstances(); foreach(ManagementObjectmoinmoc) { if((bool)mo["IPEnabled"]==true) { mac=mo["MacAddress"].ToString(); break; } } moc=null; mc=null; returnmac; } catch { return"unknow"; } finally { } } ///<summary> ///获取ip地址 ///</summary> ///<returns></returns> privatestringGetIPAddress() { try { //获取IP地址 stringst=""; ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollectionmoc=mc.GetInstances(); foreach(ManagementObjectmoinmoc) { if((bool)mo["IPEnabled"]==true) { //st=mo["IpAddress"].ToString(); System.Arrayar; ar=(System.Array)(mo.Properties["IpAddress"].Value); st=ar.GetValue(0).ToString(); break; } } moc=null; mc=null; returnst; } catch { return"unknow"; } finally { } } ///<summary> ///获取硬盘ID ///</summary> ///<returns></returns> privatestringGetDiskID() { try { //获取硬盘ID StringHDid=""; ManagementClassmc=newManagementClass("Win32_DiskDrive"); ManagementObjectCollectionmoc=mc.GetInstances(); foreach(ManagementObjectmoinmoc) { HDid=(string)mo.Properties["Model"].Value; } moc=null; mc=null; returnHDid; } catch { return"unknow"; } finally { } } ///<summary> ///用户名 ///</summary> ///<returns></returns> privatestringGetComputerName() { try { returnSystem.Environment.GetEnvironmentVariable("ComputerName"); } catch { return"unknow"; } finally { } } ///<summary> ///物理内存 ///</summary> ///<returns></returns> privatestringGetTotalPhysicalMemory() { try { stringst=""; ManagementClassmc=newManagementClass("Win32_ComputerSystem"); ManagementObjectCollectionmoc=mc.GetInstances(); foreach(ManagementObjectmoinmoc) { st=mo["TotalPhysicalMemory"].ToString(); } moc=null; mc=null; returnst; } catch { return"unknow"; } finally { } } ///<summary> ///PC类型 ///</summary> ///<returns></returns> privatestringGetSystemType() { try { stringst=""; ManagementClassmc=newManagementClass("Win32_ComputerSystem"); ManagementObjectCollectionmoc=mc.GetInstances(); foreach(ManagementObjectmoinmoc) { st=mo["SystemType"].ToString(); } moc=null; mc=null; returnst; } catch { return"unknow"; } finally { } } ///<summary> ///操作系统的登录用户名 ///</summary> ///<returns></returns> privatestringGetUserName() { try { stringst=""; ManagementClassmc=newManagementClass("Win32_ComputerSystem"); ManagementObjectCollectionmoc=mc.GetInstances(); foreach(ManagementObjectmoinmoc) { st=mo["UserName"].ToString(); } moc=null; mc=null; returnst; } catch { return"unknow"; } finally { } } } }
您可能感兴趣的文章:C#编程获取客户端计算机硬件及系统信息功能示例C#读取计算机CPU及HDD信息的方法C#获取计算机名,IP,MAC信息实现代码
Tomcat设置maxPostSize实现过程解析
从python读取sql的实例方法
上述就是C#学习教程:C#如何获取计算机信息分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/903819.html