简体   繁体   English

Visual Studio C#express中Win32_NetworkAdapter的引用命名空间是什么?

[英]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). 我目前正在编写一个小程序,必须远程控制一些设备(他们的服务和硬件,如Lan端口)。 I have googled and read many info about WMI and now I am trying to make my program. 我用Google搜索并阅读了很多关于WMI的信息,现在我正在努力制作我的程序。

But I couldn't find the reference namespace of Win32_NetworkAdapter class. 但我找不到Win32_NetworkAdapter类的引用名称空间。 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. 我已经将System.Management和Microsoft.Win32导入到项目中,但SDK仍告诉我无法找到Win32_NetworkAdapter。

What am I missing here? 我在这里错过了什么?

PS. PS。 I am using windows XP, VS Express 2010 Express for dev. 我正在使用Windows XP,VS Express 2010 Express for dev。

Thanks for your help. 谢谢你的帮助。


Here is my code atm: 这是我的代码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: 我在调试模式下运行它,我发现obj的属性(Classpath,Options,Path,Scope)显示错误消息:

Function evaluation disabled because a previous function evaluation timed out. 由于先前的功能评估超时,因此功能评估被禁用。 You must continue execution to reenable function evaluation. 您必须继续执行以重新启用功能评估。

Any Ideas? 有任何想法吗?

WMI "classes" are not classes in the C# sense. WMI“类”不是C#意义上的类。 They picked that noun because they resemble a class. 他们选择了这个名词,因为它们类似于一个类。 They have a name, properties and methods, just like a C# class. 它们有一个名称,属性和方法,就像C#类一样。

Under the hood it is a COM interface. 在引擎盖下它是一个COM接口。 Which also explains the debugger warning you got, COM interfaces have thread affinity. 这也解释了你得到的调试器警告,COM接口具有线程关联性。 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 . 目前尚不清楚您遇到了什么样的问题,如果Win32_NetworkAdapter查询没有返回某些内容 ,您将无法获得调试器警告。 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. 并且WMI确实往往是片状的,它严重依赖于驱动程序来返回信息并且驱动程序不是平等的。 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 . 到目前为止,实验和测试WMI查询的最佳方法是使用WMI Code Creator实用程序 It lets you execute arbitrary queries, even has an option to auto-generate the C# code you need. 它允许您执行任意查询,甚至可以选择自动生成所需的C#代码。 Highly recommended. 强烈推荐。

You either use them as @Wiktor Zychla suggested, your you generate a strongly typed C# class using mgmtclassgen.exe . 你要么把它们作为@Wiktor Zychla建议,你用你生成一个强类型的C#类mgmtclassgen.exe Also see this SO question/answer for more information and links on that topic. 另请参阅此SO问题/答案以获取有关该主题的更多信息和链接。

You don't use WMI classes in C# as classes. 您不要在C#中使用WMI类作为类。 Instead, you pass class names to WMI query methods: 而是将类名传递给WMI查询方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM