3

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.

Jared
  • 4,240
  • 4
  • 22
  • 27

6 Answers6

3

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

Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
  • To be honest with you, PowerShell was never something I dipped my toes into, but now that you mention it, I think I should if it would be a helpful tool. Thanks for the idea! – Jared Apr 15 '11 at 03:37
1

scraping the output of a command prompt is one approach - here's a stackoverflow post about copy & pasting from a console window

a more substantial approach would be build your own netstat implementation like this guy did

Community
  • 1
  • 1
Jason
  • 15,915
  • 3
  • 48
  • 72
  • I really like having my applications to be self-substantial and not have the need to rely on many outside sources, so the idea of building my own netstat implementation sounds like a great one. However, I can't really read the code right now because I'm away from my computer currently and the code is getting cut off of my iPhone's browser screen for some dumb reason. I'm planning on trying out your idea tomorrow and seeing if it works. Thanks! – Jared Apr 15 '11 at 03:43
0

Use a Process to run the command and capture its output:

Community
  • 1
  • 1
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
0

A bit of a hack, but you could redirect the output of netstat to a file "netstat -noa > c:\netstat.txt" and then load the file and parse it...

Matt
  • 2,984
  • 1
  • 24
  • 31
0

Then a future version of Windows will change the format of netstat's output and it'll break your program. Instead of doing a "screenscraping" hack why don't you just call actual Windows APIs that will provide you the data that you are looking for? This is what Windows Managmenent Instrumentation is for.

How can I access netstat-like Ethernet statistics from a Windows program

Community
  • 1
  • 1
Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
0

You can use System.Diagnostics.Process and set the RedirectStandardOutput property of startinfo to true. This way you will be able to get the StandardOutput as streamreader. Make sure to set the UseShellExecute property to false and the CreateNoWindow property to true.

Subhash Lama
  • 433
  • 2
  • 12