Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

how read Monitor,and Size with wmi in c#

  1. User

    how read Monitor,and Size with wmi in c#

  2. Assistant

    To read the monitor's size with WMI in C#, you can use the following code:

    using System;
    using System.Management;
    
    public class Program
    {
        public static void Main(string[] args)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DesktopMonitor");
    
            foreach (ManagementObject queryObj in searcher.Get())
            {
                string monitorName = queryObj["Name"].ToString();
                string monitorSize = queryObj["ScreenWidth"].ToString() + "x" + queryObj["ScreenHeight"].ToString();
                
                Console.WriteLine("Monitor Name: " + monitorName);
                Console.WriteLine("Monitor Size: " + monitorSize);
            }
    
            Console.ReadKey();
        }
    }
    

    This code uses ManagementObjectSearcher to query the Win32_DesktopMonitor class in the WMI namespace root\\CIMV2. It retrieves the name and screen resolution properties of each monitor and prints them to the console.

    Make sure to add a reference to the System.Management assembly in your project. You can do this by right-clicking on the project in Visual Studio, selecting "Add" -> "Reference", and then finding and selecting the "System.Management" assembly.

    Hope this helps!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.