Get public/external IP address?

前端 未结 26 1990
鱼传尺愫
鱼传尺愫 2020-11-22 14:32

I cant seem to get or find information on finding my routers public IP? Is this because it cant be done this way and would have to get it from a website?

26条回答
  •  忘了有多久
    2020-11-22 14:42

    Or this, it works quite well i think for what i needed. It's from here.

    public IPAddress GetExternalIP()
    {
        WebClient lol = new WebClient();
        string str = lol.DownloadString("http://www.ip-adress.com/");
        string pattern = "

    My IP address is: (.+)

    " MatchCollection matches1 = Regex.Matches(str, pattern); string ip = matches1(0).ToString; ip = ip.Remove(0, 21); ip = ip.Replace(" ", ""); ip = ip.Replace(" ", ""); return IPAddress.Parse(ip); }

提交回复
热议问题