2014-09-27 30 views
3

我正在編寫一個使用圖形卡驗證的程序。 我嘗試過使用多種方式;最接近的一個,我發現用:獲取硬件信息,如顯卡功能

lblGrapics.Text = infotypes.VideocardName.GetName() 

但自動回報等於1 我怎樣才能拿到卡的名稱和其他specifations?

回答

1

可以使用WMI獲取圖形卡信息。您需要引用System.Management並將其導入。

WMI is a great library which contains the details about various components required for the system to operate. Hard Disk Drive related information, processor information, Network components and the list goes on. It is really easy to query the data if you know a little about the data how it is organized.

您必須使用ManagementObjectSearcher類。

例子:

Imports System.Management 


Public Class Form1 

Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click 

    MsgBox(GetGraphicsCardName()) 
End Sub 

Private Function GetGraphicsCardName() As String 
    Dim GraphicsCardName = String.Empty 
    Try 
      Dim WmiSelect As New ManagementObjectSearcher _("rootCIMV2", "SELECT * FROM Win32_VideoController") 
      For Each WmiResults As ManagementObject In WmiSelect.Get() 
       GraphicsCardName = WmiResults.GetPropertyValue("Name").ToString 
       If (Not String.IsNullOrEmpty(GraphicsCardName)) Then 
        Exit For 
       End If 
      Next 
    Catch err As ManagementException 
      MessageBox.Show(err.Message) 
    End Try 
    Return GraphicsCardName 
End Function 
End Class 

Source

+0

程序返回MSGBOX:無效的命名空間 – user3628517 2014-09-27 23:21:32

+0

你有沒有加入參考'System.Management'? – GeorgeChond 2014-09-27 23:24:35

+0

你是什麼意思引用我導入System.Management – user3628517 2014-09-27 23:26:48

5

這將允許您查詢任何WMI類,並獲得所需的值的屬性。在你的情況下,你可以從the Win32_VideoController class中選擇。其他WMI類可以是found here

Imports System.Management 

Public Class WMI 

    Public Shared Function GetWMISettingsDictionary(ByVal wmiClass As String, 
         ShoppingList() As String) As Dictionary(Of String, String) 

     Dim wmiInfo As New Dictionary(Of String, String) 
     Dim searcher As New System.Management.ManagementObjectSearcher("select * from " & wmiClass) 
     For Each item As System.Management.ManagementObject In searcher.Get 

      For Each PC As System.Management.PropertyData In item.Properties 

       ' perform case insensitive search 
       For Each s As String in ShoppingList 
        If s.ToLowerInvariant = PC.Name.ToLowerInvariant Then 
         If PC.Value IsNot Nothing Then 
          wmiInfo.Add(PC.Name, PC.Value.ToString) 
          ' halt search-by-name 
          Exit For 
         End If 
        End If 
       Next 
      Next 
      ' Note: this is to prevent a crash when there is more than one item 
      ' WMI reports on such as 2 display adapters; just get the first one. 
      Exit For 
     Next 

     Return wmiInfo 
    End Function 


    ' helpful tool to see how WMI props are organized, discover the names etc 
    Public Shared Sub DebugWMIPropValues(wmiClass As String) 

     Using searcher As New Management.ManagementObjectSearcher("Select * from " & wmiClass) 
      Dim moReturn As Management.ManagementObjectCollection = searcher.Get 

      For Each mo As Management.ManagementObject In moReturn 
       Console.WriteLine("====") 
       DebugProperties(mo) 

      Next 
     End Using 

    End Sub 

    ' debug tool to poll a management object to get the properties and values 
    Private Shared Sub DebugProperties(mo As Management.ManagementObject) 

     For Each pd As PropertyData In mo.Properties 
      If pd.Value IsNot Nothing Then 

       If pd.Value.GetType Is GetType(String()) Then 
        Dim n As Integer = 0 
        For Each s As String In CType(pd.Value, Array) 
         Console.WriteLine("{0}({1}): {2}", pd.Name, n.ToString, 
              If(pd.Value IsNot Nothing, 
              s, 
              "Nothing")) 
         n += 1 
        Next 
       Else 
        Console.WriteLine("{0}: {1}", pd.Name, 
             If(pd.Value IsNot Nothing, 
             pd.Value.ToString, 
             "Nothing")) 
       End If 
      End If 
     Next 
    End Sub 
End Class 

要使用它,你只需要創建所需的屬性的「購物清單」,並通過WMI類:

Dim shopList() As String = {"VideoProcessor", "Name", "AdapterRAM"} 

' the return is a Dictionary to keep the Name and Value together 
Dim wmiItems As Dictionary(Of String, String) 
wmiItems = WMI.GetWMISettingsDictionary("Win32_VideoController", shopList) 

' print them to the console window: 
For Each kvp As KeyValuePair(Of String, String) In wmiItems 
    Console.WriteLine("Item: {0} value: {1}", kvp.Key, kvp.Value) 
Next 

類包括一種方式來轉儲一個屬性名稱和值類。要使用它:

WMI.DebugWMIPropValues("Win32_VideoController") 

只要看看在結果的輸出窗口(調試菜單 - >窗口 - >輸出繼電器

樣本輸出的購物清單:

Item: AdapterRAM value: 1073741824 
Item: Name value: AMD Radeon HD 6450 
Item: VideoProcessor value: ATI display adapter (0x6779) 

適用於我的系統TM


備註,更新:GetWMISettingsDictionary旨在收集單個物料的屬性。因爲它會得到大多數東西的設置,但只有第一個視頻卡,第一個顯示器等。

有幾種方法可以根據您的需要進行更改。可以修改它以在每個項目的List中單獨返回Dictionary。或者你可以在WHERE子句追加到WMI類名來獲取特定設備的屬性,並經常根據需要調用它:

wmiClass = "Win32_VideoController WHERE Name = 'FizzBar Deluxe'" 
    ' or 
    wmiClass = "Win32_VideoController WHERE DeviceID = 'VideoController1'" 

    wmiItems = WMI.GetWMISettingsDictionary(wmiClass , shopList) 

名稱搜索現在是不區分大小寫。

最後,請注意,對於低端視頻適配器,AdapterRAM將報告總系統RAM。這是因爲沒有任何板上RAM的適配器只是使用系統RAM,所以報告正確。

1

「無效命名空間」是不是System.Management,那是因爲

Dim WmiSelect As New ManagementObjectSearcher _("rootCIMV2", "SELECT * FROM 

的第一個參數是不能接受的。

嘗試使用另一個構造函數不第一個參數:

Dim WmiSelect As New ManagementObjectSearcher _("SELECT * FROM Win32_VideoController")