Win7网络检测 WindowsAPICodePack

在Windows7操作系统下,支持的网络类型越来越复杂,微软提供了WindowsAPICodePack来进行简化底层开发,

 

我们大家来亲自实践一下关于网络状态开发,基于WindowsAPICodePack

启动VS2010

引入dll文件,添加如下代码即可

private void LoadNetworkConnections()
        {
            try
            {
                NetworkCollection networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.All);
                foreach (Network n in networks)
                {
                    // 创建 tab
                    TabItem tabItem = new TabItem();
                    tabItem.Header = string.Format("Network {0} ({1})", tabControl1.Items.Count, n.Name);
                    tabControl1.Items.Add(tabItem);

                    //
                    StackPanel stackPanel2 = new StackPanel();
                    stackPanel2.Orientation = Orientation.Vertical;

                    // 列举所有信息
                    AddProperty("网络名称: ", n.Name, stackPanel2);
                    AddProperty("网络类型: ", n.Description, stackPanel2);
                    AddProperty("域类型: ", n.DomainType.ToString(), stackPanel2);
                    AddProperty("是否连接: ", n.IsConnected.ToString(), stackPanel2);
                    AddProperty("是否上网: ", n.IsConnectedToInternet.ToString(), stackPanel2);
                    AddProperty("网络 ID: ", n.NetworkId.ToString(), stackPanel2);
                    AddProperty("类别: ", n.Category.ToString(), stackPanel2);
                    AddProperty("创建时间: ", n.CreatedTime.ToString(), stackPanel2);
                    AddProperty("连接时间: ", n.ConnectedTime.ToString(), stackPanel2);
                    AddProperty("连接: ", n.Connectivity.ToString(), stackPanel2);

                    //
                    StringBuilder s = new StringBuilder();
                    s.AppendLine("网络连接:");
                    NetworkConnectionCollection connections = n.Connections;
                    string hh = System.Environment.NewLine;
                    foreach (NetworkConnection nc in connections)
                    {
                        s.AppendFormat(hh + "连接 ID: {0}" + hh + "类型: {1}" + hh + "是否连接: {2}" + hh + "是否连接因特网: {3}" + hh,
                            nc.ConnectionId, nc.DomainType, nc.IsConnected, nc.IsConnectedToInternet);
                        s.AppendFormat(hh + "适配器 ID: {0}" + hh + "连接: {1}" + hh,
                            nc.AdapterId, nc.Connectivity);
                    }
                    s.AppendLine();

                    Label label = new Label();
                    label.Content = s.ToString();

                    stackPanel2.Children.Add(label);
                    tabItem.Content = stackPanel2;
                }
            }
            catch (Exception ex)
            {
                txtReadWrite.WriteLog("检测网络异常!" + ex.GetType().FullName + ex.Message + ex.StackTrace);
                (new Win_Popup("消息", "删除数据时出错!" + ex.Message, MessageBoxButton.OK)).ShowDialog();
            }
        }

        private void AddProperty(string propertyName, string propertyValue, StackPanel parent)
        {
            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;

            Label propertyNameLabel = new Label();
            propertyNameLabel.Content = propertyName;
            panel.Children.Add(propertyNameLabel);

            Label propertyValueLabel = new Label();
            propertyValueLabel.Content = propertyValue;
            panel.Children.Add(propertyValueLabel);

            parent.Children.Add(panel);
        }

窗口代码如下:

 <Grid>
        <TabControl Height="Auto" HorizontalAlignment="Stretch" Margin="10,10,10,10" Name="tabControl1" VerticalAlignment="Stretch" Width="Auto">
        </TabControl>
    </Grid>

看下效果图吧:

Win7网络检测 WindowsAPICodePack

Win7网络检测 WindowsAPICodePack

通过这个接口,可以获取各个网络的信息。dll下载点这

 

Win7网络检测 WindowsAPICodePack

原文:http://www.cnblogs.com/lipanpan/p/3948796.html

上一篇:创建一个超级简单的UDP服务器 / 客户端


下一篇:题解 P2505 [HAOI2012]道路