检查IP地址的前X位数

Eye*_*arp 0 c# ip digits

我的目标是在用户的本地IP无法启动时执行代码10.80.

我找不到一种不容易出错的方法,例如:

这是我必须得到的IP:

        IPHostEntry host;
        string localIP = "?";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily.ToString() == "InterNetwork")
            {
                localIP = ip.ToString();
            }
        }
        iplabel.Text = localIP;
Run Code Online (Sandbox Code Playgroud)

然后我尝试将其转换为a int来检查它是否是<或>:

string ipstring = iplabel.Text.Replace(".", "");
int ipnum = int.Parse(ipstring);
if (ipnum > 1080000000 && ipnum < 1080255255)
{//stuff}
Run Code Online (Sandbox Code Playgroud)

但问题是,如果有一个2位数的IP值,例如10.80.22.23它将无法正常工作,因为它检查的数字大于该范围.

有没有更好的解决方案来检查C#中int或IP地址的前x位数?

Fla*_*ter 6

你有没有尝试过:

bool IsCorrectIP = ( ipstring.StartsWith("10.80.") );
Run Code Online (Sandbox Code Playgroud)

对不起,如果答案太简洁了.但那应该可以解决手头的问题.