Пригодится в том случае если нужно быстро получить ip и скопировать. Это будет консольное приложение. Прежде всего нам нужно подключить using... using System.Net; using System.Windows.Forms; using System.IO; using System; и перед методом Main добавить [STAThread] Console.Title = "Your IP is: ..."; var request = WebRequest.Create("http://checkip.dyndns.org"); // переменная с ответом string reqstring; Console.WriteLine("Checking with http://checkip.dyndns.org..."); using (var reader = new StreamReader(request.GetResponse().GetResponseStream())) // чекаем запрос { reqstring = reader.ReadToEnd(); } string[] a = reqstring.Split(':'); // указываем разделитель затем получаем ip string a2 = a[1].Substring(1); string[] a3 = a2.Split('<'); string ip = a3[0]; Console.Title = "Your IP is: " + ip; // заголовок Console.WriteLine("Your IP adress is: "+ip + Environment.NewLine); //вывод ip из интернета String host = System.Net.Dns.GetHostName(); // получаем имя хоста System.Net.IPAddress locip = System.Net.Dns.GetHostByName(host).AddressList[0]; //локальный ip Console.WriteLine("Your local IP adress is: " + locip); // вывод local ip Clipboard.Clear(); //очистка буфера обмена Clipboard.SetText(ip); //добавляем в буфер наш ip Console.WriteLine("Copied to clipboard~"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); Code Console.Title = "Your IP is: ..."; var request = WebRequest.Create("http://checkip.dyndns.org"); // переменная с ответом string reqstring; Console.WriteLine("Checking with http://checkip.dyndns.org..."); using (var reader = new StreamReader(request.GetResponse().GetResponseStream())) // чекаем запрос { reqstring = reader.ReadToEnd(); } string[] a = reqstring.Split(':'); // указываем разделитель затем получаем ip string a2 = a[1].Substring(1); string[] a3 = a2.Split('<'); string ip = a3[0]; Console.Title = "Your IP is: " + ip; // заголовок Console.WriteLine("Your IP adress is: "+ip + Environment.NewLine); //вывод ip из интернета String host = System.Net.Dns.GetHostName(); // получаем имя хоста System.Net.IPAddress locip = System.Net.Dns.GetHostByName(host).AddressList[0]; //локальный ip Console.WriteLine("Your local IP adress is: " + locip); // вывод local ip Clipboard.Clear(); //очистка буфера обмена Clipboard.SetText(ip); //добавляем в буфер наш ip Console.WriteLine("Copied to clipboard~"); Console.WriteLine("Press any key to exit..."); Console.ReadKey();
Что-то слишком много кода для простого запроса Этого достаточно new WebClient().DownloadString("https://api.ipify.org/");