Windows Phone 7 检查手机网络

简介:

在空间Microsoft.Phone.Net.NetworkInformation下的NetworkInterface类 和NetworkInterfaceType类是Windows Phone 7手机网络信息相关的类。NetworkInterface提供了当前手机网络的一些信息,NetworkInterfaceType是一个手机网络的类型的枚举。
下面用一个小例子来使用NetworkInterface和NetworkInterfaceType来检查手机的网络状态。

 

 

 


 
 
  1.  
  2. <Grid   x:Name="LayoutRoot" Background="Transparent"> 
  3.  
  4.         <Grid.RowDefinitions> 
  5.  
  6.             <RowDefinition   Height="Auto" /> 
  7.  
  8.             <RowDefinition     Height="*" /> 
  9.  
  10.         </Grid.RowDefinitions> 
  11.  
  12.         <StackPanel  x:Name="TitlePanel"  Grid.Row="0"  Margin="12,17,0,28"> 
  13.  
  14.             <TextBlock  x:Name="PageTitle"  Text="检查手机网络"   Margin="9,-7,0,0"   Style="{StaticResource PhoneTextTitle1Style}" /> 
  15.  
  16.         </StackPanel> 
  17.  
  18.         <Grid   x:Name="ContentPanel"   Grid.Row="1"   Margin="12,0,12,0"> 
  19.  
  20.             <TextBox  Name="Message"   Background="Yellow"    Text="Unknown"   VerticalAlignment="Center"    HorizontalAlignment="Center" Margin="131,184,94,378" Width="231" /> 
  21.  
  22.             <Button Content="查看网络信息" Height="72" HorizontalAlignment="Left" Margin="103,6,0,0" Name="button1" VerticalAlignment="Top" Width="244" Click="button1_Click" /> 
  23.  
  24.             <TextBox Height="72" HorizontalAlignment="Left" Margin="131,271,0,0" Name="netname" Text="TextBox" VerticalAlignment="Top" Width="244" /> 
  25.  
  26.             <TextBlock Height="49" HorizontalAlignment="Left" Margin="9,198,0,0" Name="textBlock1" Text="网络状态:" VerticalAlignment="Top" Width="121" /> 
  27.  
  28.             <TextBlock HorizontalAlignment="Left" Margin="9,287,0,310" Name="textBlock2" Text="网络类型:" /> 
  29.  
  30.         </Grid> 
  31.  
  32.     </Grid> 

 


 
 
  1.  
  2. using System;  
  3.  
  4. using System.Collections.Generic;  
  5.  
  6. using System.Linq;  
  7.  
  8. using System.Net;  
  9.  
  10. using System.Windows;  
  11.  
  12. using System.Windows.Controls;  
  13.  
  14. using System.Windows.Documents;  
  15.  
  16. using System.Windows.Input;  
  17.  
  18. using System.Windows.Media;  
  19.  
  20. using System.Windows.Media.Animation;  
  21.  
  22. using System.Windows.Shapes;  
  23.  
  24. using Microsoft.Phone.Controls;  
  25.  
  26. using System.Net.NetworkInformation;  
  27.  
  28. using Microsoft.Phone.Net.NetworkInformation;   
  29.  
  30.     
  31.  
  32. namespace checknet  
  33.  
  34. {  
  35.  
  36.     public partial class MainPage : PhoneApplicationPage  
  37.  
  38.     {  
  39.  
  40.         private bool networkIsAvailable;  
  41.  
  42.         private NetworkInterfaceType _currentNetworkType; //网络连接的类型  
  43.  
  44.    
  45.  
  46.         public MainPage()  
  47.  
  48.         {  
  49.  
  50.             InitializeComponent();  
  51.  
  52.         }  
  53.  
  54.    
  55.  
  56.         private void button1_Click(object sender, RoutedEventArgs e)  
  57.  
  58.         {  
  59.  
  60.             networkIsAvailable = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();//当前网络是否可用  
  61.  
  62.             _currentNetworkType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType;//获取当前网络的类型  
  63.  
  64.    
  65.  
  66.             if (networkIsAvailable)  
  67.  
  68.             {  
  69.  
  70.                 Message.Text = "联网状态";  
  71.  
  72.                 Message.Background = new SolidColorBrush(Colors.Green);  
  73.  
  74.             }  
  75.  
  76.             else  
  77.  
  78.             {  
  79.  
  80.                 Message.Text = "断网状态";  
  81.  
  82.                 Message.Background = new SolidColorBrush(Colors.Red);  
  83.  
  84.             }   
  85.  
  86.     
  87.  
  88.             switch (_currentNetworkType)  
  89.  
  90.             {  
  91.  
  92.                 case NetworkInterfaceType.MobileBroadbandCdma:  
  93.  
  94.                     netname.Text = "Cdma网络";  
  95.  
  96.                     break;  
  97.  
  98.                 case NetworkInterfaceType.MobileBroadbandGsm:  
  99.  
  100.                     netname.Text = "Csm网络";  
  101.  
  102.                     break;  
  103.  
  104.                 case NetworkInterfaceType.Wireless80211:  
  105.  
  106.                     netname.Text = "Wireless网络";  
  107.  
  108.                     break;  
  109.  
  110.                 case NetworkInterfaceType.Ethernet:  
  111.  
  112.                     netname.Text = "Ethernet网络";  
  113.  
  114.                     break;  
  115.  
  116.                 case NetworkInterfaceType.None:  
  117.  
  118.                     netname.Text = "网络不可用";  
  119.  
  120.                     break;  
  121.  
  122.                 default:  
  123.  
  124.                     netname.Text = "其他的网络";  
  125.  
  126.                     break;  
  127.  
  128.             }         
  129.  
  130.         }  
  131.  
  132.     }  
  133.  

 本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078644



相关文章
|
5月前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
162 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
8月前
|
监控 安全 网络协议
恶意软件无处逃!国内版“Manus”AiPy开发Windows沙箱工具,进程行为+网络传输层级监控! 头像 豪气的
NImplant.exe 是一款后渗透测试工具,可实现远程管理与持久化控制。其优点包括无文件技术、加密通信和插件扩展,但也存在被检测风险及配置复杂等问题。为深入分析其行为,我们基于 aipy 开发了 Windows 沙箱工具,针对桌面上的 NImplant.exe 进行多维度分析,涵盖进程行为、网络连接(如 TCP 请求、目标 IP/域名)、文件控制等,并生成传输层监控报告与沙箱截图。结果显示,aipy 工具响应迅速,报告清晰易读,满足分析需求。
|
8月前
|
网络协议 安全 测试技术
Windows为何在高速网络环境频繁“失速”?
本文深入剖析了企业在高速网络环境中,因Windows系统限制导致传输速率下降的问题,包括接收缓冲区、安全软件及老旧设备等因素,并提供四步定位法及优化方案,助力企业突破传输瓶颈,提升效率。
|
9月前
|
安全 Ubuntu Linux
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
352 0
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
|
10月前
|
XML 安全 网络安全
Nipper 3.7.0 Windows x64 - 网络设备漏洞评估
Nipper 3.7.0 Windows x64 - 网络设备漏洞评估
235 0
Nipper 3.7.0 Windows x64 - 网络设备漏洞评估
|
11月前
|
安全 网络协议 网络安全
当虚拟机出现网络连接问题时,应该先检查Hyper-V的网卡连接配置
当虚拟机出现网络连接问题时,应首先检查Hyper-V的网卡配置。具体步骤包括:确认虚拟机运行状态、检查虚拟交换机类型和物理网卡连接、确保虚拟机网络适配器正确连接到虚拟交换机,并验证网络配置(IP地址等)。常见问题如虚拟交换机配置错误、网络适配器未连接或防火墙阻止连接,可通过重新配置或调整设置解决。必要时重启虚拟机和宿主机,查看事件日志或联系技术支持以进一步排查问题。
|
安全 Windows
【Azure Cloud Service】在Windows系统中抓取网络包 ( 不需要另外安全抓包工具)
通常,在生产环境中,为了保证系统环境的安全和纯粹,是不建议安装其它软件或排查工具(如果可以安装,也是需要走审批流程)。 本文将介绍一种,不用安装Wireshark / tcpdump 等工具,使用Windows系统自带的 netsh trace 命令来获取网络包的步骤
348 32
|
运维 监控 网络协议
|
Android开发 数据安全/隐私保护 虚拟化
安卓手机远程连接登录Windows服务器教程
安卓手机远程连接登录Windows服务器教程
3154 5
|
Docker 容器
docker中检查容器的网络模式
【10月更文挑战第5天】
1462 1