简体   繁体   中英

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.

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

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

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

有点破解,但你可以将netstat的输出重定向到文件“netstat -noa> c:\\ netstat.txt”,然后加载文件并解析它......

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM