Get PC's Monitor Information Using .NET / WMI

ε祈祈猫儿з 提交于 2019-12-19 07:35:11

问题


Is there anyway using WMI/.Net to grab monitor information such as Manufacturer, Serial Number, Monitor Size etc.?

Using a script is an option as well, or can I query the registry directly to get this information?

SELECT * FROM Win32_DesktopMonitor doesn't really return any useful information for me in this case.


回答1:


You may want to try this

https://raw.githubusercontent.com/MaxAnderson95/Get-Monitor-Information/master/Get-Monitor.ps1

Cheers




回答2:


Hey, I use this tool for a lot of my WMI work, especially when prototyping and creating POCs....

Microsoft WMI Code Generator

This tool is great for creating quick console app code for any wmi query or method invocation in both C# and VB.NET

try
        {
            ManagementObjectSearcher searcher = 
                new ManagementObjectSearcher("root\\CIMV2", 
                "SELECT * FROM Win32_DesktopMonitor"); 

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_DesktopMonitor instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Description: {0}", queryObj["Description"]);
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
        }

The code above will get you the make and model of the monitor.




回答3:


That select query should give you what you want. Here is the documentation which contains the details of the query.

Then you could do something like this:

    public void GetMonitorDetails()
    {
       using(ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor")
       {
          foreach(ManagementObject currentObj in searcher.Get())
          {
             String name = currentObj("Name").ToString();
             String device_id = currentObj("DeviceID").ToString();
             // ...
          }
       }
    }



回答4:


This post, combined with the answer below about the WMI management tool had my answer. Here is the code that returns your monitor resolutions.

try {                 
        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher("root\\WMI",
            "SELECT * FROM WmiMonitorBasicDisplayParams");    

        foreach (ManagementObject queryObj in searcher.Get()) {
            Debug.WriteLine("-----------------------------------");
            Debug.WriteLine("WmiMonitorBasicDisplayParams instance");
            Debug.WriteLine("-----------------------------------");
            Debug.WriteLine("Description: {0}", queryObj["SupportedDisplayFeatures"]);
        }
    } catch (ManagementException e) {
        MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
    }

In my case, I'm still stuck, because it is returning the "scaled down" resolution of each monitor. One of mine is a 4K display, being reported as 2560x1440.



来源:https://stackoverflow.com/questions/3477077/get-pcs-monitor-information-using-net-wmi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!