简体   繁体   English

将 NetworkInterface 适配器传递给 combobox 中的方法

[英]Pass NetworkInterface adapter to a method from combobox

Hi All: How can i pass as.networkinterface adater from my combobox to the method?大家好:我如何将 as.networkinterface 适配器从我的 combobox 传递给该方法? GetDeviceInfo获取设备信息

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

I poulate.network devices to my combobox:我 poulate.network 设备到我的 combobox:

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

I have the below method & how can i pass the selected combobox item?我有以下方法&我怎样才能传递选定的 combobox 项目?

I know how to slect the item for ex:cbox1.Items[cbox1.SelectedIndex].ToString() but i couldn't inderstand how to pass as.network adapter?我知道如何为 ex:cbox1.Items[cbox1.SelectedIndex].ToString() 选择项目,但我无法理解如何传递 as.network 适配器?

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();
}

Instead of adding.network interface names to the combobox items you should add your NetworkInterface objects directly.而不是将网络接口名称添加到 combobox 项中,您应该直接添加NetworkInterface对象。

cbox1.Items.Add(nic);

Then you can set the item template in your combobox to fix the display:然后你可以在你的combobox中设置项目模板来修复显示:

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

And you can access your selected.network interface in the event callback:您可以在事件回调中访问您的 selected.network 接口:

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"
    }
}

EDIT: If you really want to add only interface names to the combobox then you can implement the event callback like that:编辑:如果您真的只想向 combobox 添加接口名称,那么您可以像这样实现事件回调:

private void Cbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(e.AddedItems is not null && e.AddedItems.Count > 0 && e.AddedItems[0] is string niName)
    {
        var ni = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(x => x.Name == niName);
        // "ni" can be null!
    }
}

But you may encounter some issues, for example if the two interfaces have the same name or the interface is no longer discovered.但是您可能会遇到一些问题,例如,如果两个接口具有相同的名称或接口不再被发现。 To fix the second problem you could store the list of interfaces in the variable instead of calling GetAllNetworkInterfaces() every time you select a new item.要解决第二个问题,您可以将接口列表存储在变量中,而不是每次 select 一个新项目时都调用GetAllNetworkInterfaces()

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

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