C#读取CPU数量,内存容量

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
可以通过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);
}

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:并发服务器设计

下一篇:常用数据库查询判断表和字段是否存在的SQL