how get monitor width and height from wmi in c#
- User
how get monitor width and height from wmi in c#
- Assistant
To get the monitor's width and height using WMI in C#, you can follow these steps:
-
Add a reference to the System.Management assembly to your project.
-
Import the System.Management namespace in your C# file.
-
Use the ManagementObjectSearcher class to execute a WMI query to retrieve monitor information.
-
Iterate through the resulting ManagementObjectCollection to access individual monitors.
-
Use the ManagementObject class to access the properties of each monitor.
Here's an example code snippet that demonstrates how to get the monitor's width and height using WMI in C#:
using System; using System.Management; namespace MonitorInfoExample { class Program { static void Main(string[] args) { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DesktopMonitor"); foreach (ManagementObject queryObj in searcher.Get()) { string name = queryObj["Name"].ToString(); int width = Convert.ToInt32(queryObj["ScreenWidth"]); int height = Convert.ToInt32(queryObj["ScreenHeight"]); Console.WriteLine("Monitor Name: " + name); Console.WriteLine("Width: " + width); Console.WriteLine("Height: " + height); Console.WriteLine(); } Console.ReadKey(); } } }Note: The code above retrieves information for all the monitors connected to the system. If you want to target a specific monitor, you can modify the WMI query accordingly, for example, by filtering based on the monitor's name or device ID.
-