【问题标题】:Pass NetworkInterface adapter to a method from combobox将 NetworkInterface 适配器传递给组合框的方法
【发布时间】:2022-11-29 21:19:45
【问题描述】:

大家好:我如何作为网络接口适配器从我的组合框传递到该方法?获取设备信息

私有字符串 GetDeviceInfo(网络接口适配器)?

我将网络设备填充到我的组合框中:

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                cbox1.Items.Add(nic.Name);
            }

我有以下方法&如何传递选定的组合框项目?

我知道如何为 ex:xbox 1.Items[combobox1.SelectedIndex].ToString() 选择项目,但我无法理解如何作为网络适配器传递?

private void cbox1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
//
}

private string GetDeviceInfo(NetworkInterface adapter)
{
    if (adapter == null)
    {
        return String.Empty;
    }

    IPInterfaceProperties properties = adapter.GetIPProperties();

    StringBuilder infoBuilder = new StringBuilder();

    infoBuilder.Append(adapter.Description + "\n");
    infoBuilder.Append("=================================================\n");
    infoBuilder.AppendFormat(" ID ......................... : {0}\n",
        adapter.Id);
    infoBuilder.AppendFormat(" Name ....................... : {0}\n",
        adapter.Name);
    infoBuilder.AppendFormat(" Interface type ............. : {0}\n",
        adapter.NetworkInterfaceType);
    infoBuilder.AppendFormat(" Physical Address ........... : {0}\n",
               BitConverter.ToString(adapter.GetPhysicalAddress().GetAddressBytes()));
    infoBuilder.AppendFormat(" Operational status ......... : {0}\n",
        adapter.OperationalStatus);
    infoBuilder.AppendFormat(" Speed ...................... : {0} Mb/s\n",
        adapter.Speed / 1000000);

    string versions = String.Empty;

    // Create a display string for the supported IP versions.
    if (adapter.Supports(NetworkInterfaceComponent.IPv4))
    {
        versions = "IPv4";
    }
    if (adapter.Supports(NetworkInterfaceComponent.IPv6))
    {
        if (versions.Length > 0)
        {
            versions += " ";
        }
        versions += "IPv6";
    }

    infoBuilder.AppendFormat(" IP version ................. : {0}\n",
        versions);

    infoBuilder.Append(GetIPAddresses(properties));

    // The following information is not useful for loopback adapters.
    if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
    {
        return infoBuilder.ToString();
    }

    infoBuilder.AppendFormat(" DNS suffix ................. : {0}\n",
        properties.DnsSuffix);

    if (adapter.Supports(NetworkInterfaceComponent.IPv4))
    {
        IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();

        infoBuilder.AppendFormat(" Index ...................... : {0}\n",
            ipv4.Index);
        infoBuilder.AppendFormat(" MTU ........................ : {0}\n",
            ipv4.Mtu);
        infoBuilder.AppendFormat(" APIPA active ............... : {0}\n",
            ipv4.IsAutomaticPrivateAddressingActive);
        infoBuilder.AppendFormat(" APIPA enabled .............. : {0}\n",
            ipv4.IsAutomaticPrivateAddressingEnabled);
        infoBuilder.AppendFormat(" DHCP enabled ............... : {0}\n",
            ipv4.IsDhcpEnabled);
        infoBuilder.AppendFormat(" Forwarding enabled.......... : {0}\n",
            ipv4.IsForwardingEnabled);
        infoBuilder.AppendFormat(" Uses WINS .................. : {0}\n",
            ipv4.UsesWins);

        if (ipv4.UsesWins)
        {
            IPAddressCollection winsServers = properties.WinsServersAddresses;
            if (winsServers.Count > 0)
            {
                foreach (IPAddress winsServer in winsServers)
                {
                    infoBuilder.AppendFormat(" WINS Server ................ : {0}\n",
                        winsServer);
                }
            }
        }
    }

    if (adapter.Supports(NetworkInterfaceComponent.IPv6))
    {
        IPv6InterfaceProperties ipv6 = properties.GetIPv6Properties();

        infoBuilder.AppendFormat(" Index ...................... : {0}\n",
            ipv6.Index);
        infoBuilder.AppendFormat(" MTU ........................ : {0}\n",
            ipv6.Mtu);
    }

    infoBuilder.AppendFormat(" DNS enabled ................ : {0}\n",
        properties.IsDnsEnabled);
    infoBuilder.AppendFormat(" Dynamically configured DNS . : {0}\n",
        properties.IsDynamicDnsEnabled);
    infoBuilder.AppendFormat(" Receive Only ............... : {0}\n",
        adapter.IsReceiveOnly);
    infoBuilder.AppendFormat(" Multicast .................. : {0}\n",
        adapter.SupportsMulticast);

    return infoBuilder.ToString();
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    不要将网络接口名称添加到组合框项目中,而应直接添加 NetworkInterface 对象。

    cbox1.Items.Add(nic);
    

    然后你可以在你的组合框中设置项目模板来修复显示:

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    

    您可以在事件回调中访问您选择的网络接口:

    private void cbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(e.AddedItems is not null && e.AddedItems.Count > 0 && e.AddedItems[0] is NetworkInterface ni)
        {
           // do something with "ni"
        }
    }
    
    

    【讨论】:

    • 谢谢,我不知道我们可以这样传递,GetDeviceInfo(ni);有没有其他不绑定的方式通过?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2020-07-01
    相关资源
    最近更新 更多