Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be a good way to collect data from the command prompt in a C# Winforms Application?

This is my first day on StackOverflow as a member, and I have to say I've enjoyed how well the idea behind this website works! Anyway, the question:

I am creating a C# Windows Forms Application that keeps an eye on all of the network statistics of a computer. This can be done by entering "netstat" into the command prompt, but to the normal computer user these days, that black rectangle is rather daunting, and the output does not update regularly, instead the user has to retype the command to get the most up-to-date information.

What I am wanting to do is have a code that is triggered every second to use a "netstat -noa" command in the command prompt and feed the data back into a DataGridView control with the columns "PID", "Foreign Address", and "Process State". The problem is, I've never done much using the command prompt in a GUI App and have absolutely no clue as to how to get the text table that pops up to the command into an organized fashion and into my DataGridView columns. I believe there may have been one similar posting on the site, but I also believe that it may have been Linux based.

Thanks for any help everyone!

Edit: Wow, I wasn't expecting so much input after two hours! You guys are great! It's been a lot better of an experience here than it has been on -shiver- other sites. I'm really looking forward to trying out the personal netstat implementation for the application and/or revving up Windows PowerShell. However, I'm not going to be able to attempt anything until tomorrow because it's getting late where's I'm at. I suppose we'll see what works out tomorrow. Cheers everyone.

like image 935
Jared Avatar asked Jun 11 '26 10:06

Jared


1 Answers

I know you've tagged c#, but have you considered using Powershell at all? It's pretty much perfect for requesting data back from foreign boxes.

Suggested reading: http://blogs.msdn.com/b/spike/archive/2010/02/04/how-to-query-for-netstat-info-using-powershell.aspx

Alternatively, google for: powershell netstat


In terms of C#, you probably want GetActiveTcpConnections():

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getactivetcpconnections(v=VS.80).aspx

like image 63
Lynn Crumbling Avatar answered Jun 13 '26 23:06

Lynn Crumbling