Windows 7 network resource management

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.