?? getip.vb
字號(hào):
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
Public Sub New()
MyBase.New()
'該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent()
'在 InitializeComponent() 調(diào)用之后添加任何初始化
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 窗體設(shè)計(jì)器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下過(guò)程是 Windows 窗體設(shè)計(jì)器所必需的
'可以使用 Windows 窗體設(shè)計(jì)器修改此過(guò)程。
'不要使用代碼編輯器修改它。
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 = "計(jì)算機(jī)名:"
'
'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 = "本機(jī)名稱和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->計(jì)算機(jī)名"
'
'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 = "計(jì)算機(jī)名->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 = "獲取計(jì)算機(jī)名和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
'根據(jù)輸入的計(jì)算機(jī)名獲取其IP地址
Dim name As String
name = txtHostname.Text
'JudgeName()函數(shù)用于判斷輸入的計(jì)算機(jī)名是否合法
'合法則返回True,否則返回False
If JudgeName(name) Then
Try
'獲取該計(jì)算機(jī)的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("發(fā)生錯(cuò)誤:" + ex.Message)
End Try
txtHostname.Focus()
txtHostname.SelectionStart = 0
txtHostname.SelectionLength = txtHostname.Text.Length
Else
'若輸入的計(jì)算機(jī)名不合法,則提示錯(cuò)誤
MessageBox.Show("不存在主機(jī)!")
'焦點(diǎn)重定向到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用于統(tǒng)計(jì)連續(xù)的數(shù)字個(gè)數(shù)
Dim numcount As Integer = 0
'dotcount用于統(tǒng)計(jì)該字符串中"."的個(gè)數(shù)
Dim dotcount As Integer = 0
'合法的IP地址字符串長(zhǎng)度不小于7
If ip.Length < 7 Then
Return False
End If
'判斷首位是否為數(shù)字
If Char.IsDigit(ip.Chars(0)) = False Then
Return False
Else
numcount += 1
End If
Dim i As Integer
'依次判斷各位字符是否為數(shù)字或"."
For i = 1 To ip.Length - 1
If Char.IsDigit(ip.Chars(i)) Then
'若該字符為數(shù)字
numcount += 1
'連續(xù)數(shù)字不得超過(guò)3位
If numcount > 3 Then
Return False
End If
'若為3位,則其值不得超過(guò)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
'若該字符為點(diǎn)
'如果該點(diǎn)與前一點(diǎn)之間沒(méi)有任何數(shù)字,則該IP地址非法
If numcount = 0 Then
Return False
End If
numcount = 0
dotcount += 1
Else
'若為其他字符,則該IP地址非法
Return False
End If
Next
'最后,點(diǎn)的總數(shù)應(yīng)為3個(gè)
If dotcount <> 3 Then
Return False
End If
'最后一位字符應(yīng)該是數(shù)字
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
'該字符是字母或數(shù)字
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
'獲取本地主機(jī)的計(jì)算機(jī)名及IP地址并顯示
Try
Dim Address() As System.Net.IPAddress
Dim name As String
'獲取本地主機(jī)的計(jì)算機(jī)名
name = System.Net.Dns.GetHostName()
txtHostname.Text = name
'獲取本地主機(jī)的IP地址列表
Address = System.Net.Dns.GetHostByName(name).AddressList
'獲取主機(jī)的首個(gè)IP地址
txtIP.Text = Address(0).ToString
Catch ex As Exception
'捕捉到異常,顯示其信息
MessageBox.Show("發(fā)生錯(cuò)誤:" + ex.Message)
End Try
txtIP.Focus()
'設(shè)置txtip控件中的內(nèi)容為被選中
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
'根據(jù)輸入的IP地址獲取其計(jì)算機(jī)名
Dim ip As String
ip = txtIP.Text
'JudgeIP()函數(shù)用于判斷輸入的IP地址是否合法
'合法則返回True,否則返回False
If JudgeIP(ip) Then
Try
'獲取該計(jì)算機(jī)的名稱并顯示
Dim name As String
name = System.Net.Dns.GetHostByAddress(ip).HostName
txtHostname.Text = name
Catch ex As Exception
'顯示異常信息
MessageBox.Show("發(fā)生錯(cuò)誤:" + ex.Message)
End Try
txtIP.Focus()
txtIP.SelectionStart = 0
txtIP.SelectionLength = txtIP.Text.Length
Else
'若輸入的IP地址不合法,則提示錯(cuò)誤
MessageBox.Show("不存在此IP!")
'焦點(diǎn)重定向到txtip控件讓用戶重新輸入
txtIP.Focus()
txtIP.SelectionStart = 0
txtIP.SelectionLength = txtIP.Text.Length
End If
End Sub
End Class
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -