From d4a7f52a5a4c8d4acb5af9d29e10856eb8a557c2 Mon Sep 17 00:00:00 2001 From: zs <zhousong@weben-smart.com> Date: 周三, 20 11月 2024 15:37:48 +0800 Subject: [PATCH] 程序开机自启 --- DataCapture_MA/Form1.cs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 53 insertions(+), 5 deletions(-) diff --git a/DataCapture_MA/Form1.cs b/DataCapture_MA/Form1.cs index 9a7579b..d025d3d 100644 --- a/DataCapture_MA/Form1.cs +++ b/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) // 鍙樉绀篒Pv4鍦板潃 { 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} 鐨処P鍦板潃锛屽皢榛樿閫変腑绗竴涓狪P銆�", "鎻愮ず", 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() @@ -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, "寮�鍚疷DP閫氫俊鎴愬姛" + iPEndPoint.ToString()); - ipSelect.Enabled = false; + //ipSelect.Enabled = false; local_port.Enabled = false; to_ipTxt.Enabled = false; to_portTxt.Enabled = false; @@ -124,7 +172,7 @@ colse_connect.Enabled = true; r_showBox.Text += "鏁版嵁鎺ユ敹宸插惎鍔╘n"; } - catch(Exception ex) + catch (Exception ex) { Log4NetHelper.WriteInfoLog(LogType.Receive, "寮�鍚疷DP閫氫俊澶辫触" + ex); MessageBox.Show(ex.Message); -- Gitblit v1.9.3