您的位置:首页 > 编程语言 > C#

c#,使用WMI对象获取系统的DPI。

2012-04-06 12:42 232 查看
在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象。

我们使用的WMI对象是:Win32_DesktopMonitor

对象参考:http://msdn.microsoft.com/zh-cn/library/Aa394122

代码:

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();
}

}
}


以上代码在WIN7的环境下测试通过。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: