如何使用VB.NET获取计算机的名称和IP地址?

时间:2010-02-10 05:57:53

标签: vb.net

如何通过使用vb.net编码发送mac ip地址作为输入来获取系统的IP地址?

10 个答案:

答案 0 :(得分:31)

使用我的班级:)

My.Computer.Name

至于IP地址快速谷歌搜索

Private Sub GetIPAddress()

Dim strHostName As String

Dim strIPAddress As String



strHostName = System.Net.Dns.GetHostName()

strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()


MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)

End Sub

答案 1 :(得分:19)

Private Function GetIPv4Address() As String
    GetIPv4Address = String.Empty
    Dim strHostName As String = System.Net.Dns.GetHostName()
    Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)

    For Each ipheal As System.Net.IPAddress In iphe.AddressList
        If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
            GetIPv4Address = ipheal.ToString()
        End If
    Next

End Function

答案 2 :(得分:3)

以下是此示例。 在此示例中,我们可以获取给定主机名的IP地址。

   Dim strHostName As String = "jayeshsorathia.blogspot.com"
    'string strHostName = "www.microsoft.com";
    ' Get DNS entry of specified host name
    Dim addresses As IPAddress() = Dns.GetHostEntry(strHostName).AddressList

    ' The DNS entry may contains more than one IP addresses.
    ' Iterate them and display each along with the type of address (AddressFamily).
    For Each address As IPAddress In addresses
        Response.Write(String.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily))
        Response.Write("<br/><br/>")
    Next

答案 3 :(得分:1)

谢谢Shuwaiee

我在Private Sub已经使用它时做了一些小改动。

Dim GetIPAddress()

Dim strHostName As String

Dim strIPAddress As String

strHostName = System.Net.Dns.GetHostName()

strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()

MessageBox.Show("Host Name: " & strHostName & vbCrLf & "IP Address: " & strIPAddress)

但是也改变了细节的显示方式,以便他们可以使用& vbCrLf &

在单独的行上显示
MessageBox.Show("Host Name: " & strHostName & vbCrLf & "IP Address: " & strIPAddress)

希望这有助于某人。

答案 4 :(得分:0)

Dim ipAddress As IPAddress
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
ipAddress = ipHostInfo.AddressList(0)

答案 5 :(得分:0)

显示计算机名称,使用按钮进行调用

  

Dim strHostName As String

    strHostName = System.Net.Dns.GetHostName().

    MsgBox(strHostName)

显示用户名,使用按钮进行呼叫

  

如果TypeOf My.User.CurrentPrincipal是Security.Principal.WindowsPrincipal那么

        Dim parts() As String = Split(My.User.Name, "\")

        Dim username As String = parts(1)

        MsgBox(username)

   End If

对于IP地址来说,它有点复杂,但我试着尽可能多地解释。 首先在Form1_Load之前但在导入部分

之后编写下一个代码
  

Public Class Form1

Dim mem As String

Private Sub GetIPAddress()

    Dim strHostName As String
    Dim strIPAddress As String
    strHostName = System.Net.Dns.GetHostName()
    strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()

    mem = strIPAddress
    MessageBox.Show("IP Address: " & strIPAddress)

End Sub

然后在Form1_Load部分中调用它

  

GetIPAddress()

结果:在表单加载时,它将显示一个msgbox以及IP地址,用于放入Label1.text或其他一些用代码播放的地方。

答案 6 :(得分:0)

Imports System.Net

Module MainLine
    Sub Main()
        Dim hostName As String = Dns.GetHostName
        Console.WriteLine("Host Name : " & hostName & vbNewLine)
        For Each address In Dns.GetHostEntry(hostName).AddressList()
            Select Case Convert.ToInt32(address.AddressFamily)
                Case 2
                    Console.WriteLine("IP Version 4 Address: " & address.ToString)
                Case 23
                    Console.WriteLine("IP Version 6 Address: " & address.ToString)
            End Select
        Next
        Console.ReadKey()
    End Sub
End Module

答案 7 :(得分:0)

仅限IP版本4 ......

Imports System.Net

Module MainLine
    Sub Main()
        Dim hostName As String = Dns.GetHostName
        Console.WriteLine("Host Name: " & hostName & vbNewLine)
        Console.WriteLine("IP Version 4 Address(es):")
        For Each address In Dns.GetHostEntry(hostName).AddressList().
            Where(Function(p) p.AddressFamily = Sockets.AddressFamily.InterNetwork)
            Console.WriteLine(vbTab & address.ToString)
        Next
        Console.ReadKey()
    End Sub
End Module

答案 8 :(得分:0)

Label12.Text = "My ID : " + System.Net.Dns.GetHostByName(Net.Dns.GetHostName()).AddressList(0).ToString()

使用此代码,不带任何变量

答案 9 :(得分:-2)

    Public strHostName As String
    Public strIPAddress As String
    strHostName = System.Net.Dns.GetHostName()
    strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
    MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)