我有这样的代码
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()
Me.Text = GetComputerName()
Me.ComboBox1.Items.AddRange(myClientMachineIP)
End Sub
我试图获得一个活跃的Ipv4地址适配器并将其添加到组合框但我收到此错误类型'字符串'无法转换为' 1维数组的对象'
答案 0 :(得分:1)
您可以选中AddressFamiliy
来过滤您的地址,
要过滤所有IPv4地址,AddressFamiliy
应等于AddressFamily.InterNetwork
:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP = myClientMachineAddressList.AddressList.Where(Function(a) a.AddressFamily = AddressFamily.InterNetwork).ToArray()
Me.Text = GetComputerName()
Me.ComboBox1.Items.AddRange(myClientMachineIP)
End Sub