简体   繁体   English

如何使用C#在Windows中检测网络位置类型?

[英]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. 我正在使用C#编写Windows应用程序,我需要检测网络位置类型。

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. 当您将Windows计算机连接到网络(有线或无线)时,它将尝试识别网络,然后将其分类为公共,私有等。我希望以编程方式使用我的应用程序执行相同的操作。 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. 解析本地计算机的IP地址。 This is a challenge by itself as usually machine would have several IP addresses. 这本身就是一个挑战,因为通常机器会有多个IP地址。 It may use several IP at the same time. 它可能同时使用多个IP。 IP addresses may (and are) dynamically change IP地址可以(并且是)动态地改变

  • Check if the IP address is in the private range. 检查IP地址是否在私有范围内。 See this answer for links and more info. 有关链接和更多信息,请参阅此答案 Effectively, every private address is routable only within a local network - LAN. 实际上,每个私有地址只能在本地网络(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. 网关机器记住请求来自何处(私有IP)将其转换为公共IP并解决反向响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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