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 7401559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:30:14+00:00 2026-05-29T04:30:14+00:00

I am trying to get IP address on local machine: private string GetIP() {

  • 0

I am trying to get IP address on local machine:

    private string GetIP()
    {

        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);


        IPAddress[] addr = ipEntry.AddressList;

        foreach (IPAddress ipaddr in addr)
        {
            if (ipaddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                return ipaddr.ToString();
        }

        return "IP error";                                   
    }

However I can’t find a way to identify which interface is the one i need. For example:

enter image description here

I am lucky that the one i need is second in the list. But if it were in the back i would get IP of a wrong interface. How to check if I am getting IP for local area connection (or in general, the interface responsible for the connection).

  • 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-29T04:30:17+00:00Added an answer on May 29, 2026 at 4:30 am

    You may be able to enumerate the network interfaces directly (rather than just their IPs) and filter then based on their interface type:

    var interfaces = NetworkInterface.GetAllNetworkInterfaces()
    

    And then filter it with something like:

    interfaces.Where(ni => ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
                           ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
    

    It may still return multiple network interfaces but it’ll filter out at least some of them that you don’t want. I use the above filter to get rid of loopback and virtual machine interfaces.

    Then from there you can get the network interface’s IP address using the IP properties.

    In the spirit of brevity, once you determine which interface is the right one, you can get the IPv4 address (or at least one of them) of the interface using:

    iface.GetIPProperties().UnicastAddresses.SingleOrDefault(ua => ua.Address.AddressFamily == AddressFamily.InterNetwork);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I'm trying to get the IP-Address of my machine in my local network
I'm trying to get the ip address of my local PC, and one one
i'm trying to get information from a remote server on my local machine. readyState
I'm trying to retrieve the IP Address of the local machine in my program.
I am trying to get an address based on the long/lat. it appears that
I'm trying to get the FROM email address in Mule ESB. I'm getting the
I am trying to get my Internet IP address in Java but I keep
I am trying to get some simple UDP communication working on my local network.
I am trying to get incoming email to a specific email address redirected to
I'm experimenting with MSMQ and can get a sender/receiver working on a local machine.

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.