Our computers usually connect to many network types: wired network, wireless network, and VPN. Each network type may contain different network resources. In Windows 7, all network resources connected to the user are saved. This article uses the Windows API Code Pack to obtain the attributes and details of these network resources.
Create a new project and introduce WindowsAPICodePack. dll. Open the XAML code and add <TabControl> to store all network resources as tags.
<Grid> <TabControl Margin="11,10,11,11" x:Name="tabControl" /></Grid>
Add using Microsoft. WindowsAPICodePack. Net namespace to C. Write an AddProperty () method and add network attributes and information in the Tab.
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);}
Next, compile a LoadNetworkConnections () method to load network resources stored in Windows 7. The NetworkCollection class can be used to obtain information about all network resources in the local machine. NetworkConnectivityLevels can be used to select three different state modes: All, Connected, and Disconnected. In this example, we select All network resources (All ). Then, the network resources are traversed, and corresponding TabItem labels are created. The network resource attributes are added to the tags through the AddProperty () method created above.
private void LoadNetworkConnections(){ NetworkCollection networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.All); foreach (Network n in networks) { TabItem tabItem = new TabItem(); tabItem.Header = string.Format("Network {0} ({1})", tabControl.Items.Count, n.Name); tabControl.Items.Add(tabItem); StackPanel stackPanel = new StackPanel(); stackPanel.Orientation = Orientation.Vertical; AddProperty("Name: ", n.Name, stackPanel); AddProperty("Description: ", n.Description, stackPanel); AddProperty("Domain type: ", n.DomainType.ToString(), stackPanel); AddProperty("Is connected: ", n.IsConnected.ToString(), stackPanel); AddProperty("Is connected to the internet: ", n.IsConnectedToInternet.ToString(), stackPanel); AddProperty("Network ID: ", n.NetworkId.ToString(), stackPanel); AddProperty("Category: ", n.Category.ToString(), stackPanel); AddProperty("Created time: ", n.CreatedTime.ToString(), stackPanel); AddProperty("Connected time: ", n.ConnectedTime.ToString(), stackPanel); AddProperty("Connectivity: ", n.Connectivity.ToString(), stackPanel); tabItem.Content = stackPanel; }}
Finally, you only need to add the LoadNetworkConnections () method to MainWindow.
public MainWindow(){ InitializeComponent(); LoadNetworkConnections();}
Run the program to view the effect. The detailed information of all network resources on the local machine will be displayed for your reference.
Source code download
NetworkListMgr.zip