如何在vb.net中收集网络系统的ip地址 [英] how to collect the network system ip address in vb.net

查看:81
本文介绍了如何在vb.net中收集网络系统的ip地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

 

 

                      如何在vb.net中收集网络系统IP地址

                      how to collect the network system ip address in vb.net

推荐答案

好吧,

这有点模糊。 您想要运行您的应用程序的当前计算机的IP地址吗?或者您想要整个公司内部网的子网掩码模式?  

that is a bit vague.  Do you want the IP address of the current computer that is running your applicaiton? or do you want the subnet masking pattern for the entire company intranet?  

获取当前系统的IP有点奇怪,但很有可能。 我创建了一个完整的模型来处理这个问题,因为你必须枚举系统适配器,然后枚举分配给每个适配器的IP地址。 
没有直接的方法来确定哪个IP地址是"系统"。 IP地址,因为,例如,如果你有一台带有无线和有线局域网的笔记本电脑,并且连接两个端口,系统将依赖于当前的负载和操作,
,这意味着在那种情况下你将一个系统有两个IP地址,并且无法确定哪个是"默认"。 但是,我的系统确实提供了适配器名称/描述,因此您可以通过枚举确定
哪个适配器是首选IP,在您自己的设计内部。

Getting the current system's IP is a bit strange, but quite possible.  I've created an entire model to take care of this for me, since you have to enumerate the system adapters and then enumerate the IP addresses assigned to each adapter.  there is no direct way to determine which IP address is the "System" IP address, since, for example, if you have a laptop with wireless and wired LAN, and you connect both ports, the system will go through either dependent upon the current load and operation, which means in that circumstance you will have two IP addresses for one system, and there is no way to determine which one is the "default".  However, my system does provide the adapter names / description, so you can, via the enumeration, determine which adapter is the preferred IP, internally to your own design.

My枚举类相当复杂,因为它们使用了很多我的内部集合库,但它们基于System.Net.NetworkInformation命名空间的Shared方法。 以下函数使用NetworkInterface类
来检索当前系统上的可用适配器,然后使用这些NetworkInterface对象(每个适配器一个),您可以枚举IP属性,从而最终确定与每个接口关联的IP地址。

My enumerated classes are rather complex however, since they use a lot of my internal collection library, but they are based on the Shared methods of the System.Net.NetworkInformation namespace.  The following function uses the NetworkInterface class to retrieve the available adapters on the current system, and then with those NetworkInterface objects (one for each adapter) you can enumerate the IP Properties, and thus eventually the IP Address associated with each interface.

	
Public Function GetAssignedIPAddresses() As IPAddress()
	Dim networks() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
	Dim props() As IPInterfaceProperties = networks.Select(Function(net) net.GetIPProperties()).ToArray()
	Dim addys() As IPAddress = props.SelectMany(Function(prop) prop.UnicastAddresses.Select( _
		 Function(a) a.Address)).ToArray()
	Return addys.Where(Function(a) Not IPAddress.IsLoopback(a)).ToArray()
End Function

细分非常简单。 NetworkInterface.GetAllNetworkInterfaces()返回NetworkInterface对象的数组,这些对象描述系统上的每个适配器。 NetworkInterface.GetIPProperties返回单个
适配器的IPInterfaceProperties对象。上面函数的第二行使用linq可枚举扩展来返回每个NetworkInterfaces的所有IPInterfaceProperties的数组。第三行,获取该数组,并使用IpInterfaceProperties
类的UnicaseAddresses属性获取该适配器的IP地址数组。因为它是一个数组,所以使用可枚举的扩展名SelectMany()将IPAddress对象数组的数组压缩成一个单维数组。

The breakdown is quite simple. NetworkInterface.GetAllNetworkInterfaces() returns an array of the NetworkInterface objects which describe each adapter on the system. The NetworkInterface.GetIPProperties, returns an IPInterfaceProperties object for the individual adapters. The Second line of the function above, uses linq enumerable extensions to return an array of the all the IPInterfaceProperties for each of the NetworkInterfaces. The third line, takes that array, and uses the UnicaseAddresses property of the IpInterfaceProperties class to get the array of IP addresses for that adapter. since it is an array, using the enumerable extension SelectMany() condenses the array of array of IPAddress objects into a single dimension array.

Jaeden"Sifo Dyas" al'Raec Ruiner

Jaeden "Sifo Dyas" al'Raec Ruiner


这篇关于如何在vb.net中收集网络系统的ip地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