how to convert string to System.Net.IPAddress
.net, c#, ip-address
Solution
Use the static `IPAddress.Parse` method to parse a `string` into an `IPAddress`:
foreach (var ipLine in File.ReadAllLines("proxy.txt"))
{
var ip = IPAddress.Parse(ipLine);
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
textBox1.Text = ip.ToString();
}
}
If the lines in the file are not always valid IP addresses, you may want to consider using `TryParse` to avoid any exceptions being thrown.
Problem
how can i convert string to System.Net,IPAddress in C#/.net 3.5 i tried this but i got this error "Cannot convert type 'string' to 'System.Net.IPAddress'" ``` public void Form1_Load(object sender, EventArgs e) { IPHostEntry host; string localIP = "?"; host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in File.ReadAllLines("proxy.txt")) { if (ip.AddressFamily.ToString() == "InterNetwork") { localIP = ip.ToString(); textBox1.Text = ip.ToString(); } } } ```