IfElse检查扫描是否等于IPv4

时间:2013-09-01 17:33:20

标签: vb.net if-statement

我想知道,如果myClientMachineIp(来自代码)等于AddressFamily.InterNetwork,如何进行If语句检查?

我目前的代码:

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 As String = myClientMachineAddressList.AddressList(0).ToString()

        If myClientMachineIP = AddressFamily.InterNetwork Then
            TextBox1.Text = myClientMachineIP
        Else
            TextBox1.Text = "IP does not equal to IPv4"

        End If
    End Sub

1 个答案:

答案 0 :(得分:1)

请勿通过致电ToString()来丢弃有价值的信息。使用IPAddress类型:

Dim myClientMachineAddressList _
    = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP = myClientMachineAddressList.AddressList(0)

If myClientMachineIP.AddressFamily = Sockets.AddressFamily.InterNetwork Then
    TextBox1.Text = myClientMachineIP.ToString()
Else
    TextBox1.Text = "IP does not equal to IPv4".
End If