[英]how to convert string to System.Net.IPAddress
how can i convert string to System.Net,IPAddress in C#/.net 3.5 我如何将字符串转换为C#/。net 3.5中的System.Net,IPAddress
i tried this but i got this error "Cannot convert type 'string' to 'System.Net.IPAddress'" 我尝试了此操作,但收到此错误“无法将类型'string'转换为'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();
}
}
}
Use the static IPAddress.Parse
method to parse a string
into an IPAddress
: 使用静态IPAddress.Parse
方法将string
解析为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. 如果文件中的行并非总是有效的IP地址,则可能需要考虑使用TryParse
以避免引发任何异常。
The IPAddress.Parse
method accepts a string. IPAddress.Parse
方法接受一个字符串。
foreach (string line in File.ReadAllLines("proxy.txt"))
{
IPAddress ip = IPAddress.Parse(line);
// ...
}
foreach (IPAddress ip in File.ReadAllLines("proxy.txt").Select(s => IPAddress.Parse(s))) {
// ...
}
You can use IPAddress.Parse Method for example: 您可以使用IPAddress.Parse方法,例如:
private static void parse(string ipAddress)
{
try
{
IPAddress address = IPAddress.Parse(ipAddress);
}
您可以使用IPAddress.Parse来做到这一点。
One thing you can do is this... In the foreach loop, you need an instance that matches your collection. 您可以做的一件事是...在foreach循环中,您需要一个与您的收藏相匹配的实例。 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... 程序员无法使用int或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". 这根本行不通,并且会引发有关“ int”不是“ 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. IPAddress是它自己的类和类型。 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. 明确将字符串转换为IP地址。 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. 接下来,您需要将当前字符串解析为本地IP地址实例。 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. 有几种方法可以这样做:1)您可以简单地将字符串(System.Net.IPAddress.Parse())解析为字符串,或者可以将字符串TryParse(IPAddress.TryParse())解析为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! 我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.