Try downloading a tool like BitMeter 2
] to manage your bandwidth.
Or if you still want to write your own, I suggest using the following WMI query
There are three columns that you will be interested in.
--> How much data has come in on an adapter
--> How much data your adapter has sent
--> Total bandwidth sent and received.
Note that these numbers will be reset when your computer is restarted, so you'll have to keep track of them between boots. I'll let you write the code that uses the WMI, but that should put you in the right place!