Get IP address in a console application

前端 未结 6 1921
醉酒成梦
醉酒成梦 2021-02-12 22:35

I am looking to figure out what my IP address is from a console application.

I am used to a web application by using the Request.ServerVariables collection

6条回答
  •  后悔当初
    2021-02-12 22:48

    Try this:

    String strHostName = Dns.GetHostName();
    
    Console.WriteLine("Host Name: " + strHostName);
    
    // Find host by name    IPHostEntry
    iphostentry = Dns.GetHostByName(strHostName);
    
    // Enumerate IP addresses
    int nIP = 0;   
    foreach(IPAddress ipaddress in iphostentry.AddressList) {
       Console.WriteLine("IP #" + ++nIP + ": " + ipaddress.ToString());    
    }
    

提交回复
热议问题