时间:2021-05-20
本文实例为大家分享了C#获取计算机信息的具体代码,供大家参考,具体内容如下
using System;using System.Configuration;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Management; namespace test{ /// <summary> /// Computer informercial /// </summary> 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; } public Computer() { CpuID = GetCpuID(); MacAddress = GetMacAddress(); DiskID = GetDiskID(); IpAddress = GetIPAddress(); LoginUserName = GetUserName(); SystemType = GetSystemType(); TotalPhysicalMemory = GetTotalPhysicalMemory(); ComputerName = GetComputerName(); } /// <summary> /// 获取CPU ID /// </summary> /// <returns></returns> private 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 { } } /// <summary> /// 获取Mac 地址 /// </summary> /// <returns></returns> private 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 { } } /// <summary> /// 获取 ip地址 /// </summary> /// <returns></returns> private 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 { } } /// <summary> /// 获取硬盘ID /// </summary> /// <returns></returns> private 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> private string GetComputerName() { try { return System.Environment.GetEnvironmentVariable("ComputerName"); } catch { return "unknow"; } finally { } } /// <summary> /// 物理内存 /// </summary> /// <returns></returns> private 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> /// PC类型 /// </summary> /// <returns></returns> private 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> private 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 { } } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
利用C#获取计算机名,IP,MAC信息,如下为源代码:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;
本文实例讲述了C#读取计算机CPU及HDD信息的方法。分享给大家供大家参考。具体如下:这里使用C#读取计算机CPU,HDD信息,适用于Windowspublic
本文实例讲述了C#实现远程关闭计算机或重启计算机的方法。分享给大家供大家参考。具体如下://////远程关闭计算机或重启计算机//////命令///ip地址st
本文实例讲述了C#检测远程计算机端口是否打开的方法。分享给大家供大家参考。具体分析如下:这段C#代码用于检测远程计算机的3389端口是否处理打开状态,可以根据实
当下,C#与Python都是比较热门的计算机编程语言,他们各有优缺点,如果能让他们互相配合工作,那是多么美好的事情,今天我来讲解一下如何利用C#来调用Pyt