private static int int1024 = 1048576; ManagementObjectSearcher Vd = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_VideoController"); ManagementObjectCollection Video = Vd.Get(); foreach (ManagementObject w in Video) { string gpuTotalMem = String.Format("{0} ", (Convert.ToDouble(w["AdapterRam"]) / int1024).ToString()); if (w["Caption"] != null) StringBild.AppendFormat("Ваша Видеокарта: {0} : {1} [Bytes]", w["Caption"], gpuTotalMem); } 

Removing memory from a video card

For the 2nd shows ( 2176 МБ ) - it's normal here!

And for the 1st shows ( 2048 MB ) - And there should be 2 ГБ

As shown by me:

Your Video Card: NVIDIA GeForce GTX 860M - (2048 MB)

Your graphics card: Intel (R) HD Graphics 4600 - (2176 MB)

  • one
    but after all, 2048 MB is the correct result .. round that 1 digit if the memory is 20 ** MB .. or what is the difficulty? - Anton Komyshan
  • I wanted to make it show the exact value as in the picture. - GooliveR
  • one
    So what's the problem then? You just need to write additional code that checks if the number of megabytes can be converted to an integer number of gigabytes, then result, otherwise output in megabytes. - iksuy

1 answer 1

We check if there is a remainder from the division of 1024. If there is no remainder, then divide by another 1024 and assign "GB". If there is a remainder of the division, then we assign "MB".

  foreach (ManagementObject w in Video) { string gpuTotalMem; if((Convert.ToDouble(w["AdapterRam"]) / int1024) % 1024 == 0){ gpuTotalMem = ((Convert.ToDouble(w["AdapterRam"]) / int1024) / 1024).ToString() + " ГБ"; } else { gpuTotalMem = (Convert.ToDouble(w["AdapterRam"]) / int1024).ToString() + " МБ"; } if (w["Caption"] != null) StringBild.AppendFormat("Ваша Видеокарта: {0} : {1} ", w["Caption"], gpuTotalMem); } 
  • Thanks, it turned out) - GooliveR