1public class IPRegion
2 {
3 public static bool IPInRegion(string beginIP, string endIP, string inputIP)
4 {
5 IPAddress _beginip;
6 IPAddress _endip;
7 IPAddress _inputip;
8 try
9 {
10 _beginip = IPAddress.Parse(beginIP);
11 _endip = IPAddress.Parse(endIP);
12 _inputip = IPAddress.Parse(inputIP);
13 }
14 catch (Exception ex)
15 {
16 ErrorHandler.Handle(ex);
17 return false;
18 }
19 if (_beginip == null || _endip == null || _inputip == null) return false;
20
21 byte[] _beginbyte = _beginip.GetAddressBytes();
22 Int64 _beginint = ConvertIPToInt(_beginbyte);
23 byte[] _endbyte = _endip.GetAddressBytes();
24 Int64 _endint = ConvertIPToInt(_endbyte);
25
26 if (_beginint > _endint) return false;
27
28 byte[] _inputtype = _inputip.GetAddressBytes();
29 Int64 _inputint = ConvertIPToInt(_inputtype);
30
31 return (_inputint <= _endint && _inputint >= _beginint);
32 }
33
34 protected static Int64 ConvertIPToInt(byte[] _bytes)
35 {
36 if (_bytes.Length != 4) return 0;
37 string _intstring = "";
38 for (int i = 0; i < _bytes.Length; i++)
39 {
40 _intstring += FormatIPPart(_bytes[i].ToString());
41 }
42 Int64 _outputint;
43 return !Int64.TryParse(_intstring, out _outputint) ? 0 : _outputint;
44 }
45
46 protected static string FormatIPPart(string _ippart)
47 {
48 Regex _regx = new Regex(@"^\d{1,3}$");
49 if (!_regx.IsMatch(_ippart)) return "000";
50 while (_ippart.Length < 3)
51 {
52 _ippart = "0" + _ippart;
53 }
54 return _ippart;
55 }
56 }
2 {
3 public static bool IPInRegion(string beginIP, string endIP, string inputIP)
4 {
5 IPAddress _beginip;
6 IPAddress _endip;
7 IPAddress _inputip;
8 try
9 {
10 _beginip = IPAddress.Parse(beginIP);
11 _endip = IPAddress.Parse(endIP);
12 _inputip = IPAddress.Parse(inputIP);
13 }
14 catch (Exception ex)
15 {
16 ErrorHandler.Handle(ex);
17 return false;
18 }
19 if (_beginip == null || _endip == null || _inputip == null) return false;
20
21 byte[] _beginbyte = _beginip.GetAddressBytes();
22 Int64 _beginint = ConvertIPToInt(_beginbyte);
23 byte[] _endbyte = _endip.GetAddressBytes();
24 Int64 _endint = ConvertIPToInt(_endbyte);
25
26 if (_beginint > _endint) return false;
27
28 byte[] _inputtype = _inputip.GetAddressBytes();
29 Int64 _inputint = ConvertIPToInt(_inputtype);
30
31 return (_inputint <= _endint && _inputint >= _beginint);
32 }
33
34 protected static Int64 ConvertIPToInt(byte[] _bytes)
35 {
36 if (_bytes.Length != 4) return 0;
37 string _intstring = "";
38 for (int i = 0; i < _bytes.Length; i++)
39 {
40 _intstring += FormatIPPart(_bytes[i].ToString());
41 }
42 Int64 _outputint;
43 return !Int64.TryParse(_intstring, out _outputint) ? 0 : _outputint;
44 }
45
46 protected static string FormatIPPart(string _ippart)
47 {
48 Regex _regx = new Regex(@"^\d{1,3}$");
49 if (!_regx.IsMatch(_ippart)) return "000";
50 while (_ippart.Length < 3)
51 {
52 _ippart = "0" + _ippart;
53 }
54 return _ippart;
55 }
56 }