Before using the WMI object, you must add a reference to System. Management, and then you can call the WMI Object.
The WMI object we use is: win32_shorttopmonitor.
Object Reference: http://msdn.microsoft.com/zh-cn/library/Aa394122
Code:
Static void Main (string [] args ){
Using (ManagementClass mc = new ManagementClass ("Win32_DesktopMonitor ")){
Using (ManagementObjectCollection moc = mc. GetInstances ()){
Int PixelsPerXLogicalInch = 0; // dpi for x
Int PixelsPerYLogicalInch = 0; // dpi for y
Foreach (ManagementObject each in moc ){
PixelsPerXLogicalInch = int. Parse (each. Properties ["PixelsPerXLogicalInch"]. Value. ToString ()));
PixelsPerYLogicalInch = int. Parse (each. Properties ["PixelsPerYLogicalInch"]. Value. ToString ()));
}
Console. WriteLine ("PixelsPerXLogicalInch:" + PixelsPerXLogicalInch. ToString ());
Console. WriteLine ("PixelsPerYLogicalInch:" + PixelsPerYLogicalInch. ToString ());
Console. Read ();
}
}
}
The above code passes the test in WIN7 environment.
From the white ocean