如何将网络接口添加到组合框并根据所选接口解析IP?

时间:2018-12-21 12:54:25

标签: c# combobox network-interface

为了练习,我想创建一个具有可用网络接口的可选下拉框(localhost),并在选择其中一个后,应为该选定接口设置一个ip分辨率(将ip4写入一个文本框,将ip6写入下一个框)。

到目前为止,我有:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<canvas id="chart" width="800" height="400" style="background: #202020"></canvas>

注意:localhostip在启动窗体时执行。

那么现在我如何将接口索引的选择绑定到对该接口IP的实际检查上?

如果可能的话,建议对我的代码进行更正/添加,以使其对我来说简单易懂,我不想只复制别人的代码并完成,我想学习

1 个答案:

答案 0 :(得分:0)

获取适配器名称和IP地址时,我建议如下所示:

通过将它们存储在字典中,您可以通过它们的适配器名称来访问它们。

请参阅我的评论以获取有关所使用过程的详细说明。

我希望这会有所帮助,我希望对评论进行彻底的了解。

有关存储适配器名称和IP的方法的说明

//create a dictionary storing the name and address of each adapter
public Dictionary<string,IPAddress> ip4ByAdapter = new Dictionary<string,IPAddress>();
public Dictionary<string,IPAddress> ip6ByAdapter = new Dictionary<string,IPAddress>();

void GetIpsAndAdapters()
{
    //Get the network interfaces
    NetworkInterface[] netInterfaces = NetworkInterface.GetAllNetworkInterfaces();

    //don't forget to clear the combo box
    comboBox1.Items.Clear();

    foreach(NetworkInterface adapter in netInterfaces)
    {
        //get the IP Properties for short hand
        IPInterfaceProperties ipProps = adapter.GetIPProperties();

        //set a default value of 0.0.0.0
        IPAddress ipv4 = new IPAddress(0); 

        //if it has one, store the ipv4 of the current adapter
        if(ipProps.UnicastAddresses.Count > 1)
            ipv4 = ipProps.UnicastAddresses[1].Address;

        //set a default value of 0.0.0.0
        IPAddress ipv6 = new IPAddress(0);

        //if it has one, store the ipv6 of the current adapter
        if (ipProps.UnicastAddresses.Count > 0)
            ipv6 = ipProps.UnicastAddresses[0].Address;

        //store the matching pair of adapter and ip address in dictionary
        //check for duplicates (loopbacks perhaps) and ignore them
        if(!ip4ByAdapter.ContainsKey(adapter.Name))
            ip4ByAdapter.Add(adapter.Name, ipv4);

        //same for ipv6
        if(!ip6ByAdapter.ContainsKey(adapter.Name))
            ip6ByAdapter.Add(adapter.Name, ipv6);

        comboBox1.Items.Add(adapter.Name);
    }
}

comboBox1.SelectedIndexChanged事件的注意事项

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //if we haven't selected an item yet, just do nothing
    if(comboBox1.SelectedIndex == -1 || comboBox1.SelectedItem is null)
        return;

    //reset the textboxes, just to an empty string
    tblocalip.Text = "";
    tblocalip6.Text = "";

    //if the Dictionary contains a matching adapter to the one selected in the combo box
    if(ip4ByAdapter.ContainsKey(comboBox1.SelectedItem.ToString()))
    {
        //then show it
        tblocalip.Text = ip4ByAdapter[comboBox1.SelectedItem.ToString()].ToString();
    }

    //same for ipv6
    if(ip6ByAdapter.ContainsKey(comboBox1.SelectedItem.ToString()))
    {
        tblocalip6.Text = ip6ByAdapter[comboBox1.SelectedItem.ToString()].ToString();
    }
}