如何确定string是否是IP地址?

时间:2015-08-05 07:43:42

标签: c# .net asp.net-mvc

我尝试确定字符串是否为ipaddress,

像这样:

 XElement ipaddresses = new XElement("ipaddresses");
            string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in lines) {

                IPAddress address;
                if (IPAddress.TryParse(item, out address))
                {

                    switch (address.AddressFamily)
                    {
                        case System.Net.Sockets.AddressFamily.InterNetwork:
                            // we have IPv4
                            break;
                        case System.Net.Sockets.AddressFamily.InterNetworkV6:
                            // we have IPv6
                            break;
                        default:
                            break;
                    }
                }

所以我用9999检查它。

然后它说它是一个IP地址,在bij地址中显示数字:m_ToString =" 0.1.134.159"

??怎么会这样?

这是字符串:public string IpAddress { get; set; }

谢谢

0 个答案:

没有答案