当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 关于网络连接状态的编程

ASP.NET
VS 2008和.NET 3.5 Beta2新特性介绍
VS 2008和.NET 3.5 Beta2常见问题的解决方案
Asp.net 备份和还原SQL Server及压缩Access数据库
Asp.Net中动态页面转静态页面
ASP.NET缓存:方法分析和实践示例
ASP.NET Forms验证(自定义、角色提供程序)
ASP.NET 2.0当中的Call Back机制
ASP.NET中MD5和SHA1加密的几种方法
在ASP.NET Atlas中调用Web Service
Cast的妙用:泛用LINQ 語句
ASP.NET将物件序列化成Binary储存至DB or File
使用Ajax后,原来导出功能失败的解决方法
装箱、转型、方法调用他们究竟有什么区别?
ASP.NET MVC :实现我们自己的视图引擎
如何构造一个C#语言的爬虫程序
Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
校内网API的.net版本XiaoNei.Net 1.0(非官方)
使用ExtJS GridPanel从Web Service 获取、绑定和显示数据
从UI->DB一条龙到代码生成到EOS,谈谈快速开发
ASP.Net安装简明手册

ASP.NET 中的 关于网络连接状态的编程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 102 ::
收藏到网摘: n/a

1. 可以用WMI (Win2K & XP):(首先要在VS.NET中创建一个项目,然后在添加引用中引用一个.net的装配件:System.Management.dll,这样你的项目才能使用WMI)with Windows 2000 & XP, UNDOCUMENTED: MSNdis_MediaConnectStatus - NdisMediaConnectStatus : (0 = connected?, 1 = not connected?)with Windows XP: Win32_NetworkAdapter - NetConnectionStatus : using System.Management;static void ReportConnection2000(){ ManagementClass mc = new ManagementClass( @"root\WMI", @"MSNdis_MediaConnectStatus", null ); ManagementObjectCollection moc = mc.GetInstances(); foreach( ManagementObject mo in moc ) { string name = (string) mo["InstanceName"]; bool active = (bool) mo["Active"]; uint status = (uint) mo["NdisMediaConnectStatus"]; Console.WriteLine( " {0}\n\tActive:{1} Media Status:{2}", name, active, status ); }}static void ReportConnectionXP(){ ManagementClass mc = new ManagementClass( @"Win32_NetworkAdapter" ); ManagementObjectCollection moc = mc.GetInstances(); foreach( ManagementObject mo in moc ) { string name = (string) mo["Name"]; object val = mo["NetConnectionStatus"]; if( val != null ) Console.WriteLine( " {0}\n\tConnection Status:{1}", name, (ushort) val ); else Console.WriteLine( " {0}\n", name ); }}如果返回0则表示连接;如果返回1则表示没连接;上面得到的是本地的连接状态;==========================================================利用ping来得取: Process p = new Process(); p.StartInfo .WorkingDirectory ="c:\\"; p.StartInfo.FileName = "ping.exe"; p.StartInfo.Arguments="192.168.52.31"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start (); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit (); MessageBox.Show(output);//得到ping的输出值