| | |
| | | using DataCapture_MA.Entity; |
| | | using DataCapture_MA.DataHnadle; |
| | | using DataCapture_MA.AutoMapperConfig; |
| | | using System.IO; |
| | | using IWshRuntimeLibrary; |
| | | |
| | | namespace DataCapture_MA |
| | | { |
| | |
| | | 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(); // 启动程序 |
| | | |
| | | // 删除一个月前的数据 |
| | | 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() |
| | |
| | | |
| | | private void start_connect_Click(object sender, EventArgs e) |
| | | { |
| | | StartReceive(); |
| | | } |
| | | public void StartReceive() |
| | | { |
| | | try |
| | | { |
| | | if (skt != null) |
| | |
| | | |
| | | |
| | | 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; |
| | |
| | | colse_connect.Enabled = true; |
| | | r_showBox.Text += "数据接收已启动\n"; |
| | | } |
| | | catch(Exception ex) |
| | | catch (Exception ex) |
| | | { |
| | | Log4NetHelper.WriteInfoLog(LogType.Receive, "开启UDP通信失败" + ex); |
| | | MessageBox.Show(ex.Message); |