C# 搜索结果将字符串解析为IP地址的Web结果不';有段时间我不工作

C# 搜索结果将字符串解析为IP地址的Web结果不';有段时间我不工作,c#,C#,我正在尝试将字符串IP转换为ipAddress,但对于某些字符串,此方法不起作用,问题是什么? 例: 例如,如果我使用这个字符串“192.168.001.011”,那么IPAddress.Parse方法将返回192.168.1.9 或“192.168.1.012”将返回“192.168.1.10” 为什么? 我真的很困惑…IPAddress.Parse将前导零视为八进制,这就是为什么会得到意外结果的原因 这对我有用 using System.Text.RegularExpressions; ..

我正在尝试将字符串IP转换为ipAddress,但对于某些字符串,此方法不起作用,问题是什么? 例:

例如,如果我使用这个字符串“192.168.001.011”,那么IPAddress.Parse方法将返回192.168.1.9 或“192.168.1.012”将返回“192.168.1.10” 为什么?
我真的很困惑…

IPAddress.Parse将前导零视为八进制,这就是为什么会得到意外结果的原因

这对我有用

using System.Text.RegularExpressions;
..
..
    Console.WriteLine("ip address");
    string ip = Console.ReadLine();
    //Remove the leading zeroes with Regex...
    ip = Regex.Replace(ip, "0*([0-9]+)", "${1}");
    System.Net.IPAddress IP = System.Net.IPAddress.Parse(ip);
    Console.WriteLine(IP);

我解决了这个cod的问题:

Console.WriteLine("ip address");
string ip = Console.ReadLine();
string[] s = ip.Split('.');
IPAddress IP= IPAddress.Parse(Int16.Parse(s[0]) + "." + Int16.Parse(s[1]) + "." + 
Int16.Parse(s[2]) + "." + Int16.Parse(s[3]));
Console.WriteLine(IP);
但我认为你的方式更好。。。非常感谢

Console.WriteLine("ip address");
string ip = Console.ReadLine();
string[] s = ip.Split('.');
IPAddress IP= IPAddress.Parse(Int16.Parse(s[0]) + "." + Int16.Parse(s[1]) + "." + 
Int16.Parse(s[2]) + "." + Int16.Parse(s[3]));
Console.WriteLine(IP);