How do I get a computer's name and IP address using VB.NET?

后端 未结 10 1016
春和景丽
春和景丽 2020-12-09 11:32

How can i get ip address of system by sending mac ip address as input using vb.net coding?

相关标签:
10条回答
  • 2020-12-09 12:10

    Use the My Class :)

    My.Computer.Name
    

    as for the IP address quick google search

    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
    
    0 讨论(0)
  • 2020-12-09 12:18

    Shows the Computer Name, Use a Button to call it

    Dim strHostName As String

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

    Shows the User Name, Use a Button to call it

    If TypeOf My.User.CurrentPrincipal Is Security.Principal.WindowsPrincipal Then

            Dim parts() As String = Split(My.User.Name, "\")
    
            Dim username As String = parts(1)
    
            MsgBox(username)
    
       End If
    

    For IP Address its little complicated, But I try to explain as much as I can. First write the next code, before Form1_Load but after import section

    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
    

    Then in Form1_Load Section just call it

    GetIPAddress()

    Result: On form load it will show a msgbox along with the IP address, for put into Label1.text or some where else play with the code.

    0 讨论(0)
  • 2020-12-09 12:24
    Dim ipAddress As IPAddress
    Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
    ipAddress = ipHostInfo.AddressList(0)
    
    0 讨论(0)
  • 2020-12-09 12:30

    Here is Example for this. In this example we can get IP address of our given host name.

       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
    
    0 讨论(0)
  • 2020-12-09 12:33
    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
    
    0 讨论(0)
  • 2020-12-09 12:33

    IP Version 4 Only ...

    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
    
    0 讨论(0)
提交回复
热议问题