获取计算机硬件信息

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Management;

/// <summary>
///Computer 的摘要说明
/// </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;
    }
    public Computer()
    {
        CpuID = GetCpuID();
        MacAddress = GetMacAddress();
        DiskID = GetDiskID();
        IpAddress = GetIPAddress();
        LoginUserName = GetUserName();
        SystemType = GetSystemType();
        TotalPhysicalMemory = GetTotalPhysicalMemory();
        ComputerName = GetComputerName();
    }
    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";
        }
        finally
        {
        }

    }
    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";
        }
        finally
        {
        }

    }
    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";
        }
        finally
        {
        }

    }

    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";
        }
        finally
        {
        }

    }

    /// <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";
        }
        finally
        {
        }

    }


    /// <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";
        }
        finally
        {
        }

    }

    /// <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";
        }
        finally
        {
        }
    }
    /// <summary> 
    ///  
    /// </summary> 
    /// <returns></returns> 
    public string GetComputerName()
    {
        try
        {
            return System.Environment.GetEnvironmentVariable("ComputerName");
        }
        catch
        {
            return "unknow";
        }
        finally
        {
        }
    }



}

转载于:https://www.cnblogs.com/XiaoZone/p/11108341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,可以使用多种方法获取电脑的硬件信息。下面是一些常用的方法: 1. 使用`platform`模块获取系统相关信息: ```python import platform # 获取操作系统名称和版本号 os_name = platform.system() os_version = platform.release() # 获取计算机名称 computer_name = platform.node() # 获取处理器类型和名称 processor_type = platform.processor() # 打印以上信息 print("操作系统:", os_name) print("版本号:", os_version) print("计算机名称:", computer_name) print("处理器:", processor_type) ``` 2. 使用`psutil`模块获取更多详细信息: ```python import psutil # 获取CPU信息 cpu_info = psutil.cpu_info() print("CPU信息:", cpu_info) # 获取内存信息 memory_info = psutil.virtual_memory() print("内存信息:", memory_info) # 获取磁盘信息 disk_info = psutil.disk_partitions() print("磁盘信息:", disk_info) ``` 3. 使用`wmi`模块获取更多硬件信息(需要安装`pywin32`库): ```python import wmi # 创建wmi对象 c = wmi.WMI() # 获取CPU信息 cpu_info = c.Win32_Processor()[0] print("CPU信息:", cpu_info) # 获取主板信息 board_info = c.Win32_BaseBoard()[0] print("主板信息:", board_info) # 获取物理内存信息 memory_infos = c.Win32_PhysicalMemory() print("内存信息:") for memory_info in memory_infos: print(memory_info) # 获取磁盘信息 disk_infos = c.Win32_DiskDrive() print("磁盘信息:") for disk_info in disk_infos: print(disk_info) ``` 以上是获取电脑硬件信息的基本方法,根据需求可以进一步筛选和加工这些信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值