gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
how read Monitor,and Size with wmi in c#
- User
how read Monitor,and Size with wmi in c#
- 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
ManagementObjectSearcherto query theWin32_DesktopMonitorclass in the WMI namespaceroot\\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.Managementassembly 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.