zs
2024-11-20 d4a7f52a5a4c8d4acb5af9d29e10856eb8a557c2
DataCapture_MA/Form1.cs
@@ -15,6 +15,8 @@
using DataCapture_MA.Entity;
using DataCapture_MA.DataHnadle;
using DataCapture_MA.AutoMapperConfig;
using System.IO;
using IWshRuntimeLibrary;
namespace DataCapture_MA
{
@@ -27,23 +29,65 @@
        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;
                }
            }
            }
            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.Mapper.Initialize((cfg) => {
            }
            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(); // 启动程序
            // 删除一个月前的数据
            new Thread(AutoDeleteHistoryDate.Handler).Start();
        }
        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()
@@ -80,6 +124,10 @@
        private void start_connect_Click(object sender, EventArgs e)
        {
            StartReceive();
        }
        public void StartReceive()
        {
            try
            {
                if (skt != null)
@@ -114,7 +162,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;