private static MIB_IFTABLE GetAllIfTable() { //缓冲区大小 uint dwSize = 0; //获取缓冲区大小 uint ret = GetIfTable(null, ref dwSize, false); if (ret == 50) { //此函数仅支持于 win98/nt 系统 return(null); } //定义,获取 MIB_IFTABLE 对象 MIB_IFTABLE tbl = new MIB_IFTABLE((int)dwSize); ret = GetIfTable(tbl.ByteArray, ref dwSize, false); //如果不成功 if (ret != 0) { return(null); } return(tbl); }
/// <summary> /// New - Undocumented! /// </summary> /// <param name="IgnoreLoopBack"></param> /// <remarks></remarks> public clsNetworkStats([System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute(true)] // ERROR: Optional parameters aren't supported in C# bool IgnoreLoopBack) { long ret = 0; MIB_IFROW ifrow = new MIB_IFROW(); MIB_IFTABLE IfTable = new MIB_IFTABLE(); int tablesize = 0; IntPtr iBuf = IntPtr.Zero; //get tablesize ret = GetIfTable(iBuf, ref tablesize, true); //resize buffer on tablesize iBuf = Marshal.AllocHGlobal(tablesize); //load buffer ret = GetIfTable(iBuf, ref tablesize, true); //marshar from buffer into MIB_IFTABLE structure IfTable = (MIB_IFTABLE)Marshal.PtrToStructure(iBuf, typeof(MIB_IFTABLE)); //initialize adapter list m_Adapters = new ArrayList(); int noInterfaces = Convert.ToInt32(IfTable.dwNumEntries); //NIC Interfaces number int IFROWSize = Marshal.SizeOf(ifrow); MIB_IFROW[] mrows = new MIB_IFROW[(int)noInterfaces + 1]; byte[] mDest = null; mDest = new byte[IFROWSize * noInterfaces + 1]; for (byte i = 1; i <= Convert.ToByte(noInterfaces); i++) { mrows[i - 1] = (MIB_IFROW)Marshal.PtrToStructure(new IntPtr(iBuf.ToInt32() + 4 + (i - 1) * IFROWSize), typeof(MIB_IFROW)); IFROW_HELPER ifhelp = PrivToPub(mrows[i - 1]); if (IgnoreLoopBack == true) { if (ifhelp.Description.IndexOf("Loopback") < 0) { m_Adapters.Add(ifhelp); } } else { m_Adapters.Add(ifhelp); } } Marshal.FreeHGlobal(iBuf); }
private static List <NetInfo> GetALLNetInfo() { List <NetInfo> ninfos = new List <NetInfo>(); MIB_IFTABLE tbl = GetAllIfTable(); if (tbl != null) { tbl.Deserialize(); for (int i = 0; i < tbl.Table.Length; i++) { var netinfo = GetNetInfo(tbl.Table[i]); var obj = ninfos.FindLast(x => x.OutOctets.Equals(netinfo.OutOctets) && x.InOctets.Equals(netinfo.InOctets)); if (obj == null && netinfo.OutOctets > 0 && netinfo.InOctets > 0) { ninfos.Add(netinfo); } } } return(ninfos); }