Code: Select all
IPAddress.Any?
Ich denke, ich habe es herausgefunden, zumindest funktioniert es für mich. Vielleicht war meine Frage schlecht formuliert, aber hier ist meine Lösung: < /p>
Code: Select all
private IPAddress SelectIPAddress()
{
Dictionary networkAdaptors = new Dictionary();
IPAddress selectedIP = null;
foreach( NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces() )
{
if( netInterface.OperationalStatus == OperationalStatus.Up && ( netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet || netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ) )
{
if( !networkAdaptors.ContainsKey( netInterface.NetworkInterfaceType ) )
{
networkAdaptors.Add( netInterface.NetworkInterfaceType, new List() );
}
IPInterfaceProperties ipProps = netInterface.GetIPProperties();
foreach( UnicastIPAddressInformation addr in ipProps.UnicastAddresses )
{
if( !addr.Address.IsIPv6LinkLocal ) // I'm a traditionalist
{
networkAdaptors[netInterface.NetworkInterfaceType].Add( addr.Address );
}
}
}
}
if( networkAdaptors.ContainsKey( NetworkInterfaceType.Ethernet ) )
{
// Just use the first Ethernet interface
//
selectedIP = networkAdaptors[NetworkInterfaceType.Ethernet][0];
}
else if( networkAdaptors.ContainsKey( NetworkInterfaceType.Wireless80211 ) )
{
// Otherwise just use the first WiFi interface
//
selectedIP = networkAdaptors[NetworkInterfaceType.Wireless80211][0];
}
return selectedIP; // null means no suitable network interface found.
}