• C#中的ManagementClass类


     

    C# 提供了ManagementClass类来获取本机的一些基本信息,比如CPU的个数,CPU的频率,网卡的MAC,内存的大小,硬盘的大小等。

    获取本机MAC地址:

           /// <summary>
           /// 获取本机的MAC地址
           /// </summary>
           /// <returns></returns>
           public static string GetMACString()
           {
               ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
               ManagementObjectCollection mo = mAdapter.GetInstances();
               foreach (ManagementBaseObject m in mo)
               {
                   if ((bool)m["IpEnabled"] == true)
                   {
                       return m["MacAddress"].ToString();
                   }
               }
               mo.Dispose();
               return null;
           }
    
    获取本机CPU的个数:
    
    /// <summary>
           /// 获取CPU的个数
           /// </summary>
           /// <returns></returns>
           public static int GetCpuCount()
           {
               try
               {
                   using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
                   {
                       ManagementObjectCollection cpus = mCpu.GetInstances();
                       return cpus.Count;
                   }
               }
               catch
               {
               }
               return -1;
           }
    
    获取本机CPU的频率
    
    /// <summary>
           /// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核
           /// </summary>
           /// <returns></returns>
           public static string[] GetCpuMHZ()
           {
               ManagementClass mc = new ManagementClass("Win32_Processor");
               ManagementObjectCollection cpus = mc.GetInstances();
    
               string[] mHz = new string[cpus.Count];
               int c = 0;
               ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
               foreach (ManagementObject mo in mySearch.Get())
               {
                   mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
                   c++;
               }
               mc.Dispose();
               mySearch.Dispose();
               return mHz;
           }
    
    获取本机硬盘的大小
    
    /// <summary>
           /// 获取硬盘的大小
           /// </summary>
           /// <returns></returns>
           public static string GetSizeOfDisk()
           {
               ManagementClass mc = new ManagementClass("Win32_DiskDrive");
               ManagementObjectCollection moj = mc.GetInstances();
               foreach (ManagementObject m in moj)
               {
                   return m.Properties["Size"].Value.ToString();
               }
               return "-1";
           }
        
    
    获取本机内存的大小:
    
    /// <summary>
           /// 获取内存的大小
           /// </summary>
           /// <returns></returns>
           public static string GetSizeOfMemery()
           {
               ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
               ManagementObjectCollection moc = mc.GetInstances();
    
               double sizeAll = 0.0;
               foreach (ManagementObject m in moc)
               {
                   if (m.Properties["TotalVisibleMemorySize"].Value != null)
                   {
                       sizeAll += Convert.ToDouble(m.Properties["TotalVisibleMemorySize"].Value.ToString());
                   }
               }
               mc = null;
               moc.Dispose();
    
               return sizeAll.ToString();
           }

    本程序是使用VS2005编写的,可运行。

    ManageMentClass 对象被实例化时,根据初始化参数的不同能够获取不同的本地信息:"Win32_NetworkAdapterConfiguration" 获取本机网络适配器对象。据此可以获取网络地址等。"Win32_DiskDiver" 获取本机硬盘的相关信息,"Win32_Processor" 获取本机CPU相关的信息。"Win32_OperatingSystem" 获取内存等相关信息。

  • 相关阅读:
    Java开源框架推荐(全)
    Java性能提示(全)
    国外程序员整理的 C++ 资源大全 (zt)
    技术杂记之:在阿里云centos7上部署JDK MYSQL TOMCAT
    技术杂记之:vi使用入门
    Java全栈程序员之09:IDEA+GitHub
    SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
    SpringCloud无废话入门04:Hystrix熔断器及监控
    SpringCloud无废话入门03:Feign声明式服务调用
    SpringCloud无废话入门02:Ribbon负载均衡
  • 原文地址:https://www.cnblogs.com/hardsoftware/p/5743822.html
Copyright © 2020-2023  润新知