📄 getip.vb
字号:
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtHostname As System.Windows.Forms.TextBox
Friend WithEvents txtIP As System.Windows.Forms.TextBox
Friend WithEvents btnLocal As System.Windows.Forms.Button
Friend WithEvents btnIPtoHost As System.Windows.Forms.Button
Friend WithEvents btnHosttoIP As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.txtHostname = New System.Windows.Forms.TextBox()
Me.txtIP = New System.Windows.Forms.TextBox()
Me.btnLocal = New System.Windows.Forms.Button()
Me.btnIPtoHost = New System.Windows.Forms.Button()
Me.btnHosttoIP = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "计算机名:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 64)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(72, 13)
Me.Label2.TabIndex = 1
Me.Label2.Text = "IP地址:"
'
'txtHostname
'
Me.txtHostname.Location = New System.Drawing.Point(104, 24)
Me.txtHostname.Name = "txtHostname"
Me.txtHostname.Size = New System.Drawing.Size(152, 21)
Me.txtHostname.TabIndex = 2
Me.txtHostname.Text = ""
'
'txtIP
'
Me.txtIP.Location = New System.Drawing.Point(104, 56)
Me.txtIP.Name = "txtIP"
Me.txtIP.Size = New System.Drawing.Size(152, 21)
Me.txtIP.TabIndex = 3
Me.txtIP.Text = ""
'
'btnLocal
'
Me.btnLocal.Location = New System.Drawing.Point(64, 96)
Me.btnLocal.Name = "btnLocal"
Me.btnLocal.Size = New System.Drawing.Size(144, 32)
Me.btnLocal.TabIndex = 4
Me.btnLocal.Text = "本机名称和IP"
'
'btnIPtoHost
'
Me.btnIPtoHost.Location = New System.Drawing.Point(64, 144)
Me.btnIPtoHost.Name = "btnIPtoHost"
Me.btnIPtoHost.Size = New System.Drawing.Size(144, 32)
Me.btnIPtoHost.TabIndex = 5
Me.btnIPtoHost.Text = "IP->计算机名"
'
'btnHosttoIP
'
Me.btnHosttoIP.Location = New System.Drawing.Point(64, 192)
Me.btnHosttoIP.Name = "btnHosttoIP"
Me.btnHosttoIP.Size = New System.Drawing.Size(144, 32)
Me.btnHosttoIP.TabIndex = 6
Me.btnHosttoIP.Text = "计算机名->IP"
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(288, 262)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnHosttoIP, Me.btnIPtoHost, Me.btnLocal, Me.txtIP, Me.txtHostname, Me.Label2, Me.Label1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.MaximizeBox = False
Me.Name = "frmMain"
Me.Text = "获取计算机名和IP"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnHosttoIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHosttoIP.Click
'根据输入的计算机名获取其IP地址
Dim name As String
name = txtHostname.Text
'JudgeName()函数用于判断输入的计算机名是否合法
'合法则返回True,否则返回False
If JudgeName(name) Then
Try
'获取该计算机的IP地址并显示
Dim ip As String
Dim Address() As System.Net.IPAddress
Address = System.Net.Dns.GetHostByName(name).AddressList
ip = Address(0).ToString
txtIP.Text = ip
Catch ex As Exception
'显示异常信息
MessageBox.Show("发生错误:" + ex.Message)
End Try
txtHostname.Focus()
txtHostname.SelectionStart = 0
txtHostname.SelectionLength = txtHostname.Text.Length
Else
'若输入的计算机名不合法,则提示错误
MessageBox.Show("不存在主机!")
'焦点重定向到txthostname控件让用户重新输入
txtHostname.Focus()
txtHostname.SelectionStart = 0
txtHostname.SelectionLength = txtHostname.Text.Length
End If
End Sub
Private Function JudgeIP(ByVal ip As String) As Boolean
'numcount用于统计连续的数字个数
Dim numcount As Integer = 0
'dotcount用于统计该字符串中"."的个数
Dim dotcount As Integer = 0
'合法的IP地址字符串长度不小于7
If ip.Length < 7 Then
Return False
End If
'判断首位是否为数字
If Char.IsDigit(ip.Chars(0)) = False Then
Return False
Else
numcount += 1
End If
Dim i As Integer
'依次判断各位字符是否为数字或"."
For i = 1 To ip.Length - 1
If Char.IsDigit(ip.Chars(i)) Then
'若该字符为数字
numcount += 1
'连续数字不得超过3位
If numcount > 3 Then
Return False
End If
'若为3位,则其值不得超过255
If numcount = 3 Then
Dim a, b, c As Integer
a = Char.GetNumericValue(ip.Chars(i - 2))
b = Char.GetNumericValue(ip.Chars(i - 1))
c = Char.GetNumericValue(ip.Chars(i))
If a * 100 + b * 10 + c > 255 Then
Return False
End If
End If
ElseIf ip.Chars(i) = "." Then
'若该字符为点
'如果该点与前一点之间没有任何数字,则该IP地址非法
If numcount = 0 Then
Return False
End If
numcount = 0
dotcount += 1
Else
'若为其他字符,则该IP地址非法
Return False
End If
Next
'最后,点的总数应为3个
If dotcount <> 3 Then
Return False
End If
'最后一位字符应该是数字
If Not Char.IsDigit(ip.Chars(ip.Length - 1)) Then
Return False
End If
Return True
End Function
Private Function JudgeName(ByVal name As String) As Boolean
If name.Length = 0 Then
Return False
End If
Dim i As Integer
Dim flag As Boolean = False
For i = 0 To name.Length - 1
If Char.IsLetterOrDigit(name.Chars(i)) Then
'该字符是字母或数字
flag = True
ElseIf name.Chars(i) = "-" Then
'该字符是连接符"-"
flag = True
End If
If flag = False Then
Return False
End If
Next
Return True
End Function
Private Sub btnLocal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocal.Click
'获取本地主机的计算机名及IP地址并显示
Try
Dim Address() As System.Net.IPAddress
Dim name As String
'获取本地主机的计算机名
name = System.Net.Dns.GetHostName()
txtHostname.Text = name
'获取本地主机的IP地址列表
Address = System.Net.Dns.GetHostByName(name).AddressList
'获取主机的首个IP地址
txtIP.Text = Address(0).ToString
Catch ex As Exception
'捕捉到异常,显示其信息
MessageBox.Show("发生错误:" + ex.Message)
End Try
txtIP.Focus()
'设置txtip控件中的内容为被选中
txtIP.SelectionStart = 0
txtIP.SelectionLength = txtIP.Text.Length
End Sub
Private Sub btnIPtoHost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIPtoHost.Click
'根据输入的IP地址获取其计算机名
Dim ip As String
ip = txtIP.Text
'JudgeIP()函数用于判断输入的IP地址是否合法
'合法则返回True,否则返回False
If JudgeIP(ip) Then
Try
'获取该计算机的名称并显示
Dim name As String
name = System.Net.Dns.GetHostByAddress(ip).HostName
txtHostname.Text = name
Catch ex As Exception
'显示异常信息
MessageBox.Show("发生错误:" + ex.Message)
End Try
txtIP.Focus()
txtIP.SelectionStart = 0
txtIP.SelectionLength = txtIP.Text.Length
Else
'若输入的IP地址不合法,则提示错误
MessageBox.Show("不存在此IP!")
'焦点重定向到txtip控件让用户重新输入
txtIP.Focus()
txtIP.SelectionStart = 0
txtIP.SelectionLength = txtIP.Text.Length
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -