tingfungc
tingfungc

Reputation: 115

What is the reference namespace of Win32_NetworkAdapter in visual studio C# express?

I am currently writing a small program that has to remote control some devices (their service and hardware such as Lan port). I have googled and read many info about WMI and now I am trying to make my program.

But I couldn't find the reference namespace of Win32_NetworkAdapter class. I have already import System.Management and Microsoft.Win32 into the project but the SDK still told me that Win32_NetworkAdapter could not be found.

What am I missing here?

PS. I am using windows XP, VS Express 2010 Express for dev.

Thanks for your help.


Here is my code atm:

string manage = "SELECT * From Win32_NetworkAdapter";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage);
ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject obj in collection)
{
    if (obj["Name"].ToString() != null)
    {
        if (obj["Name"].ToString() == "Realtek RTL8168C(P)/8111C(P) PCI-E Gigabit Ethernet NIC")
        {
            obj.InvokeMethod("Disable", null);
            textBox3.Text += "finished";
        }
    }
}

I ran it in debug mode and I found that the properties (Classpath, Options, Path, Scope) of obj shows an error message:

Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.

Any Ideas?

Upvotes: 3

Views: 2330

Answers (3)

Hans Passant
Hans Passant

Reputation: 941625

WMI "classes" are not classes in the C# sense. They picked that noun because they resemble a class. They have a name, properties and methods, just like a C# class.

Under the hood it is a COM interface. Which also explains the debugger warning you got, COM interfaces have thread affinity. The debugger evaluates watch expressions on a separate thread, the debugger thread. Which can deadlock when you have a breakpoint active, the thread that accesses the interface is frozen by the debugger. This is not a real problem, just an inconvenience.

It isn't clear what kind of problem you are having, you wouldn't be able to get that debugger warning if the Win32_NetworkAdapter query didn't return something. Getting the Name subtly wrong would be a possibility, note how your code would only work on another machine by accident. And WMI does tend to be flaky, it heavily relies on drivers to return the info and drivers are not created equal. Their quality is roughly proportional to the amount of money you spent on the hardware. By far the best way to experiment and test a WMI query is with the WMI Code Creator utility. It lets you execute arbitrary queries, even has an option to auto-generate the C# code you need. Highly recommended.

Upvotes: 4

Christian.K
Christian.K

Reputation: 49260

You either use them as @Wiktor Zychla suggested, your you generate a strongly typed C# class using mgmtclassgen.exe. Also see this SO question/answer for more information and links on that topic.

Upvotes: 1

Wiktor Zychla
Wiktor Zychla

Reputation: 48240

You don't use WMI classes in C# as classes. Instead, you pass class names to WMI query methods:

var search = new ManagementObjectSearcher( "SELECT * FROM Win32_NetworkAdapter" );

Upvotes: 0

Related Questions