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

发布于 2025-01-08 07:57:34 字数 994 浏览 1 评论 0原文

我目前正在编写一个小程序,它必须远程控制一些设备(它们的服务和硬件,例如 LAN 端口)。我已经用谷歌搜索并阅读了许多有关 WMI 的信息,现在我正在尝试编写我的程序。

但我找不到 Win32_NetworkAdapter 类的引用命名空间。我已经将System.Management和Microsoft.Win32导入到项目中,但SDK仍然告诉我找不到Win32_NetworkAdapter。

我在这里缺少什么?

附言。我使用 Windows XP、VS Express 2010 Express 进行开发。

感谢您的帮助。


这是我的代码 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";
        }
    }
}

我在调试模式下运行它,我发现 obj 的属性(类路径、选项、路径、范围)显示错误消息:

因先前的函数评估而禁用函数评估 超时。您必须继续执行才能重新启用功能 评价。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何时共饮酒 2025-01-15 07:57:34

WMI“类”不是 C# 意义上的类。他们选择这个名词是因为他们类似于一个类。它们有名称、属性和方法,就像 C# 类一样。

在底层它是一个 COM 接口。这也解释了您收到的调试器警告:COM 接口具有线程关联性。调试器在单独的线程(调试器线程)上计算监视表达式。当断点处于活动状态时,可能会出现死锁,访问接口的线程会被调试器冻结。这不是一个真正的问题,只是带来不便。

目前尚不清楚您遇到了什么类型的问题,如果 Win32_NetworkAdapter 查询没有返回某些内容,您将无法收到调试器警告。名称可能会出现微妙的错误,请注意您的代码只能意外地在另一台机器上运行。而且 WMI 确实不稳定,它严重依赖驱动程序返回信息,而驱动程序生来就不平等。它们的质量大致与您在硬件上花费的金额成正比。到目前为止,实验和测试 WMI 查询的最佳方法是使用 WMI 代码创建实用程序。它允许您执行任意查询,甚至可以选择自动生成您需要的 C# 代码。强烈推荐。

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.

坚持沉默 2025-01-15 07:57:34

您可以按照 @Wiktor Zychla 的建议使用它们,然后使用 mgmtclassgen.exe。另请参阅此SO问题/答案,以获取有关该主题的更多信息和链接。

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.

海风掠过北极光 2025-01-15 07:57:34

您不将 C# 中的 WMI 类用作类。相反,您将类名传递给 WMI 查询方法:

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

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" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文