简体   繁体   中英

How to detect Network Location Type in Windows using C#?

I am writing a Windows app using C# where I need to detect the network location type.

When you connect a Windows machine to a network (wired or wireless), it will attempt to identify the network and then classify it as public, private, etc. I want to programmatically do the same thing using my application. How do I do that?

Please let me know if additional details are required. Thank you.

  you networkinterface to get the network information  
  NetworkInterface[] interfaces =  NetworkInterface.GetAllNetworkInterfaces();


  it has many properties and the output showing infornt of that

  NetworkInterface[0].Description:  Intel(R) 82579LM Gigabit Network Connection
  NetworkInterface[0].ID: {xxxxxxxxxx-0322-466Fxxx-xxxxxxxxxx}
  NetworkInterface[0].Name: Ethernet
  NetworkInterface[0].Interface Type: Ethernet
  NetworkInterface[0].Operational Status: Down
  NetworkInterface[0].Speed: -1
  NetworkInterface[0].Supports Multicast: True

You would need to do two things:

  • Resolve IP address of the local machine. This is a challenge by itself as usually machine would have several IP addresses. It may use several IP at the same time. IP addresses may (and are) dynamically change

  • Check if the IP address is in the private range. See this answer for links and more info. Effectively, every private address is routable only within a local network - LAN. It is not possible to connect from the outside to a private address. If machine wants to speak to the outside world it uses a Gateway Machine . Gateway machine remembers where the request came from (private IP) translates it to a public IP and resolves the reverse response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM