Reputation: 331
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();
}
}
}
Upvotes: 17
Views: 56758
Reputation: 507
One thing you can do is this... In the foreach loop, you need an instance that matches your collection. What I mean is that if you have, for instance, you have a List of strings, like this:
List<string> lMyList
A programmer cannot iterate through this list with an instance of an int or a double...
foreach (int lIterator in lMyList)
{ // code
}
This simply will not work and it will throw a syntax error about an "int" isn't a type of "string".
To solve that, you will need to iterate through your list like this
foreach (string lIterator in lMyList)
{ // code
}
Now, to your question. An IPAddress is it's own class and type. One cannot simply make it a type of string. However, there are ways to get around this. Explicitly convert a string to an IP address. But, you will need to iterate over your list first.
foreach (string lLine in File.ReadAllLines("proxy.txt"))
{
// Code In Here
}
This will iterate all the lines in the text file. Since it's a text file, it returns a list of strings. In order to iterate over a list of strings, a programmer needs a string local variable to iterate over it.
Next, you will need to parse the current string to a local IP Address instance. There are a few ways of doing this, 1) You can simply Parse (System.Net.IPAddress.Parse()) the string, or you can TryParse (IPAddress.TryParse()) a string into an IPAddress.
Method One:
// This will parse the current string instance to an IP Address instance
IPAddress lIPAddress = IPAddress(lLine);
Method Two:
IPAddress lIPAddress; // Local instance to be referenced
// At runtime, this will "Try to Parse" the string instance to an IP Address.
// This member method returns a bool, which means true or false, to say,
// "Hey, this can be parsed!". Your referenced local IP Address instance would
// have the value of the line that was parsed.
if (IPAddress.TryParse(lLine, out lIPAddress))
{
// Code
}
Okay, now we got that out of the way. Lets finish this up.
// Iterates over the text file.
foreach (string lLine in File.ReadAllLines("proxy.txt"))
{
// Create a local instance of the IPAddress class.
IPAddress lIPAddress;
// Try to Parse the current string to the IPAddress instance.
if (IPAddress.TryParse(lLine, out lIPAddress))
{
// This means that the current line was parsed.
// Test to see if the Address Family is "InterNetwork"
if (string.Equals("InterNetwork", lIPAddress.AddressFamily.ToString()))
{
TextBox1.Text = lIPAddress.ToString();
}
}
}
I hope this helps!
Upvotes: 0
Reputation: 43743
The IPAddress.Parse
method accepts a string.
foreach (string line in File.ReadAllLines("proxy.txt"))
{
IPAddress ip = IPAddress.Parse(line);
// ...
}
Upvotes: 6
Reputation: 30618
foreach (IPAddress ip in File.ReadAllLines("proxy.txt").Select(s => IPAddress.Parse(s))) {
// ...
}
Upvotes: 3
Reputation: 498904
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.
Upvotes: 25
Reputation: 1200
You can use IPAddress.Parse Method for example:
private static void parse(string ipAddress)
{
try
{
IPAddress address = IPAddress.Parse(ipAddress);
}
Upvotes: 2