Jack
Jack

Reputation: 16724

how to get default network connection name used via C#?

I need get it independent if the network connection is active or not. I need get only dial up connection.

in this picture Claro is default netowork connection name.

enter image description here

Have no idea how do this. I hope this is clear. Thanks in advance!

Upvotes: 1

Views: 3334

Answers (2)

bobby6k34
bobby6k34

Reputation: 11

is this what you where looking for?

  using System.Net.NetworkInformation;

    class Program
    {
        static void Main(string[] args)
        {
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {

                Console.WriteLine(nic.NetworkInterfaceType);
                Console.WriteLine(nic.Name);
            }
        }
    }

if you find out what string it puts for NetworkInterfaceType with modems then you can put a if statement in there

Upvotes: 0

competent_tech
competent_tech

Reputation: 44931

To find the currently selected default connection for connecting to the internet, which can be set in a couple of ways, you need to read the registry key HKCU\RemoteAccess InternetProfile. This will contain the name of the adapter.

Now the fun part: you will need to use DotRas.

Once you have this downloaded, installed, and reference in your project, you can use code similar to the following:

// Get the default adapter
string defaultAdapter = Registry.GetValue(@"HKEY_CURRENT_USER\RemoteAccess", "InternetProfile", "") as string;

foreach (RasConnection connection in RasConnection.GetActiveConnections())
{
    if (connection.EntryName.Equals(defaultAdapter, StringComparison.InvariantCultureIgnoreCase))
    {
        if (connection.GetConnectionStatus().ConnectionState == RasConnectionState.Connected)
        {
             // Do something
        }
    }
    // Done searching
    break;
}

Upvotes: 2

Related Questions