How to get the IP address of the server on which my C# application is running on?

前端 未结 26 2275
天命终不由人
天命终不由人 2020-11-22 06:01

I am running a server, and I want to display my own IP address.

What is the syntax for getting the computer\'s own (if possible, external) IP address?

Someon

26条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 06:55

    And this is to get all local IPs in csv format in VB.NET

    Imports System.Net
    Imports System.Net.Sockets
    
    Function GetIPAddress() As String
        Dim ipList As List(Of String) = New List(Of String)
        Dim host As IPHostEntry
        Dim localIP As String = "?"
        host = Dns.GetHostEntry(Dns.GetHostName())
        For Each ip As IPAddress In host.AddressList
            If ip.AddressFamily = AddressFamily.InterNetwork Then
                localIP = ip.ToString()
                ipList.Add(localIP)
            End If
        Next
        Dim ret As String = String.Join(",", ipList.ToArray)
        Return ret
    End Function
    

提交回复
热议问题