示例#1
0
        private (bool, string) ParseIp(IpDto ipDto)
        {
            string _BuildIp(string _ip)
            {
                var m = ipRe.Match(_ip ?? "");

                return(m.Success
                    ? string.Join(
                           ".",
                           m.Groups.OfType <ReGroup>().Skip(1)
                           .SelectMany(g => g.Captures.OfType <Capture>())
                           .Select(c => int.Parse(c.Value).ToString()))
                    : null);
            }

            if (!string.IsNullOrEmpty(ipDto.IpOrSubnet))
            {
                var ip = _BuildIp(ipDto.IpOrSubnet);
                if (ip == null)
                {
                    return(false, null);
                }
                var mask = IpDto.IpMask(int.TryParse(ipDto.SubnetMask, out var ipMask) ? ipMask : 32);
                return(true, $"{ip}/{mask}");
            }

            var from = _BuildIp(ipDto.From);
            var to   = _BuildIp(ipDto.To);

            return(from != null && to != null
                ? (true, $"{from}-{to}")
                : (false, null));
        }
示例#2
0
        private void BtnOk_Click(object sender, System.EventArgs e)
        {
            if (rbtnIpOrSubnet.Checked)
            {
                var m = ipOrSubnetRe.Match(tboxIpOrSubnet.Text);
                if (m.Success)
                {
                    Ip = new IPModel
                    {
                        IpOrSubnet = m.Groups["ip"].Value,
                        SubnetMask = m.Groups["mask"].Value
                    };
                    return;
                }
            }
            if (rbtnIpInterval.Checked)
            {
                var m1 = ipRe.Match(tboxIpIntervalFrom.Text);
                var m2 = ipRe.Match(tboxIpIntervalTo.Text);
                if (m1.Success && m2.Success &&
                    IpDto.ToInt32(tboxIpIntervalTo.Text) > IpDto.ToInt32(tboxIpIntervalFrom.Text))
                {
                    Ip = new IPModel
                    {
                        From = tboxIpIntervalFrom.Text,
                        To   = tboxIpIntervalTo.Text
                    };
                    return;
                }
            }

            MessageBox.Show(
                "Enter a valid Ip configuration", "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            DialogResult = DialogResult.None;
        }