zs
2024-11-21 b3dcb81d8ad46322740e5d6f71765c66ed99add8
DataCapture_MA/Form1.cs
@@ -17,11 +17,13 @@
using DataCapture_MA.AutoMapperConfig;
using System.IO;
using IWshRuntimeLibrary;
using Microsoft.Win32;
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;
@@ -52,10 +54,9 @@
            if (!foundTargetSubnet && ipSelect.Items.Count > 0)
            {
                MessageBox.Show($"未找到网段为 {targetSubnet} 的IP地址,将默认选中第一个IP。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //MessageBox.Show($"未找到网段为 {targetSubnet} 的IP地址,将默认选中第一个IP。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ipSelect.SelectedIndex = 0;
            }
            to_ipTxt.Text = ipSelect.SelectedItem?.ToString();
            // 初始化 AutoMapper
@@ -64,18 +65,68 @@
                cfg.AddProfile<MapperConfiguration>();
            });
            // 创建快捷方式
            string shortcutName = "MaDataCapture"; // 快捷方式名称
            string targetPath = Application.ExecutablePath; // 程序路径
            string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); // 启动文件夹路径
            CreateShortcut(shortcutName, targetPath, startupPath); // 创建快捷方式
            ////创建快捷方式
            //string shortcutName = "MaDataCapture"; // 快捷方式名称
            //string targetPath = Application.ExecutablePath; // 程序路径
            //string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); // 启动文件夹路径
            //CreateShortcut(shortcutName, targetPath, startupPath); // 创建快捷方式
            SetRegistryAutoStart("MaDataCapture", Application.ExecutablePath);
            StartReceive(); // 启动程序
            CreateDatabase();  // 初始化数据库
            Log4NetHelper.WriteErrorLog(LogType.Receive, "程序启动:" + DateTime.Now);
            // 删除一个月前的数据
            new Thread(AutoDeleteHistoryDate.Handler).Start();
            this.FormClosing += CaptrueForm_FormClosing;
            this.FormClosed += CaptrueForm_FormClosed;
        }
        /// <summary>
        /// 设置注册表实现自启动
        /// </summary>
        /// <param name="appName">应用程序名称</param>
        /// <param name="appPath">应用程序路径</param>
        private void SetRegistryAutoStart(string appName, string appPath)
        {
            try
            {
                // 打开注册表路径
                RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
                if (key == null)
                {
                    //throw new Exception("无法访问注册表路径!");
                    Log4NetHelper.WriteErrorLog(LogType.Receive, "无法访问注册表路径:" + DateTime.Now);
                }
                // 设置注册表键值
                key.SetValue(appName, appPath);
                key.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"设置开机自启动失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void CaptrueForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Log4NetHelper.WriteErrorLog(LogType.Receive, "程序关闭:" + DateTime.Now);
            // 通知后台任务取消
            _cts.Cancel();
            // 确保任务完成
            Task.WaitAll(Task.WhenAll());
        }
        private void CaptrueForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Log4NetHelper.WriteErrorLog(LogType.Receive, "程序退出:" + DateTime.Now);
            // 确保程序退出
            Environment.Exit(0);
        }
        public static void CreateShortcut(string shortcutName, string targetPath, string startupPath)
        {
@@ -89,7 +140,7 @@
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
            shortcut.Save();
        }
        private void ReceiveHandle()
        {
            //接收数据处理线程