By user3628517


2014-09-27 22:29:42 8 Comments

I'm writing a program that is using a validation of graphic card. I tried using multiple ways; the closest one i found was using:

lblGrapics.Text = infotypes.VideocardName.GetName()

but the automatic return is equals 1. how can i get the card name and other specifations?

3 comments

@m_skipper 2015-03-17 13:26:05

"invalid namespace" is not about System.Management, it is because the first parameter of

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

is not accepted.

Try to use another constructor without first parameter:

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

@Ňɏssa Pøngjǣrdenlarp 2014-09-28 00:28:34

This will allow you to poll any WMI class and get the desired values for the properties. In your case, you would select from the Win32_VideoController class. Other WMI class can be 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

To use it you just create a "shopping list" of the properties you want and pass the WMI Class:

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

The class includes a way to dump the property names and values for a class. To use it:

WMI.DebugWMIPropValues("Win32_VideoController")

Just look in the Output window for the results (Debug menu -> Windows -> Ouput)

Sample output for the shopping list:

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

Works on My SystemTM


Notes, Update: GetWMISettingsDictionary is intended for harvesting the properties for a single item. As is, it will get the settings for most things, but only the first video card, the first display etc.

There are several ways to change this depending on what you need. It could be modified to return a separate Dictionary in a List for each item. Or you could append a WHERE clause to the WMI class name to get the properties for a specific device and call it as often as needed:

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

  wmiItems = WMI.GetWMISettingsDictionary(wmiClass , shopList)

The name search is now case-insensitive.

Finally, note that with low-end video adapters, AdapterRAM will report total System RAM. This is because adapters without any on-board RAM simply use system RAM, so it is reporting correctly.

@GeorgeChond 2014-09-27 22:46:38

It is possible to get graphics card info using WMI. You need to reference the System.Management and import it.

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.

You have to use the ManagementObjectSearcher class.

Example:

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

@user3628517 2014-09-27 23:21:32

the program returns msgbox: invalid namespace

@GeorgeChond 2014-09-27 23:24:35

Have you added reference to System.Management?

@user3628517 2014-09-27 23:26:48

what do you mean by reference i've imported the System.Management

@GeorgeChond 2014-09-27 23:27:31

Related Questions

Sponsored Content

39 Answered Questions

5 Answered Questions

[SOLVED] How do I access named capturing groups in a .NET Regex?

  • 2009-05-25 12:11:00
  • UnkwnTech
  • 194484 View
  • 249 Score
  • 5 Answer
  • Tags:   c# .net regex

26 Answered Questions

[SOLVED] How can I get the application's path in a .NET console application?

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

10 Answered Questions

[SOLVED] How do you get a string from a MemoryStream?

3 Answered Questions

[SOLVED] Get Hardware IDs like Microsoft does

1 Answered Questions

[SOLVED] Multiple monitors/graphic cards info using powershell

  • 2017-01-18 15:28:43
  • Jakub Zahorak
  • 2137 View
  • 1 Score
  • 1 Answer
  • Tags:   powershell wmi

0 Answered Questions

How to identify the running graphic card in a system with more than one?

  • 2014-06-05 22:49:01
  • Germán Arduino
  • 90 View
  • 0 Score
  • 0 Answer
  • Tags:   wmi gpu

1 Answered Questions

How to get piv card authentication and certificate information

1 Answered Questions

Get network usage for ethernet cards with MAC addresses

Sponsored Content