views:

612

answers:

4

there are 2 problems with the following code:

1- it gets bandwidth used by all network cards...

2- it gets all bandwidth used....

I wanna be able to change it to do the following:

1- dynamically use the network card I am using to connect to the internet.

2- to show only the bandwidth used by the current application

    private void InitializeNetworkInterfaces()
    {
        PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
        InstanceDataCollectionCollection data = performanceCounterCategory.ReadCategory();
        interfaces = performanceCounterCategory.GetInstanceNames();
        int length = interfaces.Length;
        if (length > 0)
        {
            bandwidthTimer.Enabled = true;
            dataSentCounters = new PerformanceCounter[length];
            dataReceivedCounters = new PerformanceCounter[length];
        }
        for (int i = 0; i < length; i++)
        {
            dataReceivedCounters[i] = new PerformanceCounter("Network Interface", "Bytes Sent/sec", interfaces[i]);
            dataSentCounters[i] = new PerformanceCounter("Network Interface", "Bytes Sent/sec", interfaces[i]);
        }
    }

    private void bandwidthTimer_Tick(object sender, EventArgs e)
    {
        int length = interfaces.Length;
        float sendSum = 0;
        float receiveSum = 0;
        try
        {
            for (int i = 0; i < length; i++)
            {
                sendSum += dataSentCounters[i].NextValue();
                receiveSum += dataReceivedCounters[i].NextValue();
            }
        }
        catch (Exception) {
            bandwidthTimer.Stop();
        }
        decimal used =(decimal) (sendSum + receiveSum)*8/1000;
        bandwidthUsed.Text = (decimal.Round(used, 1)).ToString();
    }
+1  A: 

If I were you I'd take a different approach to do that...

System.Net.NetworkInformation namespace provides access to network traffic data, in details, for example, IPGlobalStatistics class provides statistical data about IP (Internte Protocol), which includes both IPv4 and IPv6, there're also other classes, like TcpStatistics, UdpStatistics, ..etc.

Each class has members like BytesSent, BytesReceived and so on, which should provides you with the information you need.

Moayad Mardini
it does give a complete statistics, but it's still not clear how to get the bandwidth used by my exe EXCLUDING any other process that uses the same network interface card
Mustafa A. Jabbar
A: 

Win32_PerfFormattedData_Tcpip_NetworkInterface.CurrentBandwidth

Sheng Jiang 蒋晟
this is Netword Interface Card oriented.. NOT application oriented... i.e. I wanna know the bandwidth used via a WCF service object I am creating and using at runtime !! THAT's all
Mustafa A. Jabbar
A: 

I don't know of a way of using Windows performance counters to track per application bandwidth usage. However, there are plenty of third-party bandwidth monitoring tools that support per-application network usage statistics, NetLimiter being one of them. From their site: "It includes real-time traffic measurement and long-term per-application internet traffic statistics". Hope this helps.

tbreffni