Develop network monitor

irfanbhatti

New member
Joined
Nov 29, 2014
Messages
3
Programming Experience
Beginner
i want develop a application similar to windows resource monitor(only network Tab)
i am new please Guide me how i can achieve that goal
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+

irfanbhatti

New member
Joined
Nov 29, 2014
Messages
3
Programming Experience
Beginner
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
 
Top Bottom