/// <summary> /// Returns objects that describe the network interfaces on the local computer. /// </summary> /// <returns>A <see cref="NetworkInterface"/> array that contains objects that describe the available network interfaces, or an empty array if no interfaces are detected.</returns> /// <remarks> /// The network interfaces on a computer provide network connectivity. /// Network interfaces are also known as network adapters. /// <list type="table"><listheader><term>Platforms Supported</term><description></description></listheader> /// <item><term>Windows Mobile</term><description>Windows Mobile Version 5.0 and later</description></item> /// <item><term>Windows Embedded Compact</term><description>Windows CE .NET 4.1 and later</description></item> /// </list> /// </remarks> public static NetworkInterface[] GetAllNetworkInterfaces() { if (!NativeMethods.hasIphlp) { return(new NetworkInterface[0] { }); } int size = 0; int result = NativeMethods.GetInterfaceInfo(IntPtr.Zero, ref size); IntPtr ptr = Marshal.AllocHGlobal(size); result = NativeMethods.GetInterfaceInfo(ptr, ref size); int numAdapters = Marshal.ReadInt32(ptr); NetworkInterface[] interfaces = new NetworkInterface[numAdapters]; for (int i = 0; i < numAdapters; i++) { NativeMethods.IP_ADAPTER_INDEX_MAP aim = (NativeMethods.IP_ADAPTER_INDEX_MAP)Marshal.PtrToStructure(IntPtrInTheHand.Add(ptr, 4 + (i * 260)), typeof(NativeMethods.IP_ADAPTER_INDEX_MAP)); NativeMethods.MIB_IFROW row = new NativeMethods.MIB_IFROW(); row.dwIndex = aim.Index; result = NativeMethods.GetIfEntry(ref row); interfaces[i] = new NetworkInterface(aim.Name, row); } Marshal.FreeHGlobal(ptr); return(interfaces); }
internal IPv4InterfaceStatistics(uint index) { this.ifRow = new NativeMethods.MIB_IFROW(); this.ifRow.dwIndex = index; int result = NativeMethods.GetIfEntry(ref this.ifRow); if (result < 0) { throw new NetworkInformationException(result); } }
private NetworkInterface(string name, NativeMethods.MIB_IFROW row) { this.row = row; this.name = name; if (row.dwDescrLen > 0) { this.description = System.Text.Encoding.ASCII.GetString(row.bDescr, 0, row.dwDescrLen); int nullIndex = this.description.IndexOf('\0'); if (nullIndex > -1) { this.description = this.description.Substring(0, nullIndex); } } }
internal IPv4InterfaceStatistics(NativeMethods.MIB_IFROW row) { this.ifRow = row; }
private IPv4InterfaceStatistics() { this.ifRow = new NativeMethods.MIB_IFROW(); }