Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614948
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:46:14+00:00 2026-05-22T01:46:14+00:00

I want to programmatically select the network card that is connected to the Internet.

  • 0

I want to programmatically select the network card that is connected to the Internet. I need this to monitor how much traffic is going through the card.
This is what I use to get the instance names

var category = new PerformanceCounterCategory("Network Interface");
String[] instancenames = category.GetInstanceNames();

And this how instancenames looks on my machine

[0]    "6TO4 Adapter"    
[1]    "Internal"    
[2]    "isatap.{385049D5-5293-4E76-A072-9F7A15561418}"    
[3]    "Marvell Yukon 88E8056 PCI-E Gigabit Ethernet Controller"    
[4]    "isatap.{0CB9C3D2-0989-403A-B773-969229ED5074}"    
[5]    "Local Area Connection - Virtual Network"    
[6]    "Teredo Tunneling Pseudo-Interface"

I want the solution to be robust and work on other PCs, I would also prefer .NET. I found other solutions, but they seem to be more complicated for the purpose

  1. Use C++ or WMI
  2. Parse output of netstat

Is there anything else?

See that I already mentioned some available solutions. I am asking if there is anything else, more simple and robust (i.e. NOT C++, WMI or parsing console application output)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:46:15+00:00Added an answer on May 22, 2026 at 1:46 am

    I end up useing WMI and external function call, didn’t find any better solution. Here is my code if anybody is interested.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Management;
    using System.Net;
    using System.Net.Sockets;
    using System.Runtime.InteropServices;
    
    using log4net;
    
    namespace Networking
    {
        internal class NetworkCardLocator
        {
            [DllImport("iphlpapi.dll", CharSet = CharSet.Auto)]
            public static extern int GetBestInterface(UInt32 DestAddr, out UInt32 BestIfIndex);
    
            private static ILog log = OperationLogger.Instance();
    
            /// <summary>
            /// Selectes the name of the connection that is used to access internet or LAN
            /// </summary>
            /// <returns></returns>
            internal static string GetConnectedCardName()
            {
                uint idx = GetConnectedCardIndex();
                string query = String.Format("SELECT * FROM Win32_NetworkAdapter WHERE InterfaceIndex={0}", idx);
                var searcher = new ManagementObjectSearcher
                {
                    Query = new ObjectQuery(query)
                };
    
                try
                {
                    ManagementObjectCollection adapterObjects = searcher.Get();
    
                    var names = (from ManagementObject o in adapterObjects
                                 select o["Name"])
                                .Cast<string>()
                                .FirstOrDefault();
    
                    return names;
                }
                catch (Exception ex)
                {
                    log.Fatal(ex);
                    throw;
                }
            }
    
            private static uint GetConnectedCardIndex()
            {
                try
                {
                    string localhostName = Dns.GetHostName();
                    IPHostEntry ipEntry = Dns.GetHostEntry(localhostName);
                    IPAddress[] addresses = ipEntry.AddressList;
                    var address = GetNeededIPAddress(addresses);
                    uint ipaddr = IPAddressToUInt32(address);
                    uint interfaceindex;
                    GetBestInterface(ipaddr, out interfaceindex);
    
                    return interfaceindex;
                }
                catch (Exception ex)
                {
                    log.Fatal(ex);
                    throw;
                }
            }
    
            private static uint IPAddressToUInt32(IPAddress address)
            {
                Contract.Requires<ArgumentNullException>(address != null);
    
                try
                {
                    byte[] byteArray1 = IPAddress.Parse(address.ToString()).GetAddressBytes();
                    byte[] byteArray2 = IPAddress.Parse(address.ToString()).GetAddressBytes();
                    byteArray1[0] = byteArray2[3];
                    byteArray1[1] = byteArray2[2];
                    byteArray1[2] = byteArray2[1];
                    byteArray1[3] = byteArray2[0];
                    return BitConverter.ToUInt32(byteArray1, 0);
                }
                catch (Exception ex)
                {
                    log.Fatal(ex);
                    throw;
                }
            }
    
            private static IPAddress GetNeededIPAddress(IEnumerable<IPAddress> addresses)
            {
                var query = from address in addresses
                            where address.AddressFamily == AddressFamily.InterNetwork
                            select address;
    
                return query.FirstOrDefault();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to Select A Row in my table view programmatically, I believe I
I need to programmatically set a cell in editing mode. I know that setting
This is what I have right now: http://jsfiddle.net/w6PAC/3/ I want the select button to
I can't figure out how to programmatically select items in checkboxlist. This method of
i have a datagrid (created programmatically) wich is connected to an JsonRestStore. In that
I want to programmatically create a new column in an MS Access table. I've
I want to programmatically verify the status of an application to see if it
I want to programmatically invoke an event handler for a control. For example: DateTimePicker
I want to programmatically create a new log4j ConsoleAppender and add it as an
I want to programmatically read the contents of the /etc directory. If possible please

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.