I'm trying to get the Monitor properties in C#. I use the WMI to get the information I need. Currently I can get correct PNPID from Win32_DesktopMonitor and compare with the result of InstanceName from WmiMonitorListedSupportedSourceModes. Then I want to get detail information of the monitor by VideoModeDescriptor. Here are my codes below, but I always get the fail as "invalid query" of scherDM2c.Get(). May I know what kind of mistake I made?
private void MonitorBox_SelectedIndexChanged(object sender, EventArgs e)
{
ManagementObjectSearcher searcherM = new ManagementObjectSearcher($"SELECT * FROM Win32_DesktopMonitor Where Name Like '%{MonitorBox.Text}%'");
foreach (ManagementObject wmi in searcherM.Get())
{
dMaker.Text = wmi.GetPropertyValue("MonitorManufacturer").ToString();
dID.Text = wmi.GetPropertyValue("PNPDeviceID").ToString();
ManagementObjectSearcher searcherDM2a = new ManagementObjectSearcher("root\\wmi", "SELECT * FROM WmiMonitorListedSupportedSourceModes");
foreach (ManagementObject sbu_wmi in searcherDM2a.Get())
{
string WMIID = sbu_wmi.GetPropertyValue("InstanceName").ToString();
if (WMIID.ToUpper().Contains(dID.Text.ToUpper()))
{
ManagementObjectSearcher scherDM2c = new ManagementObjectSearcher("root\\wmi", $"SELECT * FROM WmiMonitorListedSupportedSourceModes Where InstanceName Like %{WMIID}%");
//ManagementObjectCollection MColl = scherDM2c.Get();
foreach (ManagementObject ModeList in scherDM2c.Get())
{
listBox1.Items.Add(ModeList.GetPropertyValue("HorizontalActivePixels").ToString());
}
/*listBox1.Items.Add(sbu_wmi.GetPropertyValue("PreferredMonitorSourceModeIndex").ToString());
var i = sbu_wmi.GetPropertyValue("PreferredMonitorSourceModeIndex").ToString();
listBox1.Items.Add(sbu_wmi.GetPropertyValue("MonitorSourceModes").ToString());*/
}
}
/*listBox1.Items.Add(wmi.GetPropertyValue("CreationClassName").ToString());
listBox1.Items.Add(wmi.GetPropertyValue("Caption").ToString());
listBox1.Items.Add(wmi.GetPropertyValue("MonitorType").ToString());*/
}
}