はじめての WMI

WMI 初めて使いました。MSDN の宇宙語っぷりにクラクラしたけど、宇宙仮面さんが解読済みだったので助かりました。ありがとうございます。

Win32_CacheMemory の手抜きサンプル書いてみました。
値の意味の文字列はどっかから取れないのかなぁ。コピペして Ctrl+H で VS の正規表現の練習大会になってしまいました。疲れた。

Dictionaryの初期化 ( via id:Nobuhisa:20071117:1195238968 ) も参考になりました。ありがとうございます。
「var dic = new Dictionary(){ { "one", 1 }, { "two", 2 }, { "three", 3 } };」

System.Management への参照の追加が必要です。

using System;
using System.Collections.Generic;
using System.Management;

namespace Wmi
{
  class Program
  {
    static void Main()
    {
      try
      {
        using ( ManagementClass managementClass = new ManagementClass( "Win32_CacheMemory" ) )
        using ( ManagementObjectCollection objs = managementClass.GetInstances() )
        {
          foreach ( ManagementObject mo in objs )
          {
            foreach ( var p in mo.Properties )
            {
              if ( p.Value == null )
              {
                Console.WriteLine( p.Name + " : null" );
              }
              else if ( p.IsArray )
              {
                Console.Write( p.Name + " : " );
                foreach ( var n in ( Array ) p.Value )
                {
                  //Console.Write( n + ", " );
                  Console.Write( ToMessage( p.Name, n ) + ", " );
                }
                Console.WriteLine();
              }
              else
              {
                //Console.WriteLine( p.Name + " : " + p.Value );
                Console.WriteLine( p.Name + " : " + ToMessage( p.Name, p.Value ) );
              }
            }
            Console.WriteLine();
            mo.Dispose();
          }
        }
      }
      catch ( Exception e )
      {
        Console.WriteLine( e.Message );
      }
      Console.ReadKey();
    }

    private static string ToMessage( string name, object value )
    {
      Dictionary<int, string> dic;

      if ( WmiWin32CacheMemoryMessages.TryGetValue( name, out dic ) )
      {
        string str;
        int val = ToInt( value );
        if ( dic.TryGetValue( val, out str ) )
          return str;
      }
      return value.ToString();
    }

    private static int ToInt( object o )
    {
      if ( o is UInt16 )
      {
        UInt16 u = ( UInt16 ) o;
        return ( int ) u;
      }
      else if ( o is UInt32 )
      {
        UInt32 u = ( UInt32 ) o;
        return ( int ) u;
      }
      throw new ArgumentException( "Unknown Type", "o" );
    }

    private static Dictionary<string, Dictionary<int, string>> WmiWin32CacheMemoryMessages =
      new Dictionary<string, Dictionary<int, string>> {
    { "Access", new Dictionary<int, string> {
       { 0, "Unknown" }, { 1, "Readable" }, { 2, "Writable" }, { 3, "Read/Write Supported" },
       { 4, "Windows Server 2003 and Windows XP:  Write Once" } } },
    { "Associativity", new Dictionary<int, string> {
       { 1, "Other" }, { 2, "Unknown" }, { 3, "Direct Mapped" }, { 4, "2-way Set-Associative" },
       { 5, "4-way Set-Associative" }, { 6, "Fully Associative" },
       { 7, "Windows Server 2003 and Windows XP:  8-way Set-Associative" },
       { 8, "Windows Server 2003 and Windows XP:  16-way Set-Associative" } } },
    { "Availability", new Dictionary<int, string> {
      { 1, "Other" }, { 2, "Unknown" }, { 3, "Running/Full Power" }, { 4, "Warning" },
      { 5, "In Test" }, { 6, "Not Applicable" }, { 7, "Power Off" }, { 8, "Off Line" },
      { 9, "Off Duty" }, { 10, "Degraded" }, { 11, "Not Installed" }, { 12, "Install Error" },
      { 13, "Power Save - Unknown  The device is known to be in a power save mode, but its exact status is unknown." },
      { 14, "Power Save - Low Power Mode  The device is in a power save state but still functioning, and may exhibit degraded performance." },
      { 15, "Power Save - Standby  The device is not functioning but could be brought to full power quickly." },
      { 16, "Power Cycle" },
      { 17, "Power Save - Warning  The device is in a warning state, though also in a power save mode." } } },
    { "CacheType", new Dictionary<int, string> {
      { 1, "Other" }, { 2, "Unknown" }, { 3, "Instruction" }, { 4, "Data" }, { 5, "Unified" } } },
    { "ConfigManagerErrorCode", new Dictionary<int, string> {
      { 0, "Device is working properly." },
      { 1, "Device is not configured correctly." },
      { 2, "Windows cannot load the driver for this device." },
      { 3, "Driver for this device might be corrupted, or the system may be low on memory or other resources." },
      { 4, "Device is not working properly. One of its drivers or the registry might be corrupted." },
      { 5, "Driver for the device requires a resource that Windows cannot manage." },
      { 6, "Boot configuration for the device conflicts with other devices." },
      { 7, "Cannot filter." },
      { 8, "Driver loader for the device is missing." },
      { 9, "Device is not working properly; the controlling firmware is incorrectly reporting the resources for the device." },
      { 10, "Device cannot start." },
      { 11, "Device failed." },
      { 12, "Device cannot find enough free resources to use." },
      { 13, "Windows cannot verify the device's resources." },
      { 14, "Device cannot work properly until the computer is restarted." },
      { 15, "Device is not working properly due to a possible re-enumeration problem." },
      { 16, "Windows cannot identify all of the resources that the device uses." },
      { 17, "Device is requesting an unknown resource type." },
      { 18, "Device drivers need to be reinstalled." },
      { 19, "Failure using the VxD loader." },
      { 20, "Registry might be corrupted." },
      { 21, "System failure. If changing the device driver is ineffective, see the hardware documentation. Windows is removing the device." },
      { 22, "Device is disabled." },
      { 23, "System failure. If changing the device driver is ineffective, see the hardware documentation." },
      { 24, "Device is not present, not working properly, or does not have all of its drivers installed." },
      { 25, "Windows is still setting up the device." },
      { 26, "Windows is still setting up the device." },
      { 27, "Device does not have valid log configuration." },
      { 28, "Device drivers are not installed." },
      { 29, "Device is disabled; the device firmware did not provide the required resources." },
      { 30, "Device is using an IRQ resource that another device is using." },
      { 31, "Device is not working properly; Windows cannot load the required device drivers." } } },
    { "CurrentSRAM", new Dictionary<int, string> { { 0, "Other" }, { 1, "Unknown" }, { 2, "Non-Burst" },
      { 3, "Burst" }, { 4, "Pipeline Burst" }, { 5, "Synchronous" }, { 6, "Asynchronous" } } },
    { "ErrorAccess", new Dictionary<int, string> { { 1, "Other" }, { 2, "Unknown" }, { 3, "Read" },
      { 4, "Write" }, { 5, "Partial Write" } } },
    { "ErrorCorrectType", new Dictionary<int, string> { { 0, "Reserved" }, { 1, "Other" },
      { 2, "Unknown" }, { 3, "None" }, { 4, "Parity" }, { 5, "Single-bit ECC" }, { 6, "Multi-bit ECC" } } },
    { "ErrorDataOrder", new Dictionary<int, string> { { 0, "Unknown" }, { 1, "Least Significant Byte First" },
      { 2, "Most Significant Byte First" } } },
    { "ErrorInfo", new Dictionary<int,string> { { 1, "Other" }, { 2, "Unknown" }, { 3, "OK" }, { 4, "Bad Read" },
      { 5, "Parity Error" }, { 6, "Single-Bit Error" }, { 7, "Double-Bit Error" }, { 8, "Multi-Bit Error" },
      { 9, "Nibble Error" }, { 10, "Checksum Error" }, { 11, "CRC Error" }, { 12, "Undefined" },
      { 13, "Undefined" }, { 14, "Undefined" } } },
    { "Level", new Dictionary<int,string> { { 1, "Other" }, { 2, "Unknown" }, { 3, "Primary" },
      { 4, "Secondary" }, { 5, "Tertiary" }, { 6, "Windows Server 2003 and Windows XP:  Not Applicable" } } },
    { "Location", new Dictionary<int,string> {
      { 0, "Internal" }, { 1, "External" }, { 2, "Reserved" }, { 3, "Unknown" } } },
    { "PowerManagementCapabilities", new Dictionary<int,string> {
      { 0, "Unknown" },
      { 1, "Not Supported" },
      { 2, "Disabled" },
      { 3, "Enabled  The power management features are currently enabled but the exact feature set is unknown or the information is unavailable." },
      { 4, "Power Saving Modes Entered Automatically  The device can change its power state based on usage or other criteria." },
      { 5, "Power State Settable  The SetPowerState method is supported. This method is found on the parent CIM_LogicalDevice class and can be implemented. For more information, see Extending Your Management Capabilities." },
      { 6, "Power Cycling Supported  The SetPowerState method can be invoked with the PowerState parameter set to 5 (Power Cycle)." },
      { 7, "Timed Power On Supported  The SetPowerState method can be invoked with the PowerState parameter set to 5 (Power Cycle) and Time set to a specific date and time, or interval, for power-on." },
    } },
    { "ReadPolicy", new Dictionary<int,string> { { 1, "Other" }, { 2, "Unknown" }, { 3, "Read" },
      { 4, "Read-Ahead" }, { 5, "Read and Read-Ahead" },
      { 6, "Windows Server 2003 and Windows XP:  Determination Per I/O" } } },
    { "ReplacementPolicy", new Dictionary<int,string> { { 1, " Other" }, { 2, " Unknown" },
      { 3, " Least Recently Used (LRU)" }, { 4, " First In First Out (FIFO)" },
      { 5, " Last In First Out (LIFO)" }, { 6, " Least Frequently Used (LFU)" },
      { 7, " Most Frequently Used (MFU)" },
      { 8, "Windows Server 2003 and Windows XP:  Data Dependent Multiple Algorithms" } } },
    { "StatusInfo", new Dictionary<int,string> { { 1, "Other" }, { 2, "Unknown" }, { 3, "Enabled" },
      { 4, "Disabled" }, { 5, "Not Applicable" } } },
    { "SupportedSRAM", new Dictionary<int,string> { { 0, "Other" }, { 1, "Unknown" }, { 2, "Non-Burst" },
      { 3, "Burst" }, { 4, "Pipeline Burst" }, { 5, "Synchronous" }, { 6, "Asynchronous" } } },
    { "WritePolicy", new Dictionary<int,string> { { 1, "Other" }, { 2, "Unknown" }, { 3, "Write Back" },
      { 4, "Write Through" }, { 5, "Varies with Address" },
      { 6, "Windows Server 2003 and Windows XP:  Determination Per I/O " } } }
    };
  }
}