2011-09-01 86 views
1

在Windows中,我們對我們的Monitros信息 - 一些獨特的名稱和編號。例如顯示器ID和序列號

  1. 宏基XXX
  2. 三星XXX

我qeuestion如何讓C#中的信息,因爲我知道該序列號,我們可以從WMI得到: 根\ WMI - > WmiMonitorID

約顯示: 根/ CIMV2 Win32_DesktopMonitor

但我必須有這個infromation在一起,它meens Aceer S/N XXX具有ID 1在Windows

有一些人的想法?

回答

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

也許你可以利用它來獲取顯示器的信息,因爲你知道哪管理對象進行搜索。你基本上使用SQL來檢索你要找的東西。

+0

感謝您的答覆。我知道如何獲得SERIALNUMBER信息,但問題是如何得到這樣的信息:我們有3個顯示器:S/N:XXX,S/N:YYY,S/N:ZZZ和我wolud喜歡什麼現在是這種顯示器,其中顯示器是首先在Windows的ID,第二... – jeremmy

0

在我看來,這根/ CIMV2/Win32_DesktopMonitor/PnPDeviceID(1)和根/ WMI/WMIMonitorId/實例名(2)幾乎相同

我發現我的計算機上的以下使用WMI Explorer

(1)DISPLAY \ HWP2868 \ 5 & 3EB7FBC UID16777472

(2)DISPLAY \ HWP2868 \ 5 & 3eb7fbc UID16777472_0

有兩點不同:在(2)和所述事實結束時_0那部分(2)是下殼。

我沒有比此刻參考一臺顯示器較多,因此我不能爲您提供更準確的方式向兩個條目關聯。但它看起來像你對我可以寫兩個查詢,修改其中的一個搜索條件匹配其他的格式。但是你需要調查是否有可靠的模式來做到這一點。

無論如何,似乎是足夠的通用元素,如果不通過查詢,能夠在代碼中進行匹配。

+0

有什麼奇怪的,我有兩個顯示器,但是在根/ CIMV2/Win32_DesktopMonitor我只填寫PnPDeviceID的第二臺監視器。 – jeremmy

+0

好的。我不能說這是爲什麼。 – havardhu

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); 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

感謝答覆,可惜的描述不是唯一的名字,因爲我有這種Reuslts的(我有兩個顯示器):默認監視器,默認監視器,通用即插即用監視器 – jeremmy

+0

代碼來自此鏈接給有關的SerialNumber等細節信息,但不給我信息什麼是在Windows顯示器的順序:第一個顯示器,第二個...... – jeremmy

+0

@jeremmy:你不必使用描述。將其替換爲您想要的任何字段 - 序列號等。另請參閱編輯答案並鏈接到課程。 –

1

根/ CIMV2/Win32_DesktopMonitor/PnPDeviceID 只顯示我的5個監視 和2根/ WMI/WMIMonitorId /實例名 顯示我的顯示器

的所有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 
+0

同樣適用於我:Win32_DesktopMonitor顯示2,WMIMonitorId顯示5。 –