判断是否为IP地址

方法一:

bool isIpResult = false;
            try
            {
                //using System.Net;
                IPAddress ipAddress = IPAddress.Parse(strIpAddress);
                isIpResult = true;
            }
            catch
            {
                isIpResult = false;
            }
            return isIpResult;

方法二:

bool isIpAddressType = false;
            //using System.Text.RegularExpressions;
            Regex regex = new Regex("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$");

            isIpAddressType = regex.IsMatch(strIpAddress.Trim());

            if (isIpAddressType)
            {
                string[] strTemp = strIpAddress.Split(new char[] { '.' });
                for (int i = 0; i < strTemp.Length; i++)
                {
                    if (Convert.ToInt32(strTemp[i]) > 255)
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }