The title expresses the essence of the question.
1 answer
It’s not so simple: it’s not hard to get a list of interfaces, but it’s going to determine which one is looking outside (and not into a local network, for example, on the Internet). What could be the criteria - this is not a loopback interface, there is a gateway, etc.
To get started, try this:
foreach ( NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces() ) { if ( ni.OperationalStatus == OperationalStatus.Up && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback ) { // ваш выход } } - Initially, I assumed that the interface with the smallest metric among all is used to access the Internet. In most cases, this is true, but exceptions are not so rare. - SNMetamorph
- @SNMetamorph, it seems to me that this is really not the best solution - and if such characteristics as connection speed will be used when setting priorities? It can ruin everything. - isnullxbh
- @SNMetamorph, if this is not critical for you - you can send Ping to every found interface. Given the above factors as screening out exactly unsuitable interfaces. - isnullxbh
|