Hi All: How can i pass as networkinterface adater from my combobox to the method ? GetDeviceInfo
Private string GetDeviceInfo(NetworkInterface adapter) ?
I poulate network devices to my 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?
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?
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();
}
CodePudding user response:
Instead of adding network interface names to the combobox items you should add your NetworkInterface
objects directly.
cbox1.Items.Add(nic);
Then you can set the item template in your combobox to fix the display:
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
And you can access your selected network interface in the event callback:
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:
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.