zoukankan      html  css  js  c++  java
  • C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型

    首先引入服务 然后 调用

    本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html

      public class Computer
        {
            public static string CpuID; //1.cpu序列号
            public static string MacAddress; //2.mac序列号
            public static string DiskID; //3.硬盘id
            public static string IpAddress; //4.ip地址
            public static string LoginUserName; //5.登录用户名
            public static string ComputerName; //6.计算机名
            public static string SystemType; //7.系统类型
            public static string TotalPhysicalMemory; //8.内存量 单位:M
    
             static   Computer()
            {
                CpuID = GetCpuID();
                MacAddress = GetMacAddress();
                DiskID = GetDiskID();
                IpAddress = GetIPAddress();
                LoginUserName = GetUserName();
                SystemType = GetSystemType();
                TotalPhysicalMemory = GetTotalPhysicalMemory();
                ComputerName = GetComputerName();
            }
            //1.获取CPU序列号代码 
    
           static  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.获取网卡硬件地址 
    
           static 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 
           static 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地址 
    
           static 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.操作系统的登录用户名 
           static string GetUserName()
            {
                try
                {
                    string un = "";
    
                    un = Environment.UserName;
                    return un;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }
    
            }
    
    
    
            //6.获取计算机名
           static string GetComputerName()
            {
                try
                {
                    return System.Environment.MachineName;
    
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }
            }
    
    
    
            ///7 PC类型 
           static 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.物理内存        
           static 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
                {
                }
    
            }
    
    
        }
  • 相关阅读:
    WEB前端工程师 – 职业生涯规划
    求Sn=a+aa+aaa+…+aaa…a的值
    输入一行字符,分别统计出其中英文字母 空格 数字和其他字符的个数
    getchar()的用法!
    求1+2+…+n的和不大于1000的最大自然数n
    编程打印输出*金字塔
    从键盘输入一个整数,判断该数是否回文数.
    编程求"水仙花数"
    编程求出1000以内的完全数
    输入两个正整数,求它们的最大公约数和最小公倍数.
  • 原文地址:https://www.cnblogs.com/kgdjgd/p/5862073.html
Copyright © 2011-2022 走看看