• .net获取硬件信息 分类: .NET 2014-02-18 14:13 391人阅读 评论(0) 收藏


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Management;
    using Microsoft.Win32;
    using Microsoft.VisualBasic.Devices;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace PMS.Common
    {
        public static class GetMachineInfo
        {
            /// <summary>
            /// 取机器名
            /// </summary>
            /// <returns></returns>
            public static string GethostName()
            {
                return System.Net.Dns.GetHostName();
            }
    
            /// <summary>
            /// 获取cpu序列号
            /// </summary>
            /// <returns></returns>
            public static string GetCPUSerialNumber()
            {
                string cpuSerialNumber = "";
                try
                {
                    ManagementClass mc = new ManagementClass("Win32_Processor");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        cpuSerialNumber = mo["ProcessorId"].ToString();
                        break;
                    }
                    mc.Dispose();
                    moc.Dispose();
                }
                catch { }
                return cpuSerialNumber.Replace("-", "").Replace(" ", "").Replace(":", "");
            }
    
            /// <summary>
            /// 获取硬盘序列号
            /// </summary>
            /// <returns></returns>
            public static string GetDiskSerialNumber()
            {
                string strID = "";
                try
                {
                    ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        strID = mo.Properties["SerialNumber"].Value.ToString();
                        break;
                    }
                }
                catch { }
                return strID.Replace("-", "").Replace(" ", "").Replace(":", "");
            }
    
            /// <summary>
            /// 获取网卡硬件地址
            /// </summary>
            /// <returns></returns>
            public static string GetMacAddress()
            {
                string MacAddress = "";
                try
                {
                    using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                    {
                        ManagementObjectCollection moc2 = mc.GetInstances();
                        foreach (ManagementObject mo in moc2)
                        {
                            if ((bool)mo["IPEnabled"] == true)
                            {
                                MacAddress = mo["MacAddress"].ToString();
                            }
                        }
                    }
                }
                catch { }
                return MacAddress.Replace("-", "").Replace(" ", "").Replace(":", "");
            }
    
    
            /// <summary>
            /// 内存
            /// </summary>
            /// <returns></returns>
            public static string GetMemory()
            {
                string st = "";
                try
                {
                    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        st = mo["TotalPhysicalMemory"].ToString();
                    }
                    moc = null;
                    mc = null;
                }
                catch { }
                return st.Replace("-", "").Replace(" ", "").Replace(":", "");
            }
    
            /// <summary>
            /// 读取BIOS
            /// </summary>
            /// <returns></returns>
            public static string GetBIOS()
            {
                string strID = "";
                try
                {
                    ManagementClass mc = new ManagementClass("Win32_BIOS");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        strID = mo.Properties["SerialNumber"].Value.ToString();
                        break;
                    }
                }
                catch { }
                return strID.Replace("-", "").Replace(" ", "").Replace(":", "");
            }
    
            /// <summary>
            /// 获取操作系统版本
            /// </summary>
            /// <returns></returns>
            public static string GetOS()
            {
                return new ComputerInfo().OSFullName;
            }
    
            /// <summary>
            /// 获取分辨率
            /// </summary>
            /// <returns></returns>
            public static string GetOsResolution()
            {
                Rectangle rect = Screen.PrimaryScreen.Bounds;
                int width = rect.Width;
                int height = rect.Height;
    
                return width + "*" + height;
            }
    
            /// <summary>
            /// 获取当前显示器的分辨率
            /// </summary>
            /// <returns></returns>
            public static string GetOsMemory()
            {
                double d = Convert.ToDouble(1024);
                double result = new ComputerInfo().TotalPhysicalMemory / d / d / d;
    
                return Math.Round(result) + "G";
            }
        }
    }
    


    获取硬件信息时,请优先使用命名空间Microsoft.VisualBasic.Devices(需要导入)下的ComputerInfo类


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    交换函数swap的三种实现方法
    oracle如何修改某一列的数据类型
    安装 kibana 以及添加中文分词器
    linux 安装Elasticsearch
    docker添加tomcat容器成功无法访问首页
    docker run-it centos提示FATAL
    启动、重新启动容器后,进入交互模式
    获取阿里云docker加速器地址
    CentOS6 修改默认字符集为GBK
    linux中如何查看redis的版本
  • 原文地址:https://www.cnblogs.com/configman/p/4657552.html
Copyright © 2020-2023  润新知