C#讀取CPU數量,內存容量
可以通過Windows Management Instrumentation (WMI)提供的接口讀取所需要的信息。
private static UInt32 CountPhysicalProcessors() { ManagementObjectSearcher objects = new ManagementObjectSearcher( “SELECT * FROM Win32_ComputerSystem”); ManagementObjectCollection coll = objects.Get(); foreach(ManagementObject obj in coll) { return (UInt32)obj[“NumberOfProcessors”]; } return 0; } private static UInt64 CountPhysicalMemory() { ManagementObjectSearcher objects =new ManagementObjectSearcher( “SELECT * FROM Win32_PhysicalMemory”); ManagementObjectCollection coll = objects.Get(); UInt64 total = 0; foreach (ManagementObject obj in coll) { total += (UInt64)obj[“Capacity”]; } return total; }
請添加對程序集System.Management的引用,確保代碼可以正確編譯。
Console.WriteLine(“Machine: {0}”, Environment.MachineName); Console.WriteLine(“# of processors (logical): {0}”, Environment.ProcessorCount); Console.WriteLine(“# of processors (physical): {0}” CountPhysicalProcessors()); Console.WriteLine(“RAM installed: {0:N0} bytes”, CountPhysicalMemory()); Console.WriteLine(“Is OS 64-bit? {0}”, Environment.Is64BitOperatingSystem); Console.WriteLine(“Is process 64-bit? {0}”, Environment.Is64BitProcess); Console.WriteLine(“Little-endian: {0}”, BitConverter.IsLittleEndian); foreach (Screen screen in System.Windows.Forms.Screen.AllScreens) { Console.WriteLine(“Screen {0}”, screen.DeviceName); Console.WriteLine(“\tPrimary {0}”, screen.Primary); Console.WriteLine(“\tBounds: {0}”, screen.Bounds); Console.WriteLine(“\tWorking Area: {0}”,screen.WorkingArea); Console.WriteLine(“\tBitsPerPixel: {0}”,screen.BitsPerPixel); }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!