zs
2024-11-21 6e7d363e82b03f7b99271d8e4d516f2d8e427ca3
DataCapture_MA/Form1.cs
@@ -15,11 +15,14 @@
using DataCapture_MA.Entity;
using DataCapture_MA.DataHnadle;
using DataCapture_MA.AutoMapperConfig;
using System.IO;
using IWshRuntimeLibrary;
namespace DataCapture_MA
{
    public partial class CaptrueForm : Form
    {
        private CancellationTokenSource _cts = new CancellationTokenSource();
        bool RunningFlag = false;
        Socket skt = WZ.Useful.Commons.NetworkUtil.CreateUdpSocket();
        EndPoint RemotePoint = null;
@@ -27,25 +30,83 @@
        public CaptrueForm()
        {
            InitializeComponent();
            var hostName = Dns.GetHostName();
            var ips = Dns.GetHostAddresses(hostName);
            string targetSubnet = "10.133.13"; // 目标网段
            bool foundTargetSubnet = false;
            foreach (var ip in ips)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork) // 只显示IPv4地址
                {
                    ipSelect.Items.Add(ip);
                    // 检查是否包含目标网段
                    if (ip.ToString().StartsWith(targetSubnet))
                    {
                        ipSelect.SelectedItem = ip; // 选中匹配的 IP
                        foundTargetSubnet = true;
                    }
                }
            }
            ipSelect.SelectedIndex = 0;
            to_ipTxt.Text = ipSelect.SelectedItem.ToString();
            AutoMapper.Mapper.Initialize((cfg) => {
            if (!foundTargetSubnet && ipSelect.Items.Count > 0)
            {
                //MessageBox.Show($"未找到网段为 {targetSubnet} 的IP地址,将默认选中第一个IP。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ipSelect.SelectedIndex = 0;
            }
            to_ipTxt.Text = ipSelect.SelectedItem?.ToString();
            // 初始化 AutoMapper
            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.AddProfile<MapperConfiguration>();
            });
            // 创建快捷方式
            string shortcutName = "MaDataCapture"; // 快捷方式名称
            string targetPath = Application.ExecutablePath; // 程序路径
            string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); // 启动文件夹路径
            CreateShortcut(shortcutName, targetPath, startupPath); // 创建快捷方式
            StartReceive(); // 启动程序
            CreateDatabase();  // 初始化数据库
            // 删除一个月前的数据
            new Thread(AutoDeleteHistoryDate.Handler).Start();
            this.FormClosing += CaptrueForm_FormClosing;
            this.FormClosed += CaptrueForm_FormClosed;
        }
        private void CaptrueForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 通知后台任务取消
            _cts.Cancel();
            // 确保任务完成
            Task.WaitAll(Task.WhenAll());
        }
        private void CaptrueForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // 确保程序退出
            Environment.Exit(0);
        }
        public static void CreateShortcut(string shortcutName, string targetPath, string startupPath)
        {
            string shortcutPath = Path.Combine(startupPath, shortcutName + ".lnk");
            if (System.IO.File.Exists(shortcutPath))
                return;
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
            shortcut.TargetPath = targetPath;
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
            shortcut.Save();
        }
        private void ReceiveHandle()
        {
            //接收数据处理线程  
@@ -79,6 +140,10 @@
        }
        private void start_connect_Click(object sender, EventArgs e)
        {
            StartReceive();
        }
        public void StartReceive()
        {
            try
            {
@@ -114,7 +179,7 @@
                Log4NetHelper.WriteInfoLog(LogType.Receive, "开启UDP通信成功" + iPEndPoint.ToString());
                ipSelect.Enabled = false;
                //ipSelect.Enabled = false;
                local_port.Enabled = false;
                to_ipTxt.Enabled = false;
                to_portTxt.Enabled = false;
@@ -124,7 +189,7 @@
                colse_connect.Enabled = true;
                r_showBox.Text += "数据接收已启动\n";
            }
            catch(Exception ex)
            catch (Exception ex)
            {
                Log4NetHelper.WriteInfoLog(LogType.Receive, "开启UDP通信失败" + ex);
                MessageBox.Show(ex.Message);