[Quote=引用 17 楼 cstod 的回复:]
ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach(PropertyData pd in mc.Properties)
{
Console.WriteLine(pd.Name);//这样就知道有哪些可用的属性了
}
[/Quote]
这样得到的属性里Speed,IPAdress等都是是存在的,但为什么这样取出Speed、IPAdress都会抛出空指针异常呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace WMINETTest
{
class Program
{
static void Main(string[] args)
{
getNetWorkFlow();
}
private static void getNetWorkFlow()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
//ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
//
foreach(PropertyData pd in mc.Properties)
{
Console.WriteLine(pd.Name);
}
foreach (ManagementObject mo in moc)
{
string a = mo["Speed"].ToString();
Console.WriteLine(a.ToString());
}
Console.ReadLine();
}
ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach(PropertyData pd in mc.Properties)
{
Console.WriteLine(pd.Name);//这样就知道有哪些可用的属性了
}
ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
string a = mo["Speed"].ToString();
Console.WriteLine(a.ToString());
}
[Quote=引用 2 楼 findcaiyzh 的回复:]
CPU使用率,包括指定进程和整个系统。
有例子代码
Determine CPU usage of current process (C++ and C#)
http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/
[/Quote]
谢谢findcaiyzh的回答。有一定的参考价值。硬盘的也基本解决了。内存与网络使用情况呢?有什么办法吗 ?
CPU使用率,包括指定进程和整个系统。
有例子代码
Determine CPU usage of current process (C++ and C#)
http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/
网上找的,看看有用没
以下的代码的原作是opensource的一个叫sniffer.net的,用vb.net写的,这里只是简单的用c#翻译了一下,暂时还只能监控所有数据包,不能监控某一个进程的数据包,代码如下:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace UpdateTester
{
/**//// <summary>
/// Monitor 的摘要说明。
/// </summary>
public class Monitor
{
public delegate void NewPacketEventHandler(Monitor m, Packet p);
public event NewPacketEventHandler NewPacket;
private Socket m_Monitor;
private IPAddress m_Ip;
private byte[] m_Buffer = new byte[65535];
private const System.Int32 IOC_VENDOR = 0x18000000;
private const int IOC_IN = -2147483648;
private const int SIO_RCVALL = IOC_IN ^ IOC_VENDOR ^ 1;
private const int SECURITY_BUILTIN_DOMAIN_RID = 0x20;
private const int DOMAIN_ALIAS_RID_ADMINS = 0x220;
public System.Net.IPAddress IP
{
get { return m_Ip; }
}
public byte[] Buffer
{
get { return m_Buffer; }
}
public Monitor()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Monitor(IPAddress IpAddress)
{
if (!(Environment.OSVersion.Platform == PlatformID.Win32NT) && Environment.OSVersion.Version.Major<5)
{
throw new NotSupportedException("This program requires Windows 2000, Windows XP or Windows .NET Server!");
}
m_Ip = IpAddress;
}
public void Start()
{
if (m_Monitor==null)
{
try
{
m_Monitor = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
m_Monitor.Bind(new IPEndPoint(IP, 0));
m_Monitor.IOControl(SIO_RCVALL, BitConverter.GetBytes(1), null);
m_Monitor.BeginReceive(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}
catch (Exception e)
{
m_Monitor = null;
throw new SocketException();
}
}
}
public void Stop()
{
if (m_Monitor!=null)
{
m_Monitor.Close();
}
m_Monitor = null;
}
public void OnReceive(System.IAsyncResult ar)
{
try
{
int received = m_Monitor.EndReceive(ar);
if (hosts.Length == 0) { throw new NotSupportedException("This computer does not have non-loopback interfaces installed!");}
for (int i=0; i<hosts.Length; i++)
{
}
m_PacketMonitors = new Monitor[1];
m_Packets = new ArrayList();
m_PacketMonitors[0] = new Monitor(hosts[0]);