| | |
| | | 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; |
| | |
| | | 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) |
| | | { |
| | |
| | | shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); |
| | | shortcut.Save(); |
| | | } |
| | | |
| | | |
| | | private void ReceiveHandle() |
| | | { |
| | | //接收数据处理线程 |