//</Snippet42> //<Snippet43> public static void DisplayAnycastAddresses() { int count = 0; Console.WriteLine("Anycast Addresses"); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); IPAddressInformationCollection anyCast = adapterProperties.AnycastAddresses; if (anyCast.Count > 0) { Console.WriteLine(adapter.Description); foreach (IPAddressInformation any in anyCast) { Console.WriteLine(" Anycast Address .......................... : {0} {1} {2}", any.Address, any.IsTransient ? "Transient" : "", any.IsDnsEligible ? "DNS Eligible" : "" ); count++; } Console.WriteLine(); } } if (count == 0) { Console.WriteLine(" No anycast addressses were found."); Console.WriteLine(); } }
// This constructor is for Vista and newer internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { adapterFlags = ipAdapterAddresses.flags; dnsSuffix = ipAdapterAddresses.dnsSuffix; dnsEnabled = fixedInfo.EnableDns; dynamicDnsEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0); multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection( IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress)); dnsAddresses = IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress); anycastAddresses = IpAdapterAddress.MarshalIpAddressInformationCollection( ipAdapterAddresses.firstAnycastAddress); unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection( ipAdapterAddresses.firstUnicastAddress); winsServersAddresses = IpAdapterAddress.MarshalIpAddressCollection( ipAdapterAddresses.firstWinsServerAddress); gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection( IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress)); dhcpServers = new IPAddressCollection(); if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero) dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress()); if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero) dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress()); if ((adapterFlags & AdapterFlags.IPv4Enabled) != 0) { ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses); } if ((adapterFlags & AdapterFlags.IPv6Enabled) != 0) { ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); } }
private static void ShowIPAddressInformationCollection(IPAddressInformationCollection collection) { for (var i = 0; i < collection.Count; i++) { Console.WriteLine(GetIPAddressInfo(collection[i].Address)); } }
internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { this.dnsEnabled = fixedInfo.EnableDns; this.index = ipAdapterAddresses.index; this.name = ipAdapterAddresses.AdapterName; this.ipv6Index = ipAdapterAddresses.ipv6Index; if (this.index > 0) { this.versionSupported |= IPVersion.IPv4; } if (this.ipv6Index > 0) { this.versionSupported |= IPVersion.IPv6; } this.mtu = ipAdapterAddresses.mtu; this.adapterFlags = ipAdapterAddresses.flags; this.dnsSuffix = ipAdapterAddresses.dnsSuffix; this.dynamicDnsEnabled = (ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0; this.multicastAddresses = SystemMulticastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstMulticastAddress); this.dnsAddresses = SystemIPAddressInformation.ToAddressCollection(ipAdapterAddresses.FirstDnsServerAddress, this.versionSupported); this.anycastAddresses = SystemIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstAnycastAddress, this.versionSupported); this.unicastAddresses = SystemUnicastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstUnicastAddress); if (this.ipv6Index > 0) { this.ipv6Properties = new SystemIPv6InterfaceProperties(this.ipv6Index, this.mtu); } }
public static string GetInfoString(this IPInterfaceProperties adapterProperties) { string re = ""; IPAddressCollection dnsServers = adapterProperties.DnsAddresses; if (dnsServers != null) { re += "DNS服务器:"; foreach (IPAddress dns in dnsServers) { re += $"{dns}\n"; } re += "\n"; } IPAddressInformationCollection anyCast = adapterProperties.AnycastAddresses; if (anyCast != null) { re += "任播地址:"; foreach (IPAddressInformation any in anyCast) { re += $"{any.Address}\n"; } re += "\n"; } MulticastIPAddressInformationCollection multiCast = adapterProperties.MulticastAddresses; if (multiCast != null) { re += "组播地址:"; foreach (IPAddressInformation multi in multiCast) { re += $"{multi.Address}\n"; } re += "\n"; } UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses; if (uniCast != null) { string lifeTimeFormat = "dddd, MMMM dd, yyyy hh:mm:ss tt"; foreach (UnicastIPAddressInformation uni in uniCast) { DateTime time; re += "单播地址:"; re += $"{uni.Address}\n"; re += $"\t前缀来源:\t{uni.PrefixOrigin}\n"; re += $"\t后缀来源:\t{uni.SuffixOrigin}\n"; re += $"\t重复地址检测:\t{uni.DuplicateAddressDetectionState}\n"; time = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime); time = time.ToLocalTime(); re += $"\t\t有效期至:\t{time.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture)}\n"; time = DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime); time = time.ToLocalTime(); re += $"\t\tDHCP释放时间:\t{time.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture)}\n"; } } return(re); }
// Display the interface information that doesn't update so often that // it should be on the timer that fires every 100 ms. private void UpdateCurrentNicInformation() { // Set the DNS suffix if any exists IPInterfaceProperties ipProperties = currentInterface.GetIPProperties(); this.dnsSuffixTextLabel.Text = ipProperties.DnsSuffix.ToString(); // Display the IP address information associated with this // interface including anycast, unicast, multicast, DNS servers, // WINS servers, DHCP servers, and the gateway. this.addressListView.Items.Clear(); IPAddressInformationCollection anycastInfo = ipProperties.AnycastAddresses; foreach (IPAddressInformation info in anycastInfo) { InsertAddress(info.Address, "Anycast"); } UnicastIPAddressInformationCollection unicastInfo = ipProperties.UnicastAddresses; foreach (UnicastIPAddressInformation info in unicastInfo) { InsertAddress(info.Address, "Unicast"); } MulticastIPAddressInformationCollection multicastInfo = ipProperties.MulticastAddresses; foreach (MulticastIPAddressInformation info in multicastInfo) { InsertAddress(info.Address, "Multicast"); } GatewayIPAddressInformationCollection gatewayInfo = ipProperties.GatewayAddresses; foreach (GatewayIPAddressInformation info in gatewayInfo) { InsertAddress(info.Address, "Gateway"); } IPAddressCollection ipAddresses = ipProperties.WinsServersAddresses; InsertAddresses(ipAddresses, "WINS Server"); ipAddresses = ipProperties.DhcpServerAddresses; InsertAddresses(ipAddresses, "DHCP Server"); ipAddresses = ipProperties.DnsAddresses; InsertAddresses(ipAddresses, "DNS Server"); }
private static string FormatIPAddressInformationCollection(IPAddressInformationCollection collection, string label) { string result = string.Empty; if (collection != null) { foreach (IPAddressInformation addressInformation in collection) { result += string.Format(label, addressInformation.Address.ToString(), addressInformation.IsTransient, addressInformation.IsDnsEligible); } } return(result); }
internal static IPAddressInformationCollection MarshalIpAddressInformationCollection(IntPtr ptr) { IPAddressInformationCollection addressList = new IPAddressInformationCollection(); while (ptr != IntPtr.Zero) { IpAdapterAddress addressStructure = Marshal.PtrToStructure <IpAdapterAddress>(ptr); IPAddress address = addressStructure.address.MarshalIPAddress(); addressList.InternalAdd(new SystemIPAddressInformation(address, addressStructure.flags)); ptr = addressStructure.next; } return(addressList); }
internal static IPAddressInformationCollection MarshalIpAddressInformationCollection(IntPtr ptr) { IPAddressInformationCollection addressList = new IPAddressInformationCollection(); IpAdapterAddress *pIpAdapterAddress = (IpAdapterAddress *)ptr; while (pIpAdapterAddress != null) { addressList.InternalAdd(new SystemIPAddressInformation( pIpAdapterAddress->address.MarshalIPAddress(), pIpAdapterAddress->flags)); pIpAdapterAddress = pIpAdapterAddress->next; } return(addressList); }
internal static IPAddressInformationCollection ToAddressInformationCollection(IntPtr ptr, IPVersion versionSupported) { IPAddressInformationCollection informations = new IPAddressInformationCollection(); if (ptr != IntPtr.Zero) { IPEndPoint point; IpAdapterAddress adapterAddress = (IpAdapterAddress) Marshal.PtrToStructure(ptr, typeof(IpAdapterAddress)); AddressFamily family = (adapterAddress.address.addressLength > 0x10) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; SocketAddress socketAddress = new SocketAddress(family, adapterAddress.address.addressLength); Marshal.Copy(adapterAddress.address.address, socketAddress.m_Buffer, 0, adapterAddress.address.addressLength); if (family == AddressFamily.InterNetwork) { point = (IPEndPoint) IPEndPoint.Any.Create(socketAddress); } else { point = (IPEndPoint) IPEndPoint.IPv6Any.Create(socketAddress); } informations.InternalAdd(new SystemIPAddressInformation(adapterAddress, point.Address)); while (adapterAddress.next != IntPtr.Zero) { adapterAddress = (IpAdapterAddress) Marshal.PtrToStructure(adapterAddress.next, typeof(IpAdapterAddress)); family = (adapterAddress.address.addressLength > 0x10) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; if (((family == AddressFamily.InterNetwork) && ((versionSupported & IPVersion.IPv4) > IPVersion.None)) || ((family == AddressFamily.InterNetworkV6) && ((versionSupported & IPVersion.IPv6) > IPVersion.None))) { socketAddress = new SocketAddress(family, adapterAddress.address.addressLength); Marshal.Copy(adapterAddress.address.address, socketAddress.m_Buffer, 0, adapterAddress.address.addressLength); if (family == AddressFamily.InterNetwork) { point = (IPEndPoint) IPEndPoint.Any.Create(socketAddress); } else { point = (IPEndPoint) IPEndPoint.IPv6Any.Create(socketAddress); } informations.InternalAdd(new SystemIPAddressInformation(adapterAddress, point.Address)); } } } return informations; }
internal SystemIPInterfaceProperties(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses) { _adapterFlags = ipAdapterAddresses.flags; _dnsSuffix = ipAdapterAddresses.dnsSuffix; _dnsEnabled = fixedInfo.enableDns; _dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0); _multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress)); _dnsAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress); _anycastAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection( ipAdapterAddresses.firstAnycastAddress); _unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection( ipAdapterAddresses.firstUnicastAddress); _winsServersAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection( ipAdapterAddresses.firstWinsServerAddress); _gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress)); _dhcpServers = new InternalIPAddressCollection(); if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress()); } if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress()); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv4Enabled) != 0) { _ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0) { _ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); } }
/// <summary> /// Gets the local IP address of this computer /// <para> /// It will return <c>localhost</c> if no networks are available, or <c>127.0.0.1</c> if there are multiple /// interfaces, otherwise the actual IP address /// </para> /// </summary> /// <returns></returns> public static string LocalIpAddress() { //There is no network available, so IDK if (!NetworkInterface.GetIsNetworkAvailable()) { return("localhost"); } //Get all the network interfaces NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); List <NetworkInterface> activeNetworkInterfaces = networkInterfaces.AsValueEnumerable().Where( networkInterface => networkInterface.OperationalStatus == OperationalStatus.Up && !networkInterface.IsReceiveOnly) .ToList(); //If there is more then one network interface, default to local host if (activeNetworkInterfaces.Count is > 1 or < 1) { return("127.0.0.1"); } //Get the address NetworkInterface activeInterface = activeNetworkInterfaces[0]; IPAddressInformationCollection addresses = activeInterface.GetIPProperties().AnycastAddresses; // ReSharper disable once ForCanBeConvertedToForeach for (int i = 0; i < addresses.Count; i++) { IPAddressInformation information = addresses[i]; if (information.Address.AddressFamily == AddressFamily.InterNetwork) { return(information.Address.ToString()); } } //F**k Do I know what to do if we hit here return("localhost"); }
internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo) { this.dnsEnabled = fixedInfo.EnableDns; this.name = ipAdapterInfo.adapterName; this.index = ipAdapterInfo.index; this.multicastAddresses = new MulticastIPAddressInformationCollection(); this.anycastAddresses = new IPAddressInformationCollection(); if (this.index > 0) { this.versionSupported |= IPVersion.IPv4; } if (ComNetOS.IsWin2K) { this.ReadRegDnsSuffix(); } this.unicastAddresses = new UnicastIPAddressInformationCollection(); foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList()) { this.unicastAddresses.InternalAdd(new SystemUnicastIPAddressInformation(ipAdapterInfo, address)); } try { this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo); if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0)) { this.dnsAddresses = this.ipv4Properties.DnsAddresses; } } catch (NetworkInformationException exception) { if (exception.ErrorCode != 0x57L) { throw; } } }
private string GetIPAddresses(IPInterfaceProperties adapterProperties) { StringBuilder ipAddressesBuilder = new StringBuilder(); IPAddressCollection dnsServers = adapterProperties.DnsAddresses; if (dnsServers.Count > 0) { foreach (IPAddress dns in dnsServers) { ipAddressesBuilder.AppendFormat(" DNS Server ................. : {0}\n", dns.ToString()); } } GatewayIPAddressInformationCollection gateways = adapterProperties.GatewayAddresses; if (gateways.Count > 0) { foreach (GatewayIPAddressInformation gatewayAddresssInfo in gateways) { ipAddressesBuilder.AppendFormat(" Gateway Address ............ : {0}\n", gatewayAddresssInfo.Address); } } IPAddressCollection dhcpServers = adapterProperties.DhcpServerAddresses; if (dhcpServers.Count > 0) { foreach (IPAddress dhcpServer in dhcpServers) { ipAddressesBuilder.AppendFormat(" DHCP Server ................ : {0}\n", dhcpServer); } } IPAddressInformationCollection anyCast = adapterProperties.AnycastAddresses; if (anyCast.Count > 0) { foreach (IPAddressInformation any in anyCast) { ipAddressesBuilder.AppendFormat(" Anycast Address ............ : {0} {1} {2}\n", any.Address, any.IsTransient ? "Transient" : String.Empty, any.IsDnsEligible ? "DNS Eligible" : String.Empty); } } MulticastIPAddressInformationCollection multiCast = adapterProperties.MulticastAddresses; if (multiCast.Count > 0) { foreach (IPAddressInformation multi in multiCast) { ipAddressesBuilder.AppendFormat(" Multicast Address .......... : {0} {1} {2}\n", multi.Address, multi.IsTransient ? "Transient" : String.Empty, multi.IsDnsEligible ? "DNS Eligible" : String.Empty); } } UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses; if (uniCast.Count > 0) { string lifeTimeFormat = "dddd, MMMM dd, yyyy hh:mm:ss tt"; foreach (UnicastIPAddressInformation uni in uniCast) { DateTime when; ipAddressesBuilder.AppendFormat(" Unicast Address ............ : {0}\n", uni.Address); ipAddressesBuilder.AppendFormat(" Prefix Origin ............. : {0}\n", uni.PrefixOrigin); ipAddressesBuilder.AppendFormat(" Suffix Origin ............. : {0}\n", uni.SuffixOrigin); ipAddressesBuilder.AppendFormat(" Duplicate Address Detection : {0}\n", uni.DuplicateAddressDetectionState); // Format the lifetimes as Saturday, March 13, 2010 11:33:44 PM // if en-us is the current culture. // Calculate the date and time at the end of the lifetimes. when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime); when = when.ToLocalTime(); ipAddressesBuilder.AppendFormat(" Valid Life Time ........... : {0}\n", when.ToString(lifeTimeFormat, new CultureInfo("en-US"))); when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressPreferredLifetime); when = when.ToLocalTime(); ipAddressesBuilder.AppendFormat(" Preferred life time ....... : {0}\n", when.ToString(lifeTimeFormat, new CultureInfo("en-US"))); when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime); when = when.ToLocalTime(); ipAddressesBuilder.AppendFormat(" DHCP Leased Life Time ..... : {0}\n", when.ToString(lifeTimeFormat, new CultureInfo("en-US"))); } } return(ipAddressesBuilder.ToString()); }
public static void ShowIPAddresses(IPInterfaceProperties adapterProperties) { IPAddressCollection dnsServers = adapterProperties.DnsAddresses; if (dnsServers != null) { foreach (IPAddress dns in dnsServers) { Console.WriteLine(" DNS Servers ............................. : {0}", dns.ToString() ); } } IPAddressInformationCollection anyCast = adapterProperties.AnycastAddresses; if (anyCast != null) { foreach (IPAddressInformation any in anyCast) { Console.WriteLine(" Anycast Address .......................... : {0} {1} {2}", any.Address, any.IsTransient ? "Transient" : "", any.IsDnsEligible ? "DNS Eligible" : "" ); } Console.WriteLine(); } MulticastIPAddressInformationCollection multiCast = adapterProperties.MulticastAddresses; if (multiCast != null) { foreach (IPAddressInformation multi in multiCast) { Console.WriteLine(" Multicast Address ....................... : {0} {1} {2}", multi.Address, multi.IsTransient ? "Transient" : "", multi.IsDnsEligible ? "DNS Eligible" : "" ); } Console.WriteLine(); } UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses; if (uniCast != null) { string lifeTimeFormat = "dddd, MMMM dd, yyyy hh:mm:ss tt"; foreach (UnicastIPAddressInformation uni in uniCast) { DateTime when; Console.WriteLine(" Unicast Address ......................... : {0}", uni.Address); Console.WriteLine(" Prefix Origin ........................ : {0}", uni.PrefixOrigin); Console.WriteLine(" Suffix Origin ........................ : {0}", uni.SuffixOrigin); Console.WriteLine(" Duplicate Address Detection .......... : {0}", uni.DuplicateAddressDetectionState); // Format the lifetimes as Sunday, February 16, 2003 11:33:44 PM // if en-us is the current culture. // Calculate the date and time at the end of the lifetimes. when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime); when = when.ToLocalTime(); Console.WriteLine(" Valid Life Time ...................... : {0}", when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) ); when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressPreferredLifetime); when = when.ToLocalTime(); Console.WriteLine(" Preferred life time .................. : {0}", when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) ); when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime); when = when.ToLocalTime(); Console.WriteLine(" DHCP Leased Life Time ................ : {0}", when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) ); } Console.WriteLine(); } }
internal static IPAddressInformationCollection MarshalIpAddressInformationCollection(IntPtr ptr) { IPAddressInformationCollection addressList = new IPAddressInformationCollection(); while (ptr != IntPtr.Zero) { IpAdapterAddress addressStructure = (IpAdapterAddress)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddress)); IPAddress address = addressStructure.address.MarshalIPAddress(); addressList.InternalAdd(new SystemIPAddressInformation(address, addressStructure.flags)); ptr = addressStructure.next; } return addressList; }
internal static MulticastIPAddressInformationCollection ToMulticastIpAddressInformationCollection(IPAddressInformationCollection addresses) { MulticastIPAddressInformationCollection multicastList = new MulticastIPAddressInformationCollection(); foreach (IPAddressInformation addressInfo in addresses) { multicastList.InternalAdd(new SystemMulticastIPAddressInformation((SystemIPAddressInformation)addressInfo)); } return multicastList; }
internal static MulticastIPAddressInformationCollection ToMulticastIpAddressInformationCollection(IPAddressInformationCollection addresses) { MulticastIPAddressInformationCollection multicastList = new MulticastIPAddressInformationCollection(); foreach (IPAddressInformation addressInfo in addresses) { multicastList.InternalAdd(new SystemMulticastIPAddressInformation((SystemIPAddressInformation)addressInfo)); } return(multicastList); }
private void UpdateNetworkInformation() { lstAddress.Items.Clear(); // 取得支援IP通訊協定之網路介面卡的相關資訊 IPInterfaceProperties ipProperties = selectedInterface.GetIPProperties(); // 取得DHCP伺服器位址 IPAddressCollection ipAddresses = ipProperties.DhcpServerAddresses; foreach (IPAddress ipAddress in ipAddresses) { addIPAddress("DHCP伺服器", ipAddress); } // 取得網路介面卡所設定之DNS伺服器位址 ipAddresses = ipProperties.DnsAddresses; foreach (IPAddress ipAddress in ipAddresses) { addIPAddress("DNS伺服器", ipAddress); } // 取得WINS伺服器的位址 ipAddresses = ipProperties.WinsServersAddresses; foreach (IPAddress ipAddress in ipAddresses) { addIPAddress("WINS伺服器", ipAddress); } // 取得網路介面卡的網路閘道位址 GatewayIPAddressInformationCollection gatewayInfo = ipProperties.GatewayAddresses; foreach (GatewayIPAddressInformation info in gatewayInfo) { addIPAddress("網路閘道", info.Address); } // 取得網路介面卡的Anycast IP位址 IPAddressInformationCollection anycastInfo = ipProperties.AnycastAddresses; foreach (IPAddressInformation info in anycastInfo) { addIPAddress("Anycast", info.Address); } // 取得網路介面卡的多點播送位址 MulticastIPAddressInformationCollection multicastInfo = ipProperties.MulticastAddresses; foreach (MulticastIPAddressInformation info in multicastInfo) { addIPAddress("多點播送", info.Address); } // 取得網路介面卡的單點傳送位址 UnicastIPAddressInformationCollection unicastInfo = ipProperties.UnicastAddresses; foreach (UnicastIPAddressInformation info in unicastInfo) { addIPAddress("單點傳送", info.Address); } }
private static void GetIPAddressesInfoString(IPInterfaceProperties adapterProperties, StringBuilder sb) { IPAddressCollection dnsServers = adapterProperties.DnsAddresses; if (dnsServers != null) { foreach (IPAddress dns in dnsServers) { sb.AppendFormat(" DNS Servers ............................. : {0}\n", dns.ToString() ); } } IPAddressInformationCollection anyCast = adapterProperties.AnycastAddresses; if (anyCast != null) { foreach (IPAddressInformation any in anyCast) { sb.AppendFormat(" Anycast Address .......................... : {0} {1} {2}\n", any.Address, any.IsTransient ? "Transient" : "", any.IsDnsEligible ? "DNS Eligible" : "" ); } sb.AppendLine(); } MulticastIPAddressInformationCollection multiCast = adapterProperties.MulticastAddresses; if (multiCast != null) { foreach (IPAddressInformation multi in multiCast) { sb.AppendFormat(" Multicast Address ....................... : {0} {1} {2}\n", multi.Address, multi.IsTransient ? "Transient" : "", multi.IsDnsEligible ? "DNS Eligible" : "" ); } sb.AppendLine(); } UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses; if (uniCast != null) { string lifeTimeFormat = "dddd, MMMM dd, yyyy hh:mm:ss tt"; foreach (UnicastIPAddressInformation uni in uniCast) { DateTime when; sb.AppendFormat(" Unicast Address ......................... : {0}\n", uni.Address); sb.AppendFormat(" Prefix Origin ........................ : {0}\n", uni.PrefixOrigin); sb.AppendFormat(" Suffix Origin ........................ : {0}\n", uni.SuffixOrigin); sb.AppendFormat(" Duplicate Address Detection .......... : {0}\n", uni.DuplicateAddressDetectionState); // Calculate the date and time at the end of the lifetimes. when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime); when = when.ToLocalTime(); sb.AppendFormat(" Valid Life Time ...................... : {0}\n", when.ToString(lifeTimeFormat, CultureInfo.CurrentCulture) ); when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressPreferredLifetime); when = when.ToLocalTime(); sb.AppendFormat(" Preferred life time .................. : {0}\n", when.ToString(lifeTimeFormat, CultureInfo.CurrentCulture) ); when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime); when = when.ToLocalTime(); sb.AppendFormat(" DHCP Leased Life Time ................ : {0}\n", when.ToString(lifeTimeFormat, CultureInfo.CurrentCulture) ); } sb.AppendLine(); } }
static IPAddressInformationCollection Win32FromAnycast (IntPtr ptr) { var c = new IPAddressInformationCollection (); Win32_IP_ADAPTER_ANYCAST_ADDRESS a; for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) { a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_ANYCAST_ADDRESS)); c.InternalAdd (new SystemIPAddressInformation ( a.Address.GetIPAddress (), a.LengthFlags.IsDnsEligible, a.LengthFlags.IsTransient)); } return c; }
public static IIPAddressInformationCollection ToInterface([CanBeNull] this IPAddressInformationCollection collection) { return((collection == null) ? null : new IPAddressInformationCollectionAdapter(collection)); }