Search code examples
c#asp.net-core.net-core.net-8.0system.diagnostics

How to grab current user display information?


I'm looking for something similar to the output of following the powershell script:

Get-WmiObject Win32_VideoController | Select SystemName, CurrentHorizontalResolution, CurrentVerticalResolution

Which outputs:

SystemName CurrentHorizontalResolution CurrentVerticalResolution
---------- --------------------------- -------------------------
PC_NAME    3840                      2160

Any help or guidance will be appreciated.


Solution

  • you can use System.Management library

    using System;
    using System.Management;
    
    namespace VideoControllerInfo
    {
       class Program
       {
           static void Main(string[] args)
           {
               try
               {
                   var searcher = new ManagementObjectSearcher("SELECT SystemName, CurrentHorizontalResolution, CurrentVerticalResolution FROM Win32_VideoController");
    
                   foreach (ManagementObject queryObj in searcher.Get())
                   {
                       Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);
                       Console.WriteLine("CurrentHorizontalResolution: {0}", queryObj["CurrentHorizontalResolution"]);
                       Console.WriteLine("CurrentVerticalResolution: {0}", queryObj["CurrentVerticalResolution"]);
                       Console.WriteLine();
                   }
               }
               catch (ManagementException e)
               {
                   Console.WriteLine("An error occurred while querying WMI: " + e.Message);
               }
           }
       }
      }