如何获取 Windows 网络适配器使用的连接名称?

发布于 2024-12-23 06:42:20 字数 424 浏览 0 评论 0原文

是否可以使用 WMI 查询来获取它?

我当前的代码:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                                       "SELECT * FROM Win32_NetworkAdapte");

foreach (ManagementObject queryObj in searcher.Get())
{
     Console.WriteLine(queryObj[??]);        
}

我尝试从以下位置获取连接名称:

Control Panel \ Network and Internet \ Network Connections

Is it possible get it using WMI query?

my current code:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                                       "SELECT * FROM Win32_NetworkAdapte");

foreach (ManagementObject queryObj in searcher.Get())
{
     Console.WriteLine(queryObj[??]);        
}

I'm tried get the connections name from:

Control Panel \ Network and Internet \ Network Connections

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

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

发布评论

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

评论(2

寒尘 2024-12-30 06:42:20

使用下面的代码,您将能够转储网络适配器的所有属性,您需要 Name 属性:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
    "SELECT * FROM Win32_NetworkAdapter");

foreach (ManagementObject adapter in searcher.Get())
{
    StringBuilder propertiesDump = new StringBuilder();
    foreach (var property in adapter.Properties)
    {
        propertiesDump.AppendFormat(
            "{0} == {1}{2}", 
            property.Name, 
            property.Value, 
            Environment.NewLine);        
    }
}

或者简单地使用 LINQ(添加 using System.Linq):

foreach (ManagementObject adapter in searcher.Get())
{
   string adapterName = adapter.Properties
                               .Cast<PropertyData>()
                               .Single(p => p.Name == "Name")
                               .Value.ToString();
}

PS:也可以意识到您在 WMI 查询中输入错误 - 忘记了 Adapter 中的 r:Win32_NetworkAdapte_r_

Using code below you would be able dump all properties of the Network Adapter, you need Name property:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
    "SELECT * FROM Win32_NetworkAdapter");

foreach (ManagementObject adapter in searcher.Get())
{
    StringBuilder propertiesDump = new StringBuilder();
    foreach (var property in adapter.Properties)
    {
        propertiesDump.AppendFormat(
            "{0} == {1}{2}", 
            property.Name, 
            property.Value, 
            Environment.NewLine);        
    }
}

OR simply using LINQ (add using System.Linq):

foreach (ManagementObject adapter in searcher.Get())
{
   string adapterName = adapter.Properties
                               .Cast<PropertyData>()
                               .Single(p => p.Name == "Name")
                               .Value.ToString();
}

PS: Also be aware you've typo in WMI query - forgot r in Adapter: Win32_NetworkAdapte_r_

ˉ厌 2024-12-30 06:42:20

让我们稍微修改一下代码并缩短它。 ManagementClass 类缩短了 WMI 查询。

ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection results = mc.GetInstances();

foreach (var result in results)
{
    foreach (PropertyData data in result.Properties)
        Console.WriteLine($"{data.Name} == {data.Value}");
}

Let's change the code a little more and shorten it. ManagementClass class shortens the WMI query.

ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection results = mc.GetInstances();

foreach (var result in results)
{
    foreach (PropertyData data in result.Properties)
        Console.WriteLine(
quot;{data.Name} == {data.Value}");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文