如何将网络接口添加到组合框并根据所选接口解析 ip?
How to add Network Interfaces to a Combobox and resolve ip based on selected interface?
为了练习,我想创建一个带有可用网络接口 (localhost) 的可选下拉框,并且在选择其中一个时应该有一个 ip 解析(将 ip4 写入一个文本框,将 ip6 写入下一个框)所选接口
到目前为止我有:
private void localhostip(object sender, EventArgs e)
{
var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = mc.GetInstances();
foreach (var mo in moc)
{
if ((bool)mo["ipEnabled"])
{
comboBox1.Items.Add(mo["Caption"].ToString());
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//ACQUIRE THE IP4 & IP6 ADDRESSES
string host = Dns.GetHostName();
IPAddress[] ips = Dns.GetHostAddresses(host);
tblocalhost.CharacterCasing = CharacterCasing.Upper;
tblocalip.CharacterCasing = CharacterCasing.Upper;
tblocalhost.Text = host;
if (comboBox1.SelectedIndex == 0)
{
tblocalip.Text = null;
tblocalip6.Text = null;
foreach (IPAddress ip4 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))
{
tblocalip.Text = ip4.ToString();
}
foreach (IPAddress ip6 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))
{
tblocalip6.Text = ip6.ToString();
}
}
if (comboBox1.SelectedIndex == 1)
{
tblocalip.Text = null;
tblocalip6.Text = null;
foreach (IPAddress ip4 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))
{
tblocalip.Text = ip4.ToString();
}
foreach (IPAddress ip6 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))
{
tblocalip6.Text = ip6.ToString();
}
}
}
注意:localhostip 在表单启动时执行。
那么现在如何将接口索引的选择绑定到接口 IP 的实际检查?
如果可能的话建议corrections/additions我的代码,为了让我的代码简单易懂,我不想只复制别人的代码就完成了,我想学习
在获取适配器名称和 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();
}
}
为了练习,我想创建一个带有可用网络接口 (localhost) 的可选下拉框,并且在选择其中一个时应该有一个 ip 解析(将 ip4 写入一个文本框,将 ip6 写入下一个框)所选接口
到目前为止我有:
private void localhostip(object sender, EventArgs e)
{
var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = mc.GetInstances();
foreach (var mo in moc)
{
if ((bool)mo["ipEnabled"])
{
comboBox1.Items.Add(mo["Caption"].ToString());
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//ACQUIRE THE IP4 & IP6 ADDRESSES
string host = Dns.GetHostName();
IPAddress[] ips = Dns.GetHostAddresses(host);
tblocalhost.CharacterCasing = CharacterCasing.Upper;
tblocalip.CharacterCasing = CharacterCasing.Upper;
tblocalhost.Text = host;
if (comboBox1.SelectedIndex == 0)
{
tblocalip.Text = null;
tblocalip6.Text = null;
foreach (IPAddress ip4 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))
{
tblocalip.Text = ip4.ToString();
}
foreach (IPAddress ip6 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))
{
tblocalip6.Text = ip6.ToString();
}
}
if (comboBox1.SelectedIndex == 1)
{
tblocalip.Text = null;
tblocalip6.Text = null;
foreach (IPAddress ip4 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))
{
tblocalip.Text = ip4.ToString();
}
foreach (IPAddress ip6 in ips.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))
{
tblocalip6.Text = ip6.ToString();
}
}
}
注意:localhostip 在表单启动时执行。
那么现在如何将接口索引的选择绑定到接口 IP 的实际检查?
如果可能的话建议corrections/additions我的代码,为了让我的代码简单易懂,我不想只复制别人的代码就完成了,我想学习
在获取适配器名称和 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();
}
}