VB.NET Host转IP地址帮助类

本篇介绍VB.NET怎么把Host转IP地址

1.Host转IP地址帮助类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

Imports System.Net.NetworkInformation

''' <summary>
''' VB.NET Host转IP地址帮助类
''' </summary>
Public Class HostHelper

''' <summary>
''' Host转IP地址帮助类
''' </summary>
''' <param name="host">host地址</param>
''' <param name="isIp6">是否为isIp6</param>
''' <returns></returns>
Public Shared Function HostToIp(host As String, Optional isIp6 As Boolean = False) As String
Try
Dim pg As New Ping
Dim pkey As PingReply = pg.Send(host, 1000)
If pkey IsNot Nothing AndAlso pkey.Address IsNot Nothing Then
If isIp6 Then
Return pkey.Address.MapToIPv6.ToString
End If
Return pkey.Address.MapToIPv4.ToString
End If
Return String.Empty
Catch ex As Exception
Return String.Empty
End Try
End Function

End Class

2.调用方法

1
MessageBox.Show(HostHelper.HostToIp("baidu.com", True))

VB.NET Host转IP地址帮助类
https://bgmh.work/2023/12/18/VB-NET-Host转IP地址帮助类/
作者
OuHuanHua
发布于
2023年12月18日
许可协议