监控ID和序列号

时间:2011-09-01 12:48:04

标签: c# system

在Windows中,我们有关于Monitros的信息 - 一些独特的名称和ID。 e.g。

  1. Acer xxx
  2. 三星xxx
  3. 我有问题如何在C#中获取信息,因为我知道我们可以从WMI获得的序列号: root \ WMI - > WmiMonitorID

    和关于显示: root / CIMV2 Win32_DesktopMonitor

    但是我必须把这个信息放在一起,它是因为Aceer S / N xxx在Windows中有id 1

    有人有点想法吗?

4 个答案:

答案 0 :(得分:5)

试一试:

using System.Management;

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DesktopMonitor");     
foreach (ManagementObject obj in searcher.Get())
    Console.WriteLine("Description: {0}", obj ["Description"]);

修改

这里有一个链接到一个漂亮的类,它将检索监视器的详细信息:

http://wmimonitor.svn.sourceforge.net/viewvc/wmimonitor/DisplayInfoWMIProvider/WMIProvider/WMIProvider.cs?view=markup

以下是与上述链接相关联的类。它应该为您提供监视器所需的一切:

//DisplayInfoWMIProvider (c) 2009 by Roger Zander

using System;
using System.Collections;
using System.Management.Instrumentation;
using System.DirectoryServices;
using System.Management;
//using System.Security.Principal;
using Microsoft.Win32;
using System.Text;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;            

[assembly: WmiConfiguration(@"root\cimv2", HostingModel = ManagementHostingModel.LocalSystem)]
namespace DisplayInfoWMIProvider
{
    [System.ComponentModel.RunInstaller(true)]
    public class MyInstall : DefaultManagementInstaller
    {
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            System.Runtime.InteropServices.RegistrationServices RS = new System.Runtime.InteropServices.RegistrationServices();

            //This should be fixed with .NET 3.5 SP1
            //RS.RegisterAssembly(System.Reflection.Assembly.LoadFile(Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES%\Reference Assemblies\Microsoft\Framework\v3.5\System.Management.Instrumentation.dll")), System.Runtime.InteropServices.AssemblyRegistrationFlags.SetCodeBase);
        }

