1

win32_computersystem クラスから合計物理メモリを取得する方法を知っています。しかし、それはバイトまたはキロバイトになります。この情報を MB または GB 単位で取得します。wmi (wql) クエリで。wmicも動作します。前もって感謝します。

4

3 に答える 3

6

Win32_ComputerSystemTotalPhysicalMemoryを変換できます。これを試して :

using System;
using System.Management;
namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");

                foreach (ManagementObject queryObj in searcher.Get())
                { 
                    double dblMemory;
                    if(double.TryParse(Convert.ToString(queryObj["TotalPhysicalMemory"]),out dblMemory))
                    {
                        Console.WriteLine("TotalPhysicalMemory is: {0} MB", Convert.ToInt32(dblMemory/(1024*1024)));
                        Console.WriteLine("TotalPhysicalMemory is: {0} GB", Convert.ToInt32(dblMemory /(1024*1024*1024)));
                    }
                }
            }
            catch (ManagementException e)
            {

            }
        }
    }
}
于 2013-04-17T07:13:28.383 に答える
6

プロパティの値を手動で変換する必要があります。また、Win32_PhysicalMemory WMI クラスを使用することをお勧めします。

このサンプルを試す

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

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                ManagementScope Scope;
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", "."), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
                UInt64 Capacity = 0;
                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Capacity+= (UInt64) WmiObject["Capacity"];
                }
                Console.WriteLine(String.Format("Physical Memory {0} gb", Capacity / (1024 * 1024 * 1024))); 
                Console.WriteLine(String.Format("Physical Memory {0} mb", Capacity / (1024 * 1024)));
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}", e.Message, e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}
于 2013-04-16T18:34:49.497 に答える
2

Windows Server 2012 で一貫性のない結果が得られるまで、Win32_PhysicalMemory Capacity プロパティを使用していたことに言及したいと思います。現在、両方のプロパティ (Win32_ComputerSystem:TotalPhysicalMemory と Win32_PhysicalMemory:Capacity) を使用し、2 つのうち大きい方を選択しています。

于 2015-03-18T12:06:16.713 に答える