就是一个文本框,然后一个按钮,再判断...我给高分你们大虾们

解决方案 »

  1.   

    一ipv4为例,简单的思路是获取文本框的字符串,然后以“.”将字符串分离为含有四个元素的字符串数组。判断每一个元素对应的值是否在0到255之间。
    参考代码:
            //判断IP地址是否有效
            public static bool IsRightIP(string ip)
            {
                if (Regex.IsMatch(ip, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
                {
                    string[] ips = ip.Split('.');
                    if (ips.Length == 4 || ips.Length == 6)
                    {
                        if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                            return true;
                        else
                            return false;
                    }
                    else
                        return false;
                }
                else
                    return false;
            }
      

  2.   

    1楼的方法有点搞笑,既然用了正则,那就一个正则搞定就是了,何必还要再拆分判断C#中提供了IPAddress类,可以直接判断
    IPAddress ip;
    if (IPAddress.TryParse(textBox1.Text, out ip))
    {
        MessageBox.Show("合法!");
    }
    else
    {
        MessageBox.Show("不合法!");
    }
      

  3.   

    上面的只是合法性检查,至于有效性检查,Ping一下吧
      

  4.   

    楼上的那个正则表达式, 
    我有个特例:
    127.1你试试ping这个地址, 可以正常解析成本机的。
    可是不能通过你的正则表达式。
      

  5.   

    ctrl+.
    在没有名字空间的类上面按。
      

  6.   

    增加命名空间
    using System.Text.RegularExpressions;
      

  7.   

    直接用IPAddress类。很方便就可以做到。
      

  8.   

    有效性 不等于 合法性。还是要ping 吧。
      

  9.   

    问题是那个类怎么调用呀,它提示错误 1 找不到类型或命名空间名称“IPAddress”(是否缺少 using 指令或程序集引用?) 2 当前上下文中不存在名称“IPAddress” F
      

  10.   

    我知道了,是要引用useing System.Net; 但是我觉得这种方法判断有个问题,就是我随便输入一个数比如123 它都判断合法~~~~晕
      

  11.   


    123 会被认为是 0.0.0.123如果想严格限定格式,用正则判断吧using System.Text.RegularExpressions;Regex reg = new Regex(@"(?n)^(([1-9]?[0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))$");
    if (reg.IsMatch(textBox1.Text))
    {
        MessageBox.Show("合法!");
    }
    else
    {
        MessageBox.Show("不合法!");
    }
      

  12.   

    public static bool Validate(string IP)
    {
    if (string.IsNullOrEmpty(IP))
    return false; return Regex.IsMatch(IP, @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
    }
      

  13.   

    发你一套操作IP的类using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using HKH.Common;namespace HKH.Common.Network
    {
    /// <summary>
    /// 
    /// </summary>
    /// <res> immutable class </res>
    public class IPAddress : INullable
    {
    protected const int IPLENGTH = 4;
    protected byte[] parts = new byte[4]; public IPAddress(byte p1, byte p2, byte p3, byte p4)
    {
    parts[0] = p1;
    parts[1] = p2;
    parts[2] = p3;
    parts[3] = p4;
    } public IPAddress(byte[] ip)
    :this(ip[0],ip[1],ip[2],ip[3])
    {
    } public static IPAddress Null
    {
    get { return NullIPAddress.Instance; }
    } public static bool Validate(string IP)
    {
    if (string.IsNullOrEmpty(IP))
    return false; return Regex.IsMatch(IP, @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
    } public static byte[] Split(string sIP)
    {
    if (!Validate(sIP))
    throw new IPBadFormatException(); byte[] ip = new byte[IPLENGTH];
    string[] sParts = sIP.Split('.'); for (int i = 0; i < IPLENGTH; i++)
    ip[i] = byte.Parse(sParts[i]); return ip;
    } public virtual uint TransferToUINT()
    {
    return MakeLong(MakeWord(parts[3], parts[2]), MakeWord(parts[1], parts[0]));
    } public virtual int Compare(IPAddress IP)
    {
    return (int)(this.TransferToUINT() - IP.TransferToUINT());
    } #region Protected Methods protected static ushort MakeWord(byte bLow, byte bHigh)
    {
    return ((ushort)(((byte)(bLow)) | ((ushort)((byte)(bHigh))) << 8));
    } protected static uint MakeLong(ushort bLow, ushort bHigh)
    {
    return ((uint)(((ushort)(bLow)) | ((uint)((ushort)(bHigh))) << 16));
    } #endregion #region Base Overrides public override bool Equals(object obj)
    {
    if (obj == null)
    return false; IPAddress ip = obj as IPAddress;
    if (ip == null)
    return false; return Compare(ip) == 0;
    } public override int GetHashCode()
    {
    return this.ToString().GetHashCode();
    } public override string ToString()
    {
    StringBuilder builder = new StringBuilder();
    foreach (byte part in parts)
    {
    builder.Append(part);
    builder.Append(Constants.Point);
    }
    builder.Remove(builder.Length - 1, 1); return builder.ToString();
    }
    #endregion #region INullable Members public virtual bool IsNull
    {
    get { return false; }
    } #endregion
    } # region Null IPAddress Class internal sealed class NullIPAddress : IPAddress
    {
    private const string emptyIP = "0.0.0.0"; private static NullIPAddress self = new NullIPAddress(); //Should Never use it directly
    private NullIPAddress()
    : base(0,0,0,0)
    {
    } public static IPAddress Instance
    {
    get { return self; }
    } #region Base Class Overrides public override uint TransferToUINT()
    {
    return 0;
    } public override string ToString()
    {
    return emptyIP;
    } public override bool IsNull
    {
    get { return true; }
    } #endregion
    } #endregion #region IPBadFormatException Class public class IPBadFormatException : ApplicationException
    {
    private const string ErrorMSG = "Bad Format IP Address!"; /// <summary>
    /// do not allow creation of exception with no message
    /// </summary>
    public IPBadFormatException()
    : base(ErrorMSG)
    {
    } /// <summary>
    /// Constructor takes problem message and caught exception
    /// invokes constructor on ApplicationException
    /// </summary>
    public IPBadFormatException(Exception ex)
    : base(ErrorMSG, ex)
    {
    }
    } #endregion}
      

  14.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using HKH.Common;namespace HKH.Common.Network
    {
    public class IPRange
    {
    #region Private Variables private IPAddress startIP = IPAddress.Null;
    private IPAddress endIP = IPAddress.Null; #endregion #region Constructor
    public IPRange(IPAddress startIP, IPAddress endIP)
    {
    this.startIP = startIP;
    this.endIP = endIP;
    } #endregion #region Properties
    public IPAddress StartIP
    {
    get { return startIP; }
    } public IPAddress EndIP
    {
    get { return endIP; }
    }
    #endregion #region Methods
    public bool IsInclude(IPAddress ip)
    {
    return !(startIP.Compare(ip) > 0 || endIP.Compare(ip) < 0);
    } public bool HasIntersection(IPRange ipRange)
    {
    return this.IsInclude(ipRange.startIP) || this.IsInclude(ipRange.endIP);
    } public IPRange GetIntersection(IPRange ipRange)
    {
    if (!HasIntersection(ipRange))
    throw new InvalidOperationException("The IPRanges don't have Intersection."); return new IPRange(this.IsInclude(ipRange.startIP) ? ipRange.startIP : this.startIP,
    this.IsInclude(ipRange.endIP) ? ipRange.endIP : this.endIP);
    } public IPRange Merge(IPRange ipRange)
    {
    if (!HasIntersection(ipRange))
    throw new InvalidOperationException("The IPRanges don't have Intersection."); return new IPRange(this.IsInclude(ipRange.startIP) ? this.startIP : ipRange.startIP,
    this.IsInclude(ipRange.endIP) ? this.endIP : ipRange.endIP);
    }
    #endregion #region Base Overrides public override bool Equals(object obj)
    {
    if (obj == null)
    return false; IPRange ipRange = obj as IPRange;
    if (ipRange == null)
    return false; return this.startIP.Equals(ipRange.startIP) && this.endIP.Equals(ipRange.endIP);
    } public override int GetHashCode()
    {
    return this.ToString().GetHashCode();
    } public override string ToString()
    {
    StringBuilder builder = new StringBuilder(); builder.Append(startIP.ToString());
    builder.Append(Constants.WhiteSpace);
    builder.Append(Constants.Minus);
    builder.Append(Constants.WhiteSpace);
    builder.Append(endIP.ToString()); return builder.ToString();
    }
    #endregion }
    }
      

  15.   

    让我修改一下2楼的代码:IPAddress ip;
    if (IPAddress.TryParse(textBox1.Text, out ip) && textBox1.Text.Split('.').Length == 4)
    {
        MessageBox.Show("合法!");
    }
    else
    {
        MessageBox.Show("不合法!");
    }