none
From code to label RRS feed

  • Question

  • How to make this code show to label, the ip address from the internet supplier?

      public string GetIPAddress()
            {
                IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated.
                IPAddress ipAddress = ipHostInfo.AddressList[0];

                return ipAddress.ToString();
            }

    Saturday, May 5, 2018 11:58 AM

All replies

  • Hi GeyBen420,

    Do you mean to show the ipAddress to Label control?

    If this is true, then get a label control from Toolbox,

    and assign your function to property of label control

    Like follows,


            private void button1_Click(object sender, EventArgs e)
            {
                string text = GetIPAddress();

                label1.Text = text;
            }

    Best regards

    E-John


    • Edited by E-John Saturday, May 5, 2018 12:45 PM
    Saturday, May 5, 2018 12:40 PM
  • Why do i get this error on code line:

    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated.

    Saturday, May 5, 2018 12:48 PM
  • Hi GeyBen420,

    Did you add "using System.Net;" at the beginning of the file?

    Thanks and Best regards,

    E-John

    Saturday, May 5, 2018 12:54 PM
  • This is getting the site ip, i need to get a user who is using this app.
    Saturday, May 5, 2018 9:12 PM
  • Hi GeyBen420,

    Thank you for posting here.

    >>Why do i get this error on code line:

    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated.

    This API is now obsolete. Hence you could not use Dns.Resolve(). For more details, please refer to the MSDN document.

    https://msdn.microsoft.com/en-us/library/system.net.dns.resolve%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    >>This is getting the site ip, i need to get a user who is using this app.

    If you want to get the ip of user who use this app, it means you want to get the user local ip address. Please try the code below.

     public static string GetLocalIPAddress()
            {
                var host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (var ip in host.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return ip.ToString();
                    }
                }
                throw new Exception("No network adapters with an IPv4 address in the system!");
            }

    And them show the ip address in label.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, May 7, 2018 7:50 AM
    Moderator
  • When you are asking an ASP question, you need to specify that very clearly in the question.  E-John answered the question you asked, but that apparently isn't the question you meant to ask.

    The public IP of the requestor is passed in an environment variable.  You can get that with Request.ServerVariables["REMOTE_ADDR"].

    Note that this isn't always very useful.  Because of proxies and gateways, most companies funnel their traffic through a small number of IP addresses.  For example, ALL of the computers within Intel will have the exact same REMOTE_ADDR.

    In some cases, you might want to check Request.ServerVariables["HTTP_X_FORWARDED_FOR"] first.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, May 8, 2018 5:28 AM