在C#中调用API获取网络信息和流量

最近一项目中要求显示网络流量,而且必须使用C#。

事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量。只是要在C#中实现还是比较复杂。

先看看怎么定义该 API

None.gif [DllImport( " IpHlpApi.dll " )]
None.gif        
extern   static   public   uint  GetIfTable( byte [] pIfTable,  ref   uint  pdwSize,  bool  bOrder);

本来想把 pIfTable 定义为 IntPtr,但是这样的结果是,获取的信息是错误的(直到现在都不知是什么原因)。

但显然定义为 byte[] 是不能直接使用的。幸好在 Google Code Search 找到了三个类:

ContractedBlock.gif ExpandedBlockStart.gif CustomtMarshaler.cs
None.gifusing System;
None.gif
using System.IO;
None.gif
using System.Collections;
None.gif
using System.Reflection;
None.gif
using System.Runtime.InteropServices;
None.gif
using System.Threading;
None.gif
None.gif
namespace Lemony.SystemInfo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// CustomMarshaler class implementation.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public abstract class CustomMarshaler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Fields#region Fields
InBlock.gif        
// The internal buffer
InBlock.gif
        internal byte[] data;
InBlock.gif        
private MemoryStream stream;
InBlock.gif        
private BinaryReader binReader;
InBlock.gif        
private BinaryWriter binWriter;
InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif    
ContractedSubBlock.gifExpandedSubBlockStart.gif        
constructors#region constructors
InBlock.gif
InBlock.gif        
public CustomMarshaler()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
public methods#region public methods
InBlock.gif
InBlock.gif        
public void Deserialize()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (data != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (binReader != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    binReader.Close();
InBlock.gif                    stream.Close();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// Create a steam from byte array
InBlock.gif
                stream = new MemoryStream(data);
InBlock.gif                binReader 
= new BinaryReader(stream, System.Text.Encoding.Unicode);
InBlock.gif                ReadFromStream(binReader);
InBlock.gif                binReader.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int GetSize()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
int size = 0;
InBlock.gif
InBlock.gif            FieldInfo[] fields 
= this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
InBlock.gif
InBlock.gif            
foreach (FieldInfo field in fields )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (field.FieldType.IsArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    size 
+= GetFieldSize(field);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (field.FieldType == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    size 
+= GetFieldSize(field)*2;
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else if (field.FieldType.IsPrimitive)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    size 
+= Marshal.SizeOf(field.FieldType);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return size;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
properties#region properties
InBlock.gif
InBlock.gif        
public byte[] ByteArray
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return data;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
virtual and protected methods#region virtual and protected methods
InBlock.gif
InBlock.gif        
public virtual void ReadFromStream(BinaryReader reader)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object[] param = null;
InBlock.gif
InBlock.gif            
// Get all public fields
InBlock.gif
            FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
InBlock.gif            
InBlock.gif            
// Loop through the fields
InBlock.gif
            foreach(FieldInfo field in fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Retrieve the read method from ReadMethods hashtable
InBlock.gif
                MethodInfo method = (MethodInfo)MarshallingMethods.ReadMethods[field.FieldType];
InBlock.gif
InBlock.gif                
if (field.FieldType.IsArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Type element 
= field.FieldType.GetElementType();
InBlock.gif                    
if (element.IsValueType && element.IsPrimitive)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if ((element == typeof(char)) || element == typeof(byte))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{                                                                                 
InBlock.gif                            param 
= new object[1];
InBlock.gif                            param[
0= GetFieldSize(field);
InBlock.gif                            field.SetValue(
this, method.Invoke(reader, param)); 
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else // any other value type array
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            param 
= new object[2];
InBlock.gif                            param[
0= reader;
InBlock.gif                            param[
1= GetFieldSize(field);
InBlock.gif                            field.SetValue(
this, method.Invoke(null, param)); 
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else // array of sub structures
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        
int size = GetFieldSize(field);
InBlock.gif                        method 
= (MethodInfo)MarshallingMethods.ReadMethods[typeof(CustomMarshaler)];
InBlock.gif                        Array objArray 
= Array.CreateInstance(element, size);
InBlock.gif                        
for(int i=0;i<size;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            objArray.SetValue(Activator.CreateInstance(element), i);
ExpandedSubBlockStart.gifContractedSubBlock.gif                            method.Invoke(objArray.GetValue(i), 
new object[]dot.gif{reader});
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        field.SetValue(
this, objArray); 
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (field.FieldType == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{    
InBlock.gif                    param 
= new object[2];
InBlock.gif                    param[
0= reader;
InBlock.gif                    param[
1= GetFieldSize(field);
InBlock.gif                    field.SetValue(
this, method.Invoke(null, param)); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (field.FieldType.IsValueType && field.FieldType.IsPrimitive)// regular value type
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    field.SetValue(
this, method.Invoke(reader, null)); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else //process substructure 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    CustomMarshaler subStruct 
= (CustomMarshaler)Activator.CreateInstance(field.FieldType);
InBlock.gif                    subStruct.ReadFromStream(reader);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public virtual void WriteToStream(BinaryWriter writer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object[] param = null;
InBlock.gif
InBlock.gif            FieldInfo[] fields 
= this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
InBlock.gif            
InBlock.gif            
foreach(FieldInfo field in fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Check if we have any value
InBlock.gif
                object value = field.GetValue(this);
InBlock.gif                
InBlock.gif                MethodInfo method 
= (MethodInfo)MarshallingMethods.WriteMethods[field.FieldType];
InBlock.gif                
InBlock.gif                
if (field.FieldType.IsArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Type element 
= field.FieldType.GetElementType();
InBlock.gif                    
if (element.IsValueType && element.IsPrimitive)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//method.Invoke(writer, new object[] {value});
InBlock.gif
                        Array arrObject = (Array)field.GetValue(this);
InBlock.gif                        param 
= new object[2];
InBlock.gif                        param[
0= writer;
InBlock.gif                        param[
1= arrObject;
InBlock.gif                        method.Invoke(
null, param);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//Get field size
InBlock.gif
                        int size = GetFieldSize(field);
InBlock.gif                        
//Get WriteToStream method
InBlock.gif
                        method = (MethodInfo)MarshallingMethods.WriteMethods[typeof(CustomMarshaler)];
InBlock.gif                        Array arrObject 
= (Array)field.GetValue(this);
InBlock.gif                        
for(int i=0;i<size;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                            method.Invoke(arrObject.GetValue(i), 
new object[]dot.gif{writer});
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }
                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (field.FieldType == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{    
InBlock.gif                    param 
= new object[3];
InBlock.gif                    param[
0= writer;
InBlock.gif                    param[
1= field.GetValue(this);
InBlock.gif                    param[
2= GetFieldSize(field);
InBlock.gif                    method.Invoke(
null, param);
InBlock.gif                    
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (field.FieldType.IsValueType && field.FieldType.IsPrimitive)// regular value type
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    method.Invoke(writer, 
new object[] dot.gif{value});
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected int GetFieldSize(FieldInfo field)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int size = 0;
InBlock.gif            CustomMarshalAsAttribute attrib 
= (CustomMarshalAsAttribute)field.GetCustomAttributes(typeof(CustomMarshalAsAttribute), true)[0];
InBlock.gif            
InBlock.gif            
if (attrib != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (attrib.SizeField != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    FieldInfo sizeField 
= this.GetType().GetField(attrib.SizeField);
InBlock.gif                    size 
= (int)sizeField.GetValue(this);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    size 
= attrib.SizeConst;    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return size;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
helper methods#region helper methods
InBlock.gif
InBlock.gif        
private static bool CompareByteArrays (byte[] data1, byte[] data2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// If both are null, they're equal
InBlock.gif
            if (data1==null && data2==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
// If either but not both are null, they're not equal
InBlock.gif
            if (data1==null || data2==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (data1.Length != data2.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
for (int i=0; i < data1.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (data1[i] != data2[i])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
MarshallingMethods class#region MarshallingMethods class
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// MarshallingMethods class implementation.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MarshallingMethods
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static Hashtable ReadMethods = new Hashtable();
InBlock.gif        
public static Hashtable WriteMethods = new Hashtable();
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
constructors#region constructors
InBlock.gif
InBlock.gif        
static MarshallingMethods()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Read Methods
InBlock.gif
            ReadMethods.Add(typeof(bool), typeof(BinaryReader).GetMethod("ReadBoolean"));
InBlock.gif            ReadMethods.Add(
typeof(byte), typeof(BinaryReader).GetMethod("ReadByte"));
InBlock.gif            ReadMethods.Add(
typeof(System.SByte), typeof(BinaryReader).GetMethod("ReadSByte"));
InBlock.gif            ReadMethods.Add(
typeof(System.Single), typeof(BinaryReader).GetMethod("ReadSingle"));
InBlock.gif            ReadMethods.Add(
typeof(byte[]), typeof(BinaryReader).GetMethod("ReadBytes"));
InBlock.gif            ReadMethods.Add(
typeof(char[]), typeof(BinaryReader).GetMethod("ReadChars"));
InBlock.gif            ReadMethods.Add(
typeof(System.Int16), typeof(BinaryReader).GetMethod("ReadInt16"));
InBlock.gif            ReadMethods.Add(
typeof(System.Int32), typeof(BinaryReader).GetMethod("ReadInt32"));
InBlock.gif            ReadMethods.Add(
typeof(System.UInt16), typeof(BinaryReader).GetMethod("ReadUInt16"));
InBlock.gif            ReadMethods.Add(
typeof(System.UInt32), typeof(BinaryReader).GetMethod("ReadUInt32"));
InBlock.gif            ReadMethods.Add(
typeof(System.String), typeof(MarshallingMethods).GetMethod("ReadString"));
InBlock.gif            ReadMethods.Add(
typeof(System.DateTime), typeof(MarshallingMethods).GetMethod("ReadDateTime"));
InBlock.gif            ReadMethods.Add(
typeof(System.Int16[]), typeof(MarshallingMethods).GetMethod("ReadInt16Array"));
InBlock.gif            ReadMethods.Add(
typeof(System.Int32[]), typeof(MarshallingMethods).GetMethod("ReadInt32Array"));
InBlock.gif            ReadMethods.Add(
typeof(System.UInt16[]), typeof(MarshallingMethods).GetMethod("ReadUInt16Array"));
InBlock.gif            ReadMethods.Add(
typeof(System.UInt32[]), typeof(MarshallingMethods).GetMethod("ReadUInt32Array"));
InBlock.gif            ReadMethods.Add(
typeof(CustomMarshaler), typeof(CustomMarshaler).GetMethod("ReadFromStream"));
InBlock.gif            
//Write Methods
ExpandedSubBlockStart.gifContractedSubBlock.gif
            WriteMethods.Add(typeof(bool), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(bool)}));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(byte), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(byte)}));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(System.SByte), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(System.SByte)}));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(System.Single), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(System.Single)}));
InBlock.gif            
//WriteMethods.Add(typeof(byte[]), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(byte[])}));
InBlock.gif            
//WriteMethods.Add(typeof(char[]), typeof(BinaryWriter).GetMethod("Write", new Type[]{typeof(char[])}));
ExpandedSubBlockStart.gifContractedSubBlock.gif
            WriteMethods.Add(typeof(System.Int16), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(System.Int16)}));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(System.Int32), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(System.Int32)}));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(System.UInt16), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(System.UInt16)}));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(System.UInt32), typeof(BinaryWriter).GetMethod("Write"new Type[]dot.gif{typeof(System.UInt32)}));
InBlock.gif            WriteMethods.Add(
typeof(System.String), typeof(MarshallingMethods).GetMethod("WriteString"));
InBlock.gif            WriteMethods.Add(
typeof(CustomMarshaler), typeof(CustomMarshaler).GetMethod("WriteToStream"));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(bool[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(bool[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(char[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(char[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(short[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(short[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(ushort[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(ushort[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(int[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(int[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(uint[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(uint[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(long[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(long[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(ulong[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(ulong[]) }));
ExpandedSubBlockStart.gifContractedSubBlock.gif            WriteMethods.Add(
typeof(float[]), typeof(MarshallingMethods).GetMethod("WriteArray"new Type[] dot.giftypeof(BinaryWriter), typeof(float[]) }));
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
static helper methods#region static helper methods
InBlock.gif
InBlock.gif        
public static short[] ReadInt16Array(BinaryReader reader, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
short[] result = new short[count];
InBlock.gif
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result[i] 
= reader.ReadInt16();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static int[] ReadInt32Array(BinaryReader reader, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int[] result = new int[count];
InBlock.gif
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result[i] 
= reader.ReadInt32();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static ushort[] ReadUInt16Array(BinaryReader reader, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ushort[] result = new ushort[count];
InBlock.gif
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result[i] 
= reader.ReadUInt16();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static uint[] ReadUInt32Array(BinaryReader reader, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
uint[] result = new uint[count];
InBlock.gif
InBlock.gif            
for(int i=0;i<count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result[i] 
= reader.ReadUInt32();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string ReadString(BinaryReader reader, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string result = "";
InBlock.gif            
if (count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                count 
= 255//default    
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
char[] data = reader.ReadChars(count);
InBlock.gif
InBlock.gif            result 
= new string(data).TrimEnd('\0');
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static DateTime ReadDateTime(BinaryReader reader)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return DateTime.FromFileTime(reader.ReadInt64());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static void WriteArray(BinaryWriter writer, bool[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void WriteArray(BinaryWriter writer, char[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void WriteArray(BinaryWriter writer, short[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void WriteArray(BinaryWriter writer, ushort[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void WriteArray(BinaryWriter writer, int[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void WriteArray(BinaryWriter writer, uint[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void WriteArray(BinaryWriter writer, long[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void WriteArray(BinaryWriter writer, ulong[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static void WriteArray(BinaryWriter writer, float[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < arr.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.Write(arr[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
CustomMarshalAsAttribute#region CustomMarshalAsAttribute
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// CustomMarshalAsAttribute implementaion.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public sealed class CustomMarshalAsAttribute : Attribute
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int SizeConst = 0;
InBlock.gif        
public string  SizeField = null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

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

InBlock.gif        
private const int MAX_INTERFACE_NAME_LEN = 256;
InBlock.gif        
private const int MAXLEN_PHYSADDR = 8;
InBlock.gif        
private const int MAXLEN_IFDESCR = 256;
InBlock.gif        
private const int MAX_ADAPTER_NAME = 128;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

ContractedBlock.gif ExpandedBlockStart.gif MIB_IFTABLE.cs
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
namespace Lemony.SystemInfo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// IFTable
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MIB_IFTABLE : CustomMarshaler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int dwNumEntries;
InBlock.gif        [CustomMarshalAs(SizeField 
= "dwNumEntries")]
InBlock.gif        
public MIB_IFROW[] Table;
InBlock.gif
InBlock.gif        
public MIB_IFTABLE()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.data = new byte[this.GetSize()];
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public MIB_IFTABLE(int size)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.data = new byte[size];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

再定义一个 NetInfo 类,存储网络信息

ContractedBlock.gif ExpandedBlockStart.gif NetInfo.cs
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Runtime.InteropServices;
None.gif
None.gif
None.gif
namespace Lemony.SystemInfo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 网络类型
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public enum NetType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Other 
= 1,
InBlock.gif        Ethernet 
= 6,
InBlock.gif        Tokenring 
= 9,
InBlock.gif        FDDI 
= 15,
InBlock.gif        PPP 
= 23,
InBlock.gif        Loopback 
= 24,
InBlock.gif        Slip 
= 28
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 网络状态
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public enum NetState
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        NotOperational 
= 0,
InBlock.gif        Operational 
= 1,
InBlock.gif        Disconnected 
= 2,
InBlock.gif        Connecting 
= 3,
InBlock.gif        Connected 
= 4,
InBlock.gif        Unreachable 
= 5
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 网络信息类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NetInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public NetInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string m_Name;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 名称
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_Name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_Name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private uint m_Index;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 有效编号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint Index
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_Index; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_Index = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private NetType m_Type;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 类型
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public NetType Type
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_Type; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_Type = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private uint m_Speed;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 速度
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint Speed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_Speed; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_Speed = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private uint m_InOctets;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 总接收字节数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint InOctets
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_InOctets; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_InOctets = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private uint m_OutOctets;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 总发送字节数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint OutOctets
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_OutOctets; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_OutOctets = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private NetState m_Status;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 操作状态
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public NetState Status
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_Status; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_Status = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private uint m_InErrors;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 总错收字节数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint InErrors
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_InErrors; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_InErrors = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private uint m_OutErrors;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 总错发字节数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint OutErrors
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_OutErrors; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_OutErrors = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private uint m_InUnknownProtos;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 未知协议共收字节数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public uint InUnknownProtos
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_InUnknownProtos; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_InUnknownProtos = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string m_PhysAddr;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 物理地址
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string PhysAddr
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_PhysAddr; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_PhysAddr = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

OK,现在可以获取网络信息了

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// Get IFTable
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <returns>MIB_IFTABLE Class</returns>

None.gif          private   static  MIB_IFTABLE GetAllIfTable()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//缓冲区大小
InBlock.gif
            uint dwSize = 0;
InBlock.gif
InBlock.gif            
//获取缓冲区大小
InBlock.gif
            uint ret = GetIfTable(nullref dwSize, false);
InBlock.gif            
if (ret == 50)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//此函数仅支持于 win98/nt 系统
InBlock.gif
                return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//定义,获取 MIB_IFTABLE 对象
InBlock.gif
            MIB_IFTABLE tbl = new MIB_IFTABLE((int)dwSize);
InBlock.gif            ret 
= GetIfTable(tbl.ByteArray, ref dwSize, false);
InBlock.gif
InBlock.gif            
//如果不成功
InBlock.gif
            if (ret != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return tbl;
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// Get NetInfo Class
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="row">MIB_IFROW Class</param>
ExpandedBlockEnd.gif        
/// <returns>NetInfo Class</returns>

None.gif          private   static  NetInfo GetNetInfo(MIB_IFROW row)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            NetInfo ninfo 
= new NetInfo();
InBlock.gif            ninfo.Index 
= row.dwIndex;
InBlock.gif            ninfo.Name 
= Encoding.ASCII.GetString(row.bDescr, 0, (int)row.dwDescrLen);
InBlock.gif            ninfo.PhysAddr 
= GetPhysAddr(row.bPhysAddr, (int)row.dwPhysAddrLen);
InBlock.gif            ninfo.Type 
= (NetType)row.dwType;
InBlock.gif            ninfo.Status 
= (NetState)row.dwOperStatus;
InBlock.gif            ninfo.Speed 
= row.dwSpeed;
InBlock.gif            ninfo.InErrors 
= row.dwInErrors;
InBlock.gif            ninfo.InOctets 
= row.dwInOctets;
InBlock.gif            ninfo.InUnknownProtos 
= row.dwInUnknownProtos;
InBlock.gif            ninfo.OutErrors 
= row.dwOutErrors;
InBlock.gif            ninfo.OutOctets 
= row.dwOutOctets;
InBlock.gif            
return ninfo;
ExpandedBlockEnd.gif        }

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 获取所有的网络信息
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <returns>NetInfo 网络信息范型</returns>

None.gif          public   static  List < NetInfo >  GetAllNetInfo()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//定义范型
InBlock.gif
            List<NetInfo> ninfos = new List<NetInfo>();
InBlock.gif
InBlock.gif            
//定义,获取 MIB_IFTABLE 对象
InBlock.gif
            MIB_IFTABLE tbl = GetAllIfTable();
InBlock.gif
InBlock.gif            
//如果成功
InBlock.gif
            if (tbl != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tbl.Deserialize();
InBlock.gif                
for (int i = 0; i < tbl.Table.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ninfos.Add(GetNetInfo(tbl.Table[i]));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return ninfos;
ExpandedBlockEnd.gif        }

PS:事实上,我把获取网络、CPU、内存、磁盘、进程信息等功能封装起来,并做了一个比较完善的 Windows 任务管理器,整理完后发布源码。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值