class Program
{
static void Main(string[] args)
{
foreach(Process pss in Process.GetProcesses())
{
NetworkTraffic trafficMonitor = new NetworkTraffic(pss.Id);
string totalBandwidthConsumption;
string lastAmountOfBytesReceived;
float currentAmountOfBytesReceived = trafficMonitor.GetBytesReceived();
float currentAmountOfBytesSent = trafficMonitor.GetBytesSent();
totalBandwidthConsumption = string.Format("Total Bandwidth Consumption: {0} kb", (currentAmountOfBytesReceived + currentAmountOfBytesSent) / 1024);
Console.WriteLine("Process Name:+"+pss.ProcessName.ToString());
Console.WriteLine("Bytes Sent:+"+currentAmountOfBytesSent);
Console.WriteLine("Bytes Receive:+"+currentAmountOfBytesReceived);
Console.WriteLine("Total Bytes:+"+totalBandwidthConsumption);
}
}
}
public class NetworkTraffic
{
private PerformanceCounter bytesSentPerformanceCounter;
private PerformanceCounter bytesReceivedPerformanceCounter;
private int pid;
private bool countersInitialized;
public NetworkTraffic(int processID)
{
pid = processID;
TryToInitializeCounters();
}
private void TryToInitializeCounters()
{
if (!countersInitialized)
{
PerformanceCounterCategory category = new PerformanceCounterCategory(".NET CLR Networking 4.0.0.0");
var instanceNames = category.GetInstanceNames().Where(i => i.Contains(string.Format("p{0}", pid)));
if (instanceNames.Any())
{
bytesSentPerformanceCounter = new PerformanceCounter();
bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
bytesSentPerformanceCounter.CounterName = "Bytes Sent";
bytesSentPerformanceCounter.InstanceName = instanceNames.First();
bytesSentPerformanceCounter.ReadOnly = true;
bytesReceivedPerformanceCounter = new PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
bytesReceivedPerformanceCounter.InstanceName = instanceNames.First();
bytesReceivedPerformanceCounter.ReadOnly = true;
countersInitialized = true;
}
}
}
public float GetBytesSent()
{
float bytesSent = 0;
try
{
TryToInitializeCounters();
bytesSent = bytesSentPerformanceCounter.RawValue;
}
catch { }
return bytesSent;
}
public float GetBytesReceived()
{
float bytesSent = 0;
try
{
TryToInitializeCounters();
bytesSent = bytesReceivedPerformanceCounter.RawValue;
}
catch { }
return bytesSent;
}
}
i am getting process names but not sent and receive packets
i.e
Process Name:+smss
Bytes Sent:+0
Bytes Receive:+0
Total Bytes:+Total Bandwidth Consumption: 0 kb
Process Name:+explorer
Bytes Sent:+0
Bytes Receive:+0
Total Bytes:+Total Bandwidth Consumption: 0 kb
Process Name:+System
Bytes Sent:+0
Bytes Receive:+0
Does anyone have any Idea????? please guide