From 4a56931a0372941075d767e9b498a9a8d995e83f Mon Sep 17 00:00:00 2001 From: "SONGE9B3\\song_pd_win11" Date: Tue, 27 Dec 2022 16:57:40 +0800 Subject: [PATCH] perf: update autostart codes --- STranslate/Utils/AutoStart.cs | 79 +++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/STranslate/Utils/AutoStart.cs b/STranslate/Utils/AutoStart.cs index 42447a2..012e2e5 100644 --- a/STranslate/Utils/AutoStart.cs +++ b/STranslate/Utils/AutoStart.cs @@ -8,10 +8,7 @@ namespace STranslate.Utils { public class AutoStart { - private static readonly string StartUpPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); - private static readonly string appPath = Assembly.GetEntryAssembly().Location; - private static readonly string appShortcutPath = Path.Combine(StartUpPath, Path.GetFileNameWithoutExtension(appPath) + ".lnk"); - + #region public method /// /// 设置开机自启 /// @@ -34,13 +31,54 @@ namespace STranslate.Utils { ShortCutDelete(appPath, StartUpPath); } + #endregion + + #region params + /// + /// 开机启动目录 + /// + private static readonly string StartUpPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); + + /// + /// 当前程序二进制文件路径 + /// + private static readonly string appPath = Assembly.GetEntryAssembly().Location; + + /// + /// 组合的开机启动目录中的快捷方式路径 + /// + private static readonly string appShortcutPath = Path.Combine(StartUpPath, Path.GetFileNameWithoutExtension(appPath) + ".lnk"); + #endregion + + #region native method + /// + /// 获取快捷方式中的目标(可执行文件的绝对路径) + /// + /// 快捷方式的绝对路径 + /// + /// 需引入 COM 组件 Windows Script Host Object Model + private static string GetAppPathViaShortCut(string shortCutPath) + { + try + { + WshShell shell = new WshShell(); + IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortCutPath); + //快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath; + //快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory; + return shortct.TargetPath; + } + catch + { + return null; + } + } /// /// 获取指定文件夹下的所有快捷方式(不包括子文件夹) /// /// 目标文件夹(绝对路径) /// - public static List GetDirectoryFileList(string target) + private static List GetDirectoryFileList(string target) { List list = new List(); list.Clear(); @@ -62,7 +100,7 @@ namespace STranslate.Utils /// 快捷方式目标(可执行文件的绝对路径) /// 目标文件夹(绝对路径) /// - public static bool ShortCutExist(string path, string target) + private static bool ShortCutExist(string path, string target) { bool Result = false; List list = GetDirectoryFileList(target); @@ -82,7 +120,7 @@ namespace STranslate.Utils /// 快捷方式目标(可执行文件的绝对路径) /// 目标文件夹(绝对路径) /// - public static bool ShortCutDelete(string path, string target) + private static bool ShortCutDelete(string path, string target) { bool Result = false; List list = GetDirectoryFileList(target); @@ -97,9 +135,9 @@ namespace STranslate.Utils return Result; } /// - /// 为本程序创建一个快捷方式。 + /// 为本程序创建一个开机启动快捷方式 /// - public static bool ShortCutCreate() + private static bool ShortCutCreate() { bool Result = false; try @@ -121,27 +159,6 @@ namespace STranslate.Utils } return Result; } - - /// - /// 获取快捷方式中的目标(可执行文件的绝对路径) - /// - /// 快捷方式的绝对路径 - /// - /// 需引入 COM 组件 Windows Script Host Object Model - public static string GetAppPathViaShortCut(string shortCutPath) - { - try - { - WshShell shell = new WshShell(); - IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortCutPath); - //快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath; - //快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory; - return shortct.TargetPath; - } - catch - { - return null; - } - } + #endregion } }