c# 获取电脑硬件信息通用查询类[测试通过]

C#获取电脑硬件信息通用类[Computer]代码展示和分析,简介如下:

1.项目中添加System.Management引用。

2.添加类Computer,把下面代码全选,复制,粘贴。

3.使用方法new Computer().GetComputerName()。

using System;
using System.Management;  // 添加System.Management引用

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;
    }
    /// <summary>
    /// 构造函数
    /// </summary>
    public Computer()
    {
        CpuID = GetCpuID();
        MacAddress = GetMacAddress();
        DiskID = GetDiskID();
        IpAddress = GetIPAddress();
        LoginUserName = GetUserName();
        SystemType = GetSystemType();
        TotalPhysicalMemory = GetTotalPhysicalMemory();
        ComputerName = GetComputerName();
    }
    /// <summary>
    /// 获取cpu序列号
    /// </summary>
    /// <returns></returns>
    public 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";
        }
    }
    /// <summary>
    /// 获取网卡硬件地址  
    /// </summary>
    /// <returns></returns>
    public 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";
        }
    }
    /// <summary>
    /// 获取IP地址
    /// </summary>
    /// <returns></returns>
    public 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";
        }
    }
    /// <summary>
    /// 获取硬盘ID 
    /// </summary>
    /// <returns></returns>
    public 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>   
    public 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>   
    public 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>   
    public 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>   
    public string GetComputerName()
    {
        try
        {
            return System.Environment.GetEnvironmentVariable("ComputerName");
        }
        catch
        {
            return "unknow";
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Java成神之路

Java微信公众平台开发_03_消息管理之被动回复消息

上一节,我们启用服务器配置的时候,填写了一个服务器地址(url),如下图,这个url就是回调url,是开发者用来接收微信消息和事件的接口URL 。也就是说,用户...

1.8K50
来自专栏c#开发者

在DataGrid中创建一个弹出式Details窗口

在DataGrid中创建一个弹出式Details窗口 这篇文章来自DotNetJunkie的提议。他最初写信要求我们提供一个关于如何创建在DataGrid 中...

39880
来自专栏Angular&服务

启动ActiveMQ 又停止

12830
来自专栏程序员的SOD蜜

用事实说话,成熟的ORM性能不是瓶颈,灵活性不是问题:EF5.0、PDF.NET5.0、Dapper原理分析与测试手记

[本文篇幅较长,可以通过目录查看您感兴趣的内容,或者下载格式良好的PDF版本文件查看]  目录 一、ORM的"三国志"    2 1,PDF.NET诞生历程...

48890
来自专栏逸鹏说道

ExecuteReader在执行有输出参数的存储过程时拿不到输出参数

异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 后期会在博客首发更新:http://dnt...

37470
来自专栏GIS讲堂

Arcgis4js实现链家找房的效果

买房的各位亲们不知是否留意过链家的"地图找房",这样的功能对于使用者来说,是非常方便的,大家可通过连接(https://bj.lianjia.com/ditu/...

13620
来自专栏木宛城主

Fix Backup Database is terminating abnormally When performing a Farm Backup

Problem I am trying to backup SharePoint 2013 Farm Automatically with PowerShell...

21570
来自专栏小巫技术博客

Retrofit2 &amp; RxJava2实现单文件和多文件上传

66240
来自专栏吴伟祥

把spring-boot项目部署到外部tomcat环境下

想要把spring-boot项目按照平常的web项目一样发布到tomcat容器下需要进行下列几个步骤:

33810
来自专栏happyJared

Spring Boot2集成Elasticsearch、PostgreSQL遇到的问题

  这个错误确实有点奇怪,不过好在Github上已经有相关Issue,有兴趣的可以去看看,该问题的解决方法是添加配置项:spring.jpa.propertie...

38540

扫码关注云+社区

领取腾讯云代金券