From b3dcb81d8ad46322740e5d6f71765c66ed99add8 Mon Sep 17 00:00:00 2001
From: zs <zhousong@weben-smart.com>
Date: 周四, 21 11月 2024 10:38:36 +0800
Subject: [PATCH] 修改为注册表启动

---
 DataCapture_MA/Form1.cs |   44 +++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/DataCapture_MA/Form1.cs b/DataCapture_MA/Form1.cs
index d568930..8472e21 100644
--- a/DataCapture_MA/Form1.cs
+++ b/DataCapture_MA/Form1.cs
@@ -17,6 +17,7 @@
 using DataCapture_MA.AutoMapperConfig;
 using System.IO;
 using IWshRuntimeLibrary;
+using Microsoft.Win32;
 
 namespace DataCapture_MA
 {
@@ -64,23 +65,55 @@
                 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();
 
@@ -90,6 +123,7 @@
 
         private void CaptrueForm_FormClosed(object sender, FormClosedEventArgs e)
         {
+            Log4NetHelper.WriteErrorLog(LogType.Receive, "绋嬪簭閫�鍑�:" + DateTime.Now);
             // 纭繚绋嬪簭閫�鍑�
             Environment.Exit(0);
         }

--
Gitblit v1.9.3