zoukankan      html  css  js  c++  java
  • C# 利用WMI对象获取物理内存和可用内存大小

    下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下:

    复制代码
    //获取总物理内存大小
     ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
      ManagementObjectCollection moc1 = cimobject1.GetInstances();
      foreach (ManagementObject mo1 in moc1) {
             capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));                  
      }
      moc1.Dispose();
      cimobject1.Dispose();
    
    
      //获取内存可用大小
     ManagementClass cimobject2 = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory");
      ManagementObjectCollection moc2 = cimobject2.GetInstances();
      foreach (ManagementObject mo2 in moc2) {
            available += ((Math.Round(Int64.Parse(mo2.Properties["AvailableMBytes"].Value.ToString()) / 1024.0, 1)));
    
      }
      moc2.Dispose();
      cimobject2.Dispose();
    
    
      Console.WriteLine("MemoryCapacity=" + capacity.ToString() + "G");
      Console.WriteLine("MemoryAvailable=" + available.ToString() + "G");
      Console.WriteLine("MemoryUsed=" + ((capacity - available)).ToString() + "G," + (Math.Round((capacity - available) / capacity * 100,0)).ToString() + "%");
    复制代码
  • 相关阅读:
    Cardiogram
    Increasing Speed Limits HDU
    Beaver Game CodeForces
    C++LeetCode:: Container With Most Water
    C++ leetcode::Reverse Integer
    C++ leetcode::ZigZag Conversion
    C++ leetcode Longest Palindromic Substring
    C++ leetcode Longest Substring Without Repeating Characters
    Faster RCNN
    C++ Leetcode Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/MuNet/p/6638850.html
Copyright © 2011-2022 走看看