        public override void Uninstall(IDictionary savedState)
        {

            try
            {
                ManagementClass MC = new ManagementClass(@"root\cimv2:Win32_MonitorDetails");
                MC.Delete();
            }
            catch { }

            try
            {
                base.Uninstall(savedState);
            }
            catch { }
        }
    }

    [ManagementEntity(Name = "Win32_MonitorDetails")]
    public class DisplayDetails
    {
        [ManagementKey]
        public string PnPID { get; set; }

        [ManagementProbe]
        public string SerialNumber { get; set; }

        [ManagementProbe]
        public string Model { get; set; }

        [ManagementProbe]
        public string MonitorID { get; set; }

        /// <summary>
        /// The Constructor to create a new instances of the DisplayDetails class...
        /// </summary>
        public DisplayDetails(string sPnPID, string sSerialNumber, string sModel, string sMonitorID)
        {
            PnPID = sPnPID;
            SerialNumber = sSerialNumber;
            Model = sModel;
            MonitorID = sMonitorID;
        }

        /// <summary>
        /// This Function returns all Monitor Details
        /// </summary>
        /// <returns></returns>
        [ManagementEnumerator]
        static public IEnumerable GetMonitorDetails()
        {
            //Open the Display Reg-Key
            RegistryKey Display = Registry.LocalMachine;
            Boolean bFailed = false;
            try
            {
                Display = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\DISPLAY");
            }
            catch
            {
                bFailed = true;
            }

            if (!bFailed & (Display != null))
            {

                //Get all MonitorIDss
                foreach (string sMonitorID in Display.GetSubKeyNames())
                {
                    RegistryKey MonitorID = Display.OpenSubKey(sMonitorID);

                    if (MonitorID != null)
                    {
                        //Get all Plug&Play ID's
                        foreach (string sPNPID in MonitorID.GetSubKeyNames())
                        {
                            RegistryKey PnPID = MonitorID.OpenSubKey(sPNPID);
                            if (PnPID != null)
                            {
                                string[] sSubkeys = PnPID.GetSubKeyNames();

                                //Check if Monitor is active
                                if (sSubkeys.Contains("Control"))
                                {
                                    if (sSubkeys.Contains("Device Parameters"))
                                    {
                                        RegistryKey DevParam = PnPID.OpenSubKey("Device Parameters");
                                        string sSerial = "";
                                        string sModel = "";

                                        //Define Search Keys
                                        string sSerFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xff });
                                        string sModFind = new string(new char[] { (char)00, (char)00, (char)00, (char)0xfc });

                                        //Get the EDID code
                                        byte[] bObj = DevParam.GetValue("EDID", null) as byte[];
                                        if (bObj != null)
                                        {
                                            //Get the 4 Vesa descriptor blocks
                                            string[] sDescriptor = new string[4];
                                            sDescriptor[0] = Encoding.Default.GetString(bObj, 0x36, 18);
                                            sDescriptor[1] = Encoding.Default.GetString(bObj, 0x48, 18);
                                            sDescriptor[2] = Encoding.Default.GetString(bObj, 0x5A, 18);
                                            sDescriptor[3] = Encoding.Default.GetString(bObj, 0x6C, 18);

                                            //Search the Keys
                                            foreach (string sDesc in sDescriptor)
                                            {
                                                if (sDesc.Contains(sSerFind))
                                                {
                                                    sSerial = sDesc.Substring(4).Replace("\0", "").Trim();
                                                }
                                                if (sDesc.Contains(sModFind))
                                                {
                                                    sModel = sDesc.Substring(4).Replace("\0", "").Trim();
                                                }
                                            }


                                        }
                                        if (!string.IsNullOrEmpty(sPNPID + sSerFind + sModel + sMonitorID))
                                        {
                                            yield return new DisplayDetails(sPNPID, sSerial, sModel, sMonitorID);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

答案 1 :(得分:1)

根/ CIMV2 / Win32_DesktopMonitor / PnPDeviceID 只显示我的5个显示器中的2个 和root / WMI / WMIMonitorId / InstanceName 显示我的所有5个显示器

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM WmiMonitorID",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "WmiMonitorID instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "InstanceName: " & objItem.InstanceName
Next

答案 2 :(得分:0)

例如,我们使用它来使用WMI从主HDD中检索串行:

var search = new ManagementObjectSearcher("select * from Win32_LogicalDisk where DeviceID = 'C:'");
var serials = search.Get().OfType<ManagementObject>();
m_clientToken = serials.ElementAt(0)["VolumeSerialNumber"].ToString();

也许您可以利用它来获取您的监视器信息,因为您知道要搜索哪个Mgmt对象。您基本上使用SQL来检索您正在寻找的内容。

答案 3 :(得分:0)

在我看来,root / CIMV2 / Win32_DesktopMonitor / PnPDeviceID (1)和root / WMI / WMIMonitorId / InstanceName (2)几乎相同

我使用WMI Explorer

在我的计算机上找到了以下内容

(1)DISPLAY \ HWP2868 \ 5&amp; 3EB7FBC&amp; 0&amp; UID16777472

(2)DISPLAY \ HWP2868 \ 5&amp; 3eb7fbc&amp; 0&amp; UID16777472_0

有两个不同之处:(2)末尾的_0和(2)的部分是小写的事实。

我目前没有多个显示器可供参考,因此我无法为您提供更准确的方式来关联这两个条目。但在我看来,你可以编写两个查询,在其中一个中修改搜索条件以匹配其他格式。但是你需要调查是否有可靠的模式来做到这一点。

无论如何,似乎是足够的公共元素,能够在代码中进行匹配,如果不是通过查询。