zoukankan      html  css  js  c++  java
  • 获取CPU序列号、网卡MAC地址、硬盘序列号

    <pre name="code" class="csharp"> using System;  
     using System.Collections;
     using System.Management;
    
    public class OneProgram
    {
        public static void Main()
        {
            Console.WriteLine("HostName:{0}", HardwareInfo.GethostName());
            Console.WriteLine("CPU:{0}", HardwareInfo.GetCPUSerialNumber());
            Console.WriteLine("Harddisk:{0}", HardwareInfo.GetDiskSerialNumber());
            Console.WriteLine("MAC:{0}", HardwareInfo.GetMacAddress());
            Console.ReadLine();
        }
    
    }
    
    class HardwareInfo
    {
        /// <summary>
        /// 取机器名
        /// </summary>
        /// <returns></returns>
        public static string GethostName()
        {
            return System.Net.Dns.GetHostName();
        }
    
        /// <summary>
        /// 获取CPU序列号
        /// </summary>
        /// <returns></returns>
        public static string GetCPUSerialNumber()
        {
            string cpuSerialNumber = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuSerialNumber = mo["ProcessorId"].ToString();
                break;
            }
            mc.Dispose();
            moc.Dispose();
            return cpuSerialNumber;
        }
    
        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns></returns>
        public static string GetDiskSerialNumber()
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher();
            mos.Query = new SelectQuery("Win32_DiskDrive", "", new string[] { "PNPDeviceID", "Signature" });
            ManagementObjectCollection myCollection = mos.Get();
            ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator();
            em.MoveNext();
            ManagementBaseObject moo = em.Current;
            string id = moo.Properties["signature"].Value.ToString().Trim();
            return id;
        }
    
    
        /// <summary>
        /// 获取网卡MAC地址 
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            string _MacAddress = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {
                if ((bool)mo["IPEnabled"] == true)
                    _MacAddress = mo["MacAddress"].ToString();
                mo.Dispose();
            }
            return _MacAddress.ToString();
        }
    }
    


    
                
    
  • 相关阅读:
    Callable Future 和 FutureTask
    多线程常用工具类
    Servlet的forward与include方法
    Spring MVC 执行流程分析
    使用SpringEL表达式进行三目运算
    推荐10款Java程序员使用的单元测试工具
    使用SpringEL表达式进行方法调用
    使用SpringEL操作List和Map集合
    SpringEL表达式(一)-入门案例
    Servlet的生命周期
  • 原文地址:https://www.cnblogs.com/zhaoshujie/p/9594760.html
Copyright © 2011-2022 走看看