C#获取PC信息(CPU ID、硬盘ID、网卡号等)

C#获取PC信息,可获取CPU ID、硬盘ID、网卡号等。

代码来源于网络,可编译通过,获取CPU ID功能已测试可用,其他功能未作测试。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;

namespace xxxxx
{
	class Computer
	{
		public string CpuID; //1.cpu序列号
		public string MacAddress; //2.mac序列号
		public string DiskID; //3.硬盘id
		public string IpAddress; //4.ip地址
		public string LoginUserName; //5.登录用户名
		public string ComputerName; //6.计算机名
		public string SystemType; //7.系统类型
		public string TotalPhysicalMemory; //8.内存量 单位:
		 public  Computer() 
        { 
            CpuID = GetCpuID(); 
            MacAddress = GetMacAddress(); 
            DiskID = GetDiskID(); 
            IpAddress = GetIPAddress(); 
            LoginUserName = GetUserName(); 
            SystemType = GetSystemType(); 
            TotalPhysicalMemory = GetTotalPhysicalMemory(); 
            ComputerName = GetComputerName(); 
        }

     //1.获取CPU序列号代码 

        string GetCpuID() 
        { 
            try 
            { 
                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 
            { 
            } 

        }

 

   //2.获取网卡硬件地址 

        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 
            { 
            } 

        }

 

    //3.获取硬盘ID 
        string GetDiskID() 
        { 
            try 
            { 
                 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 
            { 
            } 

        }

 

 

      //4.获取IP地址 

        string GetIPAddress() 
        { 
            try 
            { 
                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 
            { 
            } 

        } 


    
        /// 5.操作系统的登录用户名 
         string GetUserName() 
        { 
            try 
            { 
                string un= "";

               un = Environment.UserName;
                             return un; 
            } 
            catch 
            { 
                return "unknow"; 
            } 
            finally 
            { 
            } 

        } 



          //6.获取计算机名
          string GetComputerName() 
        { 
            try 
            { 
                return System.Environment.MachineName;

            } 
            catch 
            { 
                return "unknow"; 
            } 
            finally 
            { 
            } 
        }

   ///7 PC类型 
        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 
            { 
            } 
        } 

    /// 8.物理内存 

        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
			{
			}
		} 
	}
}

下载地址

C#获取PC信息类(CPUID、硬盘ID、网卡号等)-桌面系统文档类资源-CSDN下载C#获取PC信息类,可获取CPUID、硬盘ID、网卡号等。添加进项目后修改namespace即可使更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/becdecorbin/80767555

 转载自TaoHu,原作者程序格式有误,已作修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值