Get network category name (private/public/domain) from WPF with C# (.NET 7)

Andrew Jones 21 Reputation points
2023-02-06T19:52:42.34+00:00

In PowerShell I can use Get-NetConnectionProfile to get the NetworkCategory (private/public/domain). How can I do this from C# from a WPF app. I can see there are some examples using NetworkInformation.GetConnectionProfiles(); but that appears to be for UWP apps only.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,310 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2023-02-07T03:23:16.4666667+00:00

    @Andrew Jones, Welcome to Microsoft Q&A, you could try the following code to get category name from wpf.

    First of all, please ensure that you add the reference Network List Manager 1.0 Type Library from COM option.

    User's image

    Code:

    private void button1_Click(object sender, RoutedEventArgs e)
            {
    
                var manager = new NetworkListManager();
                var connectedNetworks = manager.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_ALL).Cast<INetwork>();
                foreach (var network in connectedNetworks)
                {
                    listbox1.Items.Add(network.GetCategory().ToString());
                }
            }
    
    

    Tested result:

    User's image

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful