程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 在C#中調用API獲取網絡信息和流量

在C#中調用API獲取網絡信息和流量

編輯:關於C語言

最近一項目中要求顯示網絡流量,而且必須使用C#。

事實上,調用 IpHlpApi.dll 的 GetIfTable API 可以輕易獲得網絡信息和網絡流量。只是要在C#中實現還是比較復雜。

先看看怎麼定義該 API
[DllImport("IpHlpApi.dll")]
        extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
本來想把 pIfTable 定義為 IntPtr,但是這樣的結果是,獲取的信息是錯誤的(直到現在都不知是什麼原因)。

但顯然定義為 byte[] 是不能直接使用的。幸好在 Google Code Search 找到了三個類:

CustomtMarshaler.cs
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace Lemony.SystemInfo
{
   
    /**//// <summary>
    /// CustomMarshaler class implementation.
    /// </summary>
    public abstract class CustomMarshaler
    {
        Fields#region FIElds
        // The internal buffer
        internal byte[] data;
        private MemoryStream stream;
        private BinaryReader binReader;
        private BinaryWriter binWriter;
       
        #endregion
   
        constructors#region constructors

        public CustomMarshaler()
        {

        }
       
        #endregion

        public methods#region public methods

        public void Deserialize()
        {
            if (data != null)
            {
                if (binReader != null)
                {
                    binReader.Close();
                    stream.Close();
                }
                // Create a steam from byte array
                stream = new MemoryStream(data);
                binReader = new BinaryReader(stream, System.Text.Encoding.Unicode);
                ReadFromStream(binReader);
                binReader.Close();
            }

        }

        public void Serialize()
        {
            if (data != null)
            {
                stream = new MemoryStream(data);
                binWriter = new BinaryWriter(stream, System.Text.Encoding.Unicode);
                WriteToStream(binWriter);
                binWriter.Close();
            }
        }

        public int GetSize()
        {   
            int size = 0;

            FieldInfo[] fields = this.GetType().GetFIElds(BindingFlags.Public | BindingFlags.Instance);

            foreach (FieldInfo field in fIElds )
            {
                if (field.FIEldType.IsArray)
                {
                    size += GetFieldSize(fIEld);
                }
                else if (field.FIEldType == typeof(string))
                {
                    size += GetFieldSize(fIEld)*2;
                }
                else if (field.FIEldType.IsPrimitive)
                {
                    size += Marshal.SizeOf(field.FIEldType);
                }
            }

            return size;
        }

        #endregion

        properties#region propertIEs

        public byte[] ByteArray
        {
            get
            {
                return data;
            }
        }

        #endregion

        virtual and protected methods#region virtual and protected methods

        public virtual void ReadFromStream(BinaryReader reader)
        {
            object[] param = null;

            // Get all public fIElds
            FieldInfo[] fields = this.GetType().GetFIElds(BindingFlags.Public | BindingFlags.Instance);
           
            // Loop through the fIElds
            foreach(FieldInfo field in fIElds)
            {
                // RetrIEve the read method from ReadMethods hashtable
                MethodInfo method = (MethodInfo)MarshallingMethods.ReadMethods[field.FIEldType];

                if (field.FIEldType.IsArray)
                {
                    Type element = field.FIEldType.GetElementType();
                    if (element.IsValueType && element.IsPrimitive)
                    {
                        if ((element == typeof(char)) || element == typeof(byte))
                        {                                                                                
                            param = new object[1];
                            param[0] = GetFieldSize(fIEld);
                            fIEld.SetValue(this, method.Invoke(reader, param));
                        }
                        else // any other value type array
                        {
                            param = new object[2];
                            param[0] = reader;
                            param[1] = GetFieldSize(fIEld);
                            fIEld.SetValue(this, method.Invoke(null, param));
                        }
                    }
                    else // array of sub structures
                    {
                        int size = GetFieldSize(fIEld);
                        method = (MethodInfo)MarshallingMethods.ReadMethods[typeof(CustomMarshaler)];
                        Array objArray = Array.CreateInstance(element, size);
                        for(int i=0;i<size;i++)
                        {
                            objArray.SetValue(Activator.CreateInstance(element), i);
                            method.Invoke(objArray.GetValue(i), new object[]{reader});
                        }
                        fIEld.SetValue(this, objArray);
                    }
                }
                else if (field.FIEldType == typeof(string))
                {   
                    param = new object[2];
                    param[0] = reader;
                    param[1] = GetFieldSize(fIEld);
                    fIEld.SetValue(this, method.Invoke(null, param));
                }
                else if (field.FieldType.IsValueType && field.FIEldType.IsPrimitive)// regular value type
                {
                    fIEld.SetValue(this, method.Invoke(reader, null));
                }
                else //process substructure
                {
                    CustomMarshaler subStruct = (CustomMarshaler)Activator.CreateInstance(field.FIEldType);
                    subStruct.ReadFromStream(reader);
                }
            }
        }

        public virtual void WriteToStream(BinaryWriter writer)
        {
            object[] param = null;

            FieldInfo[] fields = this.GetType().GetFIElds(BindingFlags.Public | BindingFlags.Instance);
           
            foreach(FieldInfo field in fIElds)
            {
                // Check if we have any value
                object value = fIEld.GetValue(this);
               
                MethodInfo method = (MethodInfo)MarshallingMethods.WriteMethods[field.FIEldType];
               
                if (field.FIEldType.IsArray)
                {
                    Type element = field.FIEldType.GetElementType();
                    if (element.IsValueType && element.IsPrimitive)
                    {
                        //method.Invoke(writer, new object[] {value});
                        Array arrObject = (Array)fIEld.GetValue(this);
                        param = new object[2];
                        param[0] = writer;
                        param[1] = arrObject;
                        method.Invoke(null, param);
                    }
                    else
                    {
                        //Get fIEld size
                        int size = GetFieldSize(fIEld);
                        //Get WriteToStream method
                        method = (MethodInfo)MarshallingMethods.WriteMethods[typeof(CustomMarshaler)];
                        Array arrObject = (Array)fIEld.GetValue(this);
                        for(int i=0;i<size;i++)
                        {
                            method.Invoke(arrObject.GetValue(i), new object[]{writer});
                        }
                    }                   
                }
                else if (field.FIEldType == typeof(string))
                {   
                    param = new object[3];
                    param[0] = writer;
                    param[1] = fIEld.GetValue(this);
                    param[2] = GetFieldSize(fIEld);
                    method.Invoke(null, param);
                   

                }
                else if (field.FieldType.IsValueType && field.FIEldType.IsPrimitive)// regular value type
                {
                    method.Invoke(writer, new object[] {value});
                }
            }
        }

        protected int GetFieldSize(FieldInfo fIEld)
        {
            int size = 0;
            CustomMarshalAsAttribute attrib = (CustomMarshalAsAttribute)fIEld.GetCustomAttributes(typeof(CustomMarshalAsAttribute), true)[0];
           
            if (attrib != null)
            {
                if (attrib.SizeFIEld != null)
                {
                    FieldInfo sizeField = this.GetType().GetField(attrib.SizeFIEld);
                    size = (int)sizeFIEld.GetValue(this);
                }
                else
                {
                    size = attrib.SizeConst;   
                }
            }

            return size;
        }

        #endregion

        helper methods#region helper methods

        private static bool CompareByteArrays (byte[] data1, byte[] data2)
        {
            // If both are null, they're equal
            if (data1==null && data2==null)
            {
                return true;
            }
            // If either but not both are null, they're not equal
            if (data1==null || data2==null)
            {
                return false;
            }
            if (data1.Length != data2.Length)
            {
                return false;
            }
            for (int i=0; i < data1.Length; i++)
            {
                if (data1[i] != data2[i])
                {
                    return false;
                }
            }
            return true;
        }

        #endregion

    }

    MarshallingMethods class#region MarshallingMethods class
    /**//// <summary>
    /// MarshallingMethods class implementation.
    /// </summary>
    public class MarshallingMethods
    {
        public static Hashtable ReadMethods = new Hashtable();
        public static Hashtable WriteMethods = new Hashtable();
       
        constructors#region constructors

        static MarshallingMethods()
        {
            // Read Methods
            ReadMethods.Add(typeof(bool), typeof(BinaryReader).GetMethod("ReadBoolean"));
            ReadMethods.Add(typeof(byte), typeof(BinaryReader).GetMethod("ReadByte"));
            ReadMethods.Add(typeof(System.SByte), typeof(BinaryReader).GetMethod("ReadSByte"));
            ReadMethods.Add(typeof(System.Single), typeof(BinaryReader).GetMethod("ReadSingle"));
            ReadMethods.Add(typeof(byte[]), typeof(BinaryReader).GetMethod("ReadBytes"));
            ReadMethods.Add(typeof(char[]), typeof(BinaryReader).GetMethod("ReadChars"));
            ReadMethods.Add(typeof(System.Int16), typeof(BinaryReader).GetMethod("ReadInt16"));
            ReadMethods.Add(typeof(System.Int32), typeof(BinaryReader).GetMethod("ReadInt32"));
            ReadMethods.Add(typeof(System.UInt16), typeof(BinaryReader).GetMethod("ReadUInt16"));
            ReadMethods.Add(typeof(System.UInt32), typeof(BinaryReader).GetMethod("ReadUInt32"));
            ReadMethods.Add(typeof(System.String), typeof(MarshallingMethods).GetMethod("ReadString"));
            ReadMethods.Add(typeof(System.DateTime), typeof(MarshallingMethods).GetMethod("ReadDateTime"));
            ReadMethods.Add(typeof(System.Int16[]), typeof(MarshallingMethods).GetMethod("ReadInt16Array"));
            ReadMethods.Add(typeof(System.Int32[]), typeof(MarshallingMethods).GetMethod("ReadInt32Array"));
            ReadMethods.Add(typeof(System.UInt16[]), typeof(MarshallingMethods).GetMethod("ReadUInt16Array"));
            ReadMethods.Add(typeof(System.UInt32[]), typeof(MarshallingMethods).GetMethod("ReadUInt32Array"));
            ReadMethods.Add(typeof(CustomMarshaler), typeof(CustomMarshaler).GetMethod("ReadFromStream"));
            //Write Methods
            WriteMethods.Add(typeof(bool), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(bool)}));
            WriteMethods.Add(typeof(byte), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(byte)}));
            WriteMethods.Add(typeof(System.SByte), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(System.SByte)}));
            WriteMethods.Add(typeof(System.Single), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(System.Single)}));
            //WriteMethods.Add(typeof(byte[]), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(byte[])}));
            //WriteMethods.Add(typeof(char[]), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(char[])}));
            WriteMethods.Add(typeof(System.Int16), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(System.Int16)}));
            WriteMethods.Add(typeof(System.Int32), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(System.Int32)}));
            WriteMethods.Add(typeof(System.UInt16), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(System.UInt16)}));
            WriteMethods.Add(typeof(System.UInt32), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(System.UInt32)}));
            WriteMethods.Add(typeof(System.String), typeof(MarshallingMethods).GetMethod("WriteString"));
            WriteMethods.Add(typeof(CustomMarshaler), typeof(CustomMarshaler).GetMethod("WriteToStream"));

            WriteMethods.Add(typeof(bool[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(bool[]) }));
            WriteMethods.Add(typeof(char[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(char[]) }));
            WriteMethods.Add(typeof(short[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(short[]) }));
            WriteMethods.Add(typeof(ushort[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(ushort[]) }));
            WriteMethods.Add(typeof(int[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(int[]) }));
            WriteMethods.Add(typeof(uint[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(uint[]) }));
            WriteMethods.Add(typeof(long[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(long[]) }));
            WriteMethods.Add(typeof(ulong[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(ulong[]) }));
            WriteMethods.Add(typeof(float[]), typeof(MarshallingMethods).GetMethod("WriteArray", new Type[] { typeof(BinaryWriter), typeof(float[]) }));

        }

        #endregion

        static helper methods#region static helper methods

        public static short[] ReadInt16Array(BinaryReader reader, int count)
        {
            short[] result = new short[count];

            for(int i=0;i<count;i++)
            {
                result[i] = reader.ReadInt16();
            }
            return result;
        }

        public static int[] ReadInt32Array(BinaryReader reader, int count)
        {
            int[] result = new int[count];

            for(int i=0;i<count;i++)
            {
                result[i] = reader.ReadInt32();
            }
            return result;
        }

        public static ushort[] ReadUInt16Array(BinaryReader reader, int count)
        {
            ushort[] result = new ushort[count];

            for(int i=0;i<count;i++)
            {
                result[i] = reader.ReadUInt16();
            }
            return result;
        }

        public static uint[] ReadUInt32Array(BinaryReader reader, int count)
        {
            uint[] result = new uint[count];

            for(int i=0;i<count;i++)
            {
                result[i] = reader.ReadUInt32();
            }
            return result;
        }

        public static string ReadString(BinaryReader reader, int count)
        {
            string result = "";
            if (count == 0)
            {
                count = 255; //default   
            }
            char[] data = reader.ReadChars(count);

            result = new string(data).TrimEnd('\0');
            return result;
        }

        public static void WriteString(BinaryWriter writer, string value, int size)
        {
            if (value!=null)
            {
                byte[] bstring = System.Text.Encoding.Unicode.GetBytes(value.Substring(0, size));
                writer.Write(bstring);
            }
        }

        public static DateTime ReadDateTime(BinaryReader reader)
        {
            return DateTime.FromFileTime(reader.ReadInt64());
        }


        public static void WriteArray(BinaryWriter writer, bool[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }
        public static void WriteArray(BinaryWriter writer, char[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }

        public static void WriteArray(BinaryWriter writer, byte[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }

        public static void WriteArray(BinaryWriter writer, short[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }
        public static void WriteArray(BinaryWriter writer, ushort[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }
        public static void WriteArray(BinaryWriter writer, int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }
        public static void WriteArray(BinaryWriter writer, uint[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }
        public static void WriteArray(BinaryWriter writer, long[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }
        public static void WriteArray(BinaryWriter writer, ulong[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }
        public static void WriteArray(BinaryWriter writer, float[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                writer.Write(arr[i]);
            }
        }

        public static void WriteSerializers(BinaryWriter writer, CustomMarshaler[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i].WriteToStream(writer);
            }
        }

        #endregion
    }

    #endregion

    CustomMarshalAsAttribute#region CustomMarshalAsAttribute
    /**//// <summary>
    /// CustomMarshalAsAttribute implementaion.
    /// </summary>
    public sealed class CustomMarshalAsAttribute : Attribute
    {
        public int SizeConst = 0;
        public string  SizeFIEld = null;
    }

    #endregion

}

MIB_IFROW.cs
using System;
using System.Collections.Generic;
using , System.Text;

namespace Lemony.SystemInfo
{
    public class MIB_IFROW : CustomMarshaler
    {
        [CustomMarshalAs(SizeConst = MAX_INTERFACE_NAME_LEN)]
        public string wszName;
        public uint dwIndex; // index of the interface
        public uint dwType; // type of interface
        public uint dwMtu; // max transmission unit
        public uint dwSpeed; // speed of the interface
        public uint dwPhysAddrLen; // length of physical address
        [CustomMarshalAs(SizeConst = MAXLEN_PHYSADDR)]
        public byte[] bPhysAddr; // physical address of adapter
        public uint dwAdminStatus; // administrative status
        public uint dwOperStatus; // Operational status
        public uint dwLastChange; // last time Operational status changed
        public uint dwInOctets; // octets received
        public uint dwInUcastPkts; // unicast packets received
        public uint dwInNUcastPkts; // non-unicast packets received
        public uint dwInDiscards; // received packets discarded
        public uint dwInErrors; // erroneous packets received
        public uint dwInUnknownProtos; // unknown protocol packets received
        public uint dwOutOctets; // octets sent
        public uint dwOutUcastPkts; // unicast packets sent
        public uint dwOutNUcastPkts; // non-unicast packets sent
        public uint dwOutDiscards; // outgoing packets discarded
        public uint dwOutErrors; // erroneous packets sent
        public uint dwOutQLen; // output queue length
        public uint dwDescrLen; // length of bDescr member
        [CustomMarshalAs(SizeConst = MAXLEN_IFDESCR)]
        public byte[] bDescr; // interface description        

        private const int MAX_INTERFACE_NAME_LEN = 256;
        private const int MAXLEN_PHYSADDR = 8;
        private const int MAXLEN_IFDESCR = 256;
        private const int MAX_ADAPTER_NAME = 128;
    }
}

MIB_IFTABLE.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace Lemony.SystemInfo
{
    /**//// <summary>
    /// IFTable
    /// </summary>
    public class MIB_IFTABLE : CustomMarshaler
    {
        public int dwNumEntrIEs;
        [CustomMarshalAs(SizeField = "dwNumEntrIEs")]
        public MIB_IFROW[] Table;

        public MIB_IFTABLE()
        {
            this.data = new byte[this.GetSize()];
        }

        public MIB_IFTABLE(int size)
        {
            this.data = new byte[size];
        }
    }
}

再定義一個 NetInfo 類,存儲網絡信息

NetInfo.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace Lemony.SystemInfo
{
    /**//// <summary>
    /// 網絡類型
    /// </summary>
    public enum NetType
    {
        Other = 1,
        Ethernet = 6,
        Tokenring = 9,
        FDDI = 15,
        PPP = 23,
        Loopback = 24,
        Slip = 28
    };

    /**//// <summary>
    /// 網絡狀態
    /// </summary>
    public enum NetState
    {
        NotOperational = 0,
        Operational = 1,
        Disconnected = 2,
        Connecting = 3,
        Connected = 4,
        Unreachable = 5
    };

    /**//// <summary>
    /// 網絡信息類
    /// </summary>
    public class NetInfo
    {
        public NetInfo()
        {
        }

        private string m_Name;
        /**//// <summary>
        /// 名稱
        /// </summary>
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        private uint m_Index;
        /**//// <summary>
        /// 有效編號
        /// </summary>
        public uint Index
        {
            get { return m_Index; }
            set { m_Index = value; }
        }

        private NetType m_Type;
        /**//// <summary>
        /// 類型
        /// </summary>
        public NetType Type
        {
            get { return m_Type; }
            set { m_Type = value; }
        }

        private uint m_Speed;
        /**//// <summary>
        /// 速度
        /// </summary>
        public uint Speed
        {
            get { return m_Speed; }
            set { m_Speed = value; }
        }

        private uint m_InOctets;
        /**//// <summary>
        /// 總接收字節數
        /// </summary>
        public uint InOctets
        {
            get { return m_InOctets; }
            set { m_InOctets = value; }
        }

        private uint m_OutOctets;
        /**//// <summary>
        /// 總發送字節數
        /// </summary>
        public uint OutOctets
        {
            get { return m_OutOctets; }
            set { m_OutOctets = value; }
        }

        private NetState m_Status;
        /**//// <summary>
        /// 操作狀態
        /// </summary>
        public NetState Status
        {
            get { return m_Status; }
            set { m_Status = value; }
        }

        private uint m_InErrors;
        /**//// <summary>
        /// 總錯收字節數
        /// </summary>
        public uint InErrors
        {
            get { return m_InErrors; }
            set { m_InErrors = value; }
        }

        private uint m_OutErrors;
        /**//// <summary>
        /// 總錯發字節數
        /// </summary>
        public uint OutErrors
        {
            get { return m_OutErrors; }
            set { m_OutErrors = value; }
        }

        private uint m_InUnknownProtos;
        /**//// <summary>
        /// 未知協議共收字節數
        /// </summary>
        public uint InUnknownProtos
        {
            get { return m_InUnknownProtos; }
            set { m_InUnknownProtos = value; }
        }

        private string m_PhysAddr;
        /**//// <summary>
        /// 物理地址
        /// </summary>
        public string PhysAddr
        {
            get { return m_PhysAddr; }
            set { m_PhysAddr = value; }
        }

    }
}

OK,現在可以獲取網絡信息了

        /**//// <summary>
        /// Get IFTable
        /// </summary>
        /// <returns>MIB_IFTABLE Class</returns>
        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>
        /// Get NetInfo Class
        /// </summary>
        /// <param name="row">MIB_IFROW Class</param>
        /// <returns>NetInfo Class</returns>
        private static NetInfo GetNetInfo(MIB_IFROW row)
        {
            NetInfo ninfo = new NetInfo();
            ninfo.Index = row.dwIndex;
            ninfo.Name = Encoding.ASCII.GetString(row.bDescr, 0, (int)row.dwDescrLen);
            ninfo.PhysAddr = GetPhysAddr(row.bPhysAddr, (int)row.dwPhysAddrLen);
            ninfo.Type = (NetType)row.dwType;
            ninfo.Status = (NetState)row.dwOperStatus;
            ninfo.Speed = row.dwSpeed;
            ninfo.InErrors = row.dwInErrors;
            ninfo.InOctets = row.dwInOctets;
            ninfo.InUnknownProtos = row.dwInUnknownProtos;
            ninfo.OutErrors = row.dwOutErrors;
            ninfo.OutOctets = row.dwOutOctets;
            return ninfo;
        }
/**//// <summary>
        /// 獲取所有的網絡信息
        /// </summary>
        /// <returns>NetInfo 網絡信息范型</returns>
        public static List<NetInfo> GetAllNetInfo()
        {
            //定義范型
            List<NetInfo> ninfos = new List<NetInfo>();

            //定義,獲取 MIB_IFTABLE 對象
            MIB_IFTABLE tbl = GetAllIfTable();

            //如果成功
            if (tbl != null)
            {
                tbl.Deserialize();
                for (int i = 0; i < tbl.Table.Length; i++)
                {
                    ninfos.Add(GetNetInfo(tbl.Table[i]));
                }
            }

            return ninfos;
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved