在 C# Winforms 应用程序中从命令提示符收集数据的好方法是什么?
这是我作为会员加入 StackOverflow 的第一天,我不得不说我很喜欢这个网站背后的想法!无论如何,问题是:
我正在创建一个 C# Windows 窗体应用程序,用于监视计算机的所有网络统计信息。这可以通过在命令提示符中输入“netstat”来完成,但对于现在的普通计算机用户来说,那个黑色矩形相当令人畏惧,而且输出不会定期更新,而是用户必须重新输入命令才能获得最大的结果。最新信息。
我想要做的是有一个每秒触发的代码,以在命令提示符中使用“netstat -noa”命令,并将数据反馈到 DataGridView 控件中,其中包含“PID”、“外部地址”和“进程状态”。问题是,我从来没有在 GUI 应用程序中使用命令提示符做过太多事情,并且完全不知道如何将命令弹出的文本表以有组织的方式并放入我的 DataGridView 列中。我相信该网站上可能有一篇类似的帖子,但我也相信它可能是基于 Linux 的。
感谢大家的帮助!
编辑:哇,我没想到两个小时后会有这么多输入!你们太棒了!这里的体验比在-shiver-其他网站上的体验要好得多。我真的很期待尝试应用程序的个人 netstat 实现和/或加速 Windows PowerShell。然而,直到明天我才能尝试任何事情,因为我现在所在的地方已经很晚了。我想我们明天会看到结果。大家干杯。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我知道您已经标记了 c#,但是您是否考虑过使用 Powershell?它非常适合从外部盒子请求数据。
建议阅读:
http://blogs.msdn.com/b/spike/archive/2010/02/04/how-to-query-for-netstat-info-using-powershell.aspx
或者, google 搜索:powershell netstat
就 C# 而言,您可能需要 GetActiveTcpConnections():
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getactivetcpconnections(v=VS.80).aspx
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
抓取命令提示符的输出是一种方法 - 这是关于 复制并复制从控制台窗口粘贴
更实质性的方法是构建您自己的 netstat 实现 就像这家伙所做的
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
使用
Process
运行命令并捕获其输出:Use a
Process
to run the command and capture its output:有点黑客,但您可以将 netstat 的输出重定向到文件“netstat -noa > c:\netstat.txt”,然后加载该文件并解析它...
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...
那么未来版本的 Windows 将更改 netstat 的输出格式,并且会破坏您的程序。与其进行“屏幕抓取”黑客攻击,为什么不直接调用实际的 Windows API 来为您提供所需的数据呢?这就是 Windows 管理工具是为了.
如何访问类似 netstat 的以太网来自 Windows 程序的统计信息
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
您可以使用 System.Diagnostics.Process 并将 startinfo 的 RedirectStandardOutput 属性设置为 true。这样您就可以获得 StandardOutput 作为流阅读器。确保将 UseShellExecute 属性设置为 false,将 CreateNoWindow 属性设置为 true。
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.