Newer
Older
IRIS_COLLECT / IOM_cs / insertForm / sysSetting / CtrlServer.cs
yangqianqian on 29 Dec 2020 2 KB first
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IOM_cs.insertForm.data.dto;
using System.Text.RegularExpressions;

namespace IOM_cs.insertForm.sysSetting
{
    public partial class CtrlServer : UserControl
    {
        public CtrlServer()
        {
            InitializeComponent();            
        }

        private void reFresh() {
            textBoxServerIp.Text = ConfigHelper.GetAppConfig("ServerIp");
            textBoxServerPort.Text = ConfigHelper.GetAppConfig("ServerPort"); 
        }
       

        private void CtrlServer_Load(object sender, EventArgs e)
        {
            reFresh();
        }

        private void btn_cancle_Click(object sender, EventArgs e)
        {
            reFresh();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            String ServerIp = textBoxServerIp.Text;
            String ServerPort = textBoxServerPort.Text;

            if (!IsIPAddress(ServerIp))
            {
                MessageBox.Show("请输入正确的IP", "提示");
                return;
            }
            else if (!IsIPPort(ServerPort))
            {
                MessageBox.Show("请输入正确的端口", "提示");
                return;
            }

            if (MessageBox.Show("确定保存修改?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                ConfigHelper.UpdateAppConfig("ServerIp", ServerIp);
                ConfigHelper.UpdateAppConfig("ServerPort", ServerPort);

                reFresh();

                MessageBox.Show("保存成功", "提示");
            }
        }

        public static bool IsIPAddress(string ip)
        {

            if (string.IsNullOrEmpty(ip) || ip.Length < 7 || ip.Length > 15) return false;

            string regformat = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
            Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
            return regex.IsMatch(ip);

        }
        
        public static bool IsIPPort(string port)
        {
            bool isPort = false;
            int portNum;
            isPort = Int32.TryParse(port, out portNum);
            if (isPort && portNum >= 0 && portNum <= 65535)
            {
                isPort = true;
            }
            else
            {
                isPort = false;
            }

            return isPort;
        }

       
    }
}