public IPInfo GetMyIPInfo() { try { IPInfo Result = new IPInfo(); XmlDocument doc = new XmlDocument(); doc.LoadXml((new WebClient()).DownloadString("http://ip-api.com/xml/")); Result.IP = IPAddress.Parse(((XmlNode)doc.DocumentElement.SelectSingleNode("query")).InnerText); Result.Status = ((XmlNode)doc.DocumentElement.SelectSingleNode("status")).InnerText; Result.Country = ((XmlNode)doc.DocumentElement.SelectSingleNode("country")).InnerText; Result.CountryCode = ((XmlNode)doc.DocumentElement.SelectSingleNode("countryCode")).InnerText; Result.Region = ((XmlNode)doc.DocumentElement.SelectSingleNode("region")).InnerText; Result.RegionName = ((XmlNode)doc.DocumentElement.SelectSingleNode("regionName")).InnerText; Result.City = ((XmlNode)doc.DocumentElement.SelectSingleNode("city")).InnerText; Result.Zip = ((XmlNode)doc.DocumentElement.SelectSingleNode("zip")).InnerText; Result.Latitude = float.Parse(((XmlNode)doc.DocumentElement.SelectSingleNode("lat")).InnerText, CultureInfo.InvariantCulture.NumberFormat); Result.Longitude = float.Parse(((XmlNode)doc.DocumentElement.SelectSingleNode("lon")).InnerText, CultureInfo.InvariantCulture.NumberFormat); Result.Timezone = ((XmlNode)doc.DocumentElement.SelectSingleNode("timezone")).InnerText; Result.ISP = ((XmlNode)doc.DocumentElement.SelectSingleNode("isp")).InnerText; Result.Organization = ((XmlNode)doc.DocumentElement.SelectSingleNode("org")).InnerText; Result.RespondPing = PingHost(((XmlNode)doc.DocumentElement.SelectSingleNode("query")).InnerText); return(Result); } catch { return(null); } }
void LoadMyIPInfo() { MyIPInfo = Functions.GetMyIPInfo(); if (MyIPInfo != null) { //Font OriginalFont = richTextBoxIPInfo.Font; richTextBoxIPInfo.Text = ""; richTextBoxIPInfo.DeselectAll(); richTextBoxIPInfo.SelectionAlignment = HorizontalAlignment.Center; richTextBoxIPInfo.SelectionFont = new Font("Arial", 7, FontStyle.Bold); richTextBoxIPInfo.SelectionColor = Color.Red; richTextBoxIPInfo.AppendText("Current IP: "); //richTextBoxIPInfo.SelectionFont = OriginalFont; richTextBoxIPInfo.SelectionColor = Color.DarkGreen; richTextBoxIPInfo.AppendText(MyIPInfo.IP.ToString() + " (" + MyIPInfo.Country + ", " + MyIPInfo.RegionName + ")");; richTextBoxIPInfo.SelectionFont = new Font("Arial", 7, FontStyle.Bold); richTextBoxIPInfo.SelectionColor = Color.Red; richTextBoxIPInfo.AppendText("\tLat/Long: "); //richTextBoxIPInfo.SelectionFont = OriginalFont; richTextBoxIPInfo.SelectionColor = Color.DarkGreen; richTextBoxIPInfo.AppendText(MyIPInfo.Latitude + "," + MyIPInfo.Longitude); richTextBoxIPInfo.SelectionFont = new Font("Arial", 7, FontStyle.Bold); richTextBoxIPInfo.SelectionColor = Color.Red; richTextBoxIPInfo.AppendText("\tISP: "); //richTextBoxIPInfo.SelectionFont = OriginalFont; richTextBoxIPInfo.SelectionColor = Color.DarkGreen; richTextBoxIPInfo.AppendText(MyIPInfo.ISP + ", " + MyIPInfo.Organization); } else { richTextBoxIPInfo.Text = "No internet connection"; } }