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

后端 未结 10 1063
春和景丽
春和景丽 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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.

提交回复
热议问题