class clsNetworkStats { private const long ERROR_SUCCESS = 0L; private ArrayList m_Adapters; private const long MAX_INTERFACE_NAME_LEN = 0x100L; private const long MAXLEN_IFDESCR = 0x100L; private const long MAXLEN_PHYSADDR = 8L; // 构造函数 public clsNetworkStats(): this(true) { } public clsNetworkStats(bool IgnoreLoopBack) { int lRetSize = 0; MIB_IFROW ifrow = new MIB_IFROW(); byte[] buff = new byte[1]; byte val = 0; long ret = GetIfTable(ref val, ref lRetSize, 0); buff = new byte[lRetSize]; ret = GetIfTable(ref buff[0], ref lRetSize, 0); int lRows = buff[0]; this.m_Adapters = new ArrayList(lRows); byte len = (byte)lRows; for (byte i = 1; i <= len; i++) { ifrow = new MIB_IFROW(); ifrow.dwIndex = Convert.ToUInt32(i); ret = GetIfEntry(ref ifrow); IFROW_HELPER ifhelp = this.PrivToPub(ifrow); if (IgnoreLoopBack) { if (ifhelp.Description.IndexOf("Loopback") < 0) { this.m_Adapters.Add(ifhelp); } } else { this.m_Adapters.Add(ifhelp); } } } // 获取本地连接流量 public IFROW_HELPER GetAdapter(int index) { return (IFROW_HELPER)this.m_Adapters[index]; } // 获取本地连接数量 public int Count { get { return this.m_Adapters.Count; } } // 引用 iphlpapi 及申明方法 GetIfEntry [DllImport("iphlpapi")] private static extern int GetIfEntry(ref MIB_IFROW pIfRow); [DllImport("iphlpapi")] private static extern int GetIfTable(ref byte pIfRowTable, ref int pdwSize, int bOrder); [DebuggerStepThrough] private IFROW_HELPER PrivToPub(MIB_IFROW pri) { IFROW_HELPER ifhelp = new IFROW_HELPER(); ifhelp.Name = pri.wszName.Trim(); ifhelp.Index = Convert.ToInt32(pri.dwIndex); ifhelp.Type = Convert.ToInt32(pri.dwType); ifhelp.Mtu = Convert.ToInt32(pri.dwMtu); ifhelp.Speed = Convert.ToInt32(pri.dwSpeed); ifhelp.PhysAddrLen = Convert.ToInt32(pri.dwPhysAddrLen); ifhelp.PhysAddr = Encoding.ASCII.GetString(pri.bPhysAddr); ifhelp.AdminStatus = Convert.ToInt32(pri.dwAdminStatus); ifhelp.OperStatus = Convert.ToInt32(pri.dwOperStatus); ifhelp.LastChange = Convert.ToInt32(pri.dwLastChange); ifhelp.InOctets = Convert.ToInt64(pri.dwInOctets); ifhelp.InUcastPkts = Convert.ToInt32(pri.dwInUcastPkts); ifhelp.InNUcastPkts = Convert.ToInt32(pri.dwInNUcastPkts); ifhelp.InDiscards = Convert.ToInt32(pri.dwInDiscards); ifhelp.InErrors = Convert.ToInt32(pri.dwInErrors); ifhelp.InUnknownProtos = Convert.ToInt32(pri.dwInUnknownProtos); ifhelp.OutOctets = Convert.ToInt32(pri.dwOutOctets); ifhelp.OutUcastPkts = Convert.ToInt32(pri.dwOutUcastPkts); ifhelp.OutNUcastPkts = Convert.ToInt32(pri.dwOutNUcastPkts); ifhelp.OutDiscards = Convert.ToInt32(pri.dwOutDiscards); ifhelp.OutErrors = Convert.ToInt32(pri.dwOutErrors); ifhelp.OutQLen = Convert.ToInt32(pri.dwOutQLen); ifhelp.Description = Encoding.ASCII.GetString(pri.bDescr, 0, Convert.ToInt32(pri.dwDescrLen)); ifhelp.InMegs = this.ToMegs((long)ifhelp.InOctets); ifhelp.OutMegs = this.ToMegs((long)ifhelp.OutOctets); return ifhelp; } [DebuggerStepThrough] private string ToMegs(long lSize) { string sDenominator = " B"; if (lSize > 0x3e8L) { sDenominator = " KB"; lSize = (long)Math.Round((double)(((double)lSize) / 1000.0)); } else if (lSize <= 0x3e8L) { sDenominator = " B"; } return lSize.ToString("###,###") + sDenominator; } // 定义结构, 存放本地连接状态 [StructLayout(LayoutKind.Sequential)] public struct IFROW_HELPER { public string Name; public int Index; public int Type; public int Mtu; public int Speed; public int PhysAddrLen; public string PhysAddr; public int AdminStatus; public int OperStatus; public int LastChange; public Int64 InOctets; public int InUcastPkts; public int InNUcastPkts; public int InDiscards; public int InErrors; public int InUnknownProtos; public int OutOctets; public int OutUcastPkts; public int OutNUcastPkts; public int OutDiscards; public int OutErrors; public int OutQLen; public string Description; public string InMegs; public string OutMegs; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct MIB_IFROW { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)] public string wszName; public uint dwIndex; public uint dwType; public uint dwMtu; public uint dwSpeed; public uint dwPhysAddrLen; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] bPhysAddr; public uint dwAdminStatus; public uint dwOperStatus; public uint dwLastChange; public uint dwInOctets; public uint dwInUcastPkts; public uint dwInNUcastPkts; public uint dwInDiscards; public uint dwInErrors; public uint dwInUnknownProtos; public uint dwOutOctets; public uint dwOutUcastPkts; public uint dwOutNUcastPkts; public uint dwOutDiscards; public uint dwOutErrors; public uint dwOutQLen; public uint dwDescrLen; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)] public byte[] bDescr; } 找到一个,但仍然有问题, 一般情况可以正常获得流量大小,但网卡被禁用再启用后,就返回0。
//代码如下:记得在 引用 里面添加对System.Management.dll的引用 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Management;
using System.Text;
using System.Windows.Forms;namespace Gateway
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public static string GetIPAndDNS()
{
string strIP, strSubnet, strGateway, strDNS;
strIP = "0.0.0.0";
strSubnet = "0.0.0.0";
strGateway = "0.0.0.0";
strDNS = "0.0.0.0";
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
foreach (ManagementObject nic in nics)
{
try
{
if (Convert.ToBoolean(nic["IPEnabled"]) == true)
{ if ((nic["IPAddress"] as String[]).Length > 0 && strIP == "0.0.0.0")
{
strIP = (nic["IPAddress"] as String[])[0];
}
if ((nic["IPSubnet"] as String[]).Length > 0 && strSubnet == "0.0.0.0")
{
strSubnet = (nic["IPSubnet"] as String[])[0];
}
if ((nic["DefaultIPGateway"] as String[]).Length > 0 && strGateway == "0.0.0.0")
{
strGateway = (nic["DefaultIPGateway"] as String[])[0];
}
if ((nic["DNSServerSearchOrder"] as String[]).Length > 0 && strDNS == "0.0.0.0")
{
strDNS = (nic["DNSServerSearchOrder"] as String[])[0];
} //STB.Library.ClassGlobal.WriteIniData("IPSet", "IP1", strIP, @"..\WindowsShell.ini");
//STB.Library.ClassGlobal.WriteIniData("IPSet", "IP2", strSubnet, @"..\WindowsShell.ini");
//STB.Library.ClassGlobal.WriteIniData("IPSet", "IP3", strGateway, @"..\WindowsShell.ini");
//STB.Library.ClassGlobal.WriteIniData("IPSet", "IP4", strDNS, @"..\WindowsShell.ini"); //return strIP + "*" + strSubnet + "*" + strGateway + "*" + strDNS;
}
}
catch (Exception)
{
}
}
}
catch (Exception)
{
}
return "IP地址 "+strIP + "\n" + "子网掩码 " +strSubnet + "\n" + "默认网关 " +strGateway + "\n" + "DNS服务器 " + strDNS;
} private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = GetIPAndDNS();
}
}
先帮你搞定支持里的部分
多在这里面,俺能力有限啊就这么多
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
public bool IsInternetConnected()
{
int i = 0;
return InternetGetConnectedState(out i, 0);
}
http://topic.csdn.net/u/20090710/15/d2fef0c6-74ee-4711-b1b9-7a65103c1a23.html?82928
TO:wuyq11 先谢谢,但是我调了InternetGetConnectedState方法后,i 的为 = 18,不知道这是一个什么值 ,如果是已收到数据包的话,我实际已经收到了 2,502,410。麻烦再指点一下。谢谢。
------------------------------
可以参看win32api 有没有你要的
我去找 win32api 犹如大海捞针, 请熟悉的朋友帮帮忙
我找到一个c++的, 实在看不懂,他引用了一个 Iprtrmib.h
[DllImport("iphlpapi.dll")]
public static extern uint GetIfTable(ref MIB_IFTABLE pIfTable, ref uint pdwSize, bool bOrder);MibIfrow ifrow = new MibIfrow();
MIB_IFTABLE iftable = new MIB_IFTABLE();
uint pd = 0;
uint errorCode = 0;
errorCode = GetIfTable(ref iftable, ref pd, false);
照着网上一段代码写出来了,但是结构中的值全是0,错误代码为122。 实在是不知道什么原因。有人可以帮忙吗?
class clsNetworkStats
{
private const long ERROR_SUCCESS = 0L;
private ArrayList m_Adapters;
private const long MAX_INTERFACE_NAME_LEN = 0x100L;
private const long MAXLEN_IFDESCR = 0x100L;
private const long MAXLEN_PHYSADDR = 8L; // 构造函数
public clsNetworkStats(): this(true)
{
}
public clsNetworkStats(bool IgnoreLoopBack)
{
int lRetSize = 0;
MIB_IFROW ifrow = new MIB_IFROW();
byte[] buff = new byte[1];
byte val = 0;
long ret = GetIfTable(ref val, ref lRetSize, 0);
buff = new byte[lRetSize];
ret = GetIfTable(ref buff[0], ref lRetSize, 0);
int lRows = buff[0];
this.m_Adapters = new ArrayList(lRows);
byte len = (byte)lRows;
for (byte i = 1; i <= len; i++)
{
ifrow = new MIB_IFROW();
ifrow.dwIndex = Convert.ToUInt32(i);
ret = GetIfEntry(ref ifrow);
IFROW_HELPER ifhelp = this.PrivToPub(ifrow);
if (IgnoreLoopBack)
{
if (ifhelp.Description.IndexOf("Loopback") < 0)
{
this.m_Adapters.Add(ifhelp);
}
}
else
{
this.m_Adapters.Add(ifhelp);
}
}
} // 获取本地连接流量
public IFROW_HELPER GetAdapter(int index)
{
return (IFROW_HELPER)this.m_Adapters[index];
} // 获取本地连接数量
public int Count
{
get
{
return this.m_Adapters.Count;
}
} // 引用 iphlpapi 及申明方法 GetIfEntry
[DllImport("iphlpapi")]
private static extern int GetIfEntry(ref MIB_IFROW pIfRow);
[DllImport("iphlpapi")]
private static extern int GetIfTable(ref byte pIfRowTable, ref int pdwSize, int bOrder);
[DebuggerStepThrough]
private IFROW_HELPER PrivToPub(MIB_IFROW pri)
{
IFROW_HELPER ifhelp = new IFROW_HELPER();
ifhelp.Name = pri.wszName.Trim();
ifhelp.Index = Convert.ToInt32(pri.dwIndex);
ifhelp.Type = Convert.ToInt32(pri.dwType);
ifhelp.Mtu = Convert.ToInt32(pri.dwMtu);
ifhelp.Speed = Convert.ToInt32(pri.dwSpeed);
ifhelp.PhysAddrLen = Convert.ToInt32(pri.dwPhysAddrLen);
ifhelp.PhysAddr = Encoding.ASCII.GetString(pri.bPhysAddr);
ifhelp.AdminStatus = Convert.ToInt32(pri.dwAdminStatus);
ifhelp.OperStatus = Convert.ToInt32(pri.dwOperStatus);
ifhelp.LastChange = Convert.ToInt32(pri.dwLastChange);
ifhelp.InOctets = Convert.ToInt64(pri.dwInOctets);
ifhelp.InUcastPkts = Convert.ToInt32(pri.dwInUcastPkts);
ifhelp.InNUcastPkts = Convert.ToInt32(pri.dwInNUcastPkts);
ifhelp.InDiscards = Convert.ToInt32(pri.dwInDiscards);
ifhelp.InErrors = Convert.ToInt32(pri.dwInErrors);
ifhelp.InUnknownProtos = Convert.ToInt32(pri.dwInUnknownProtos);
ifhelp.OutOctets = Convert.ToInt32(pri.dwOutOctets);
ifhelp.OutUcastPkts = Convert.ToInt32(pri.dwOutUcastPkts);
ifhelp.OutNUcastPkts = Convert.ToInt32(pri.dwOutNUcastPkts);
ifhelp.OutDiscards = Convert.ToInt32(pri.dwOutDiscards);
ifhelp.OutErrors = Convert.ToInt32(pri.dwOutErrors);
ifhelp.OutQLen = Convert.ToInt32(pri.dwOutQLen);
ifhelp.Description = Encoding.ASCII.GetString(pri.bDescr, 0, Convert.ToInt32(pri.dwDescrLen));
ifhelp.InMegs = this.ToMegs((long)ifhelp.InOctets);
ifhelp.OutMegs = this.ToMegs((long)ifhelp.OutOctets);
return ifhelp;
} [DebuggerStepThrough]
private string ToMegs(long lSize)
{
string sDenominator = " B";
if (lSize > 0x3e8L)
{
sDenominator = " KB";
lSize = (long)Math.Round((double)(((double)lSize) / 1000.0));
}
else if (lSize <= 0x3e8L)
{
sDenominator = " B";
} return lSize.ToString("###,###") + sDenominator;
} // 定义结构, 存放本地连接状态
[StructLayout(LayoutKind.Sequential)]
public struct IFROW_HELPER
{
public string Name;
public int Index;
public int Type;
public int Mtu;
public int Speed;
public int PhysAddrLen;
public string PhysAddr;
public int AdminStatus;
public int OperStatus;
public int LastChange;
public Int64 InOctets;
public int InUcastPkts;
public int InNUcastPkts;
public int InDiscards;
public int InErrors;
public int InUnknownProtos;
public int OutOctets;
public int OutUcastPkts;
public int OutNUcastPkts;
public int OutDiscards;
public int OutErrors;
public int OutQLen;
public string Description;
public string InMegs;
public string OutMegs;
} [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct MIB_IFROW
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)]
public string wszName;
public uint dwIndex;
public uint dwType;
public uint dwMtu;
public uint dwSpeed;
public uint dwPhysAddrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] bPhysAddr;
public uint dwAdminStatus;
public uint dwOperStatus;
public uint dwLastChange;
public uint dwInOctets;
public uint dwInUcastPkts;
public uint dwInNUcastPkts;
public uint dwInDiscards;
public uint dwInErrors;
public uint dwInUnknownProtos;
public uint dwOutOctets;
public uint dwOutUcastPkts;
public uint dwOutNUcastPkts;
public uint dwOutDiscards;
public uint dwOutErrors;
public uint dwOutQLen;
public uint dwDescrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)]
public byte[] bDescr;
}
找到一个,但仍然有问题, 一般情况可以正常获得流量大小,但网卡被禁用再启用后,就返回0。