Reputation: 47
I'm using a .NET Core 3.1 console app in VS 2019. I'm trying to get current screen resolution (Windows display). I don't want to get monitor maximum resolution.
I have following batch code that works. I just want to check my resolution is 1024x768 or not.
Simply want to convert this code into true C# code without launching CMD by Process.start
. I want to implement if else to check the resolution is 1024x768 or not.
Here is my batch file:
@echo off
for /f "delims=" %%# in ('"wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
set "%%#">nul
)
echo %CurrentHorizontalResolution%
echo %CurrentVerticalResolution%
pause
Upvotes: 2
Views: 2407
Reputation: 846
As a follow up on the answer from Axel Kemper, you can do this is in a one-liner
//get WMI object for VideoController1
var wmiMonitor = new ManagementObject("Win32_VideoController.DeviceID=\"VideoController1\"");
Then you can get the horizontal and vertical like this
var width = wmiMonitor["CurrentHorizontalResolution"];
var height = wmiMonitor["CurrentVerticalResolution"];
Remember to install the nuget package System.Management before trying.
Upvotes: 2
Reputation: 11322
After installing NuGet package System.Management
, you can access these parameters as explained here:
// https://wutils.com/wmi/
// Install package System.Management (6.0.0)
// using System.Management;
//create a management scope object
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
//create object query
ObjectQuery query =
new ObjectQuery("SELECT * FROM Win32_VideoController "
+ "Where DeviceID=\"VideoController1\"");
//create object searcher
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
//get a collection of WMI objects
ManagementObjectCollection queryCollection =
searcher.Get();
//enumerate the collection.
foreach (ManagementObject m in queryCollection)
{
// access properties of the WMI object
Console.WriteLine("CurrentHorizontalResolution : {0}",
m["CurrentHorizontalResolution"]);
Console.WriteLine("CurrentVerticalResolution : {0}",
m["CurrentVerticalResolution"]);
}
Upvotes: 2