From 9b2fbc6e4922fe770186721b3155ca98daacfac0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Fri, 24 Feb 2023 10:21:18 +0800 Subject: [PATCH] perf: create desktop shorcut at first time --- STranslate/Helper/ConfigHelper.cs | 1 + .../{StartupHelper.cs => ShortcutHelper.cs} | 31 ++- STranslate/Model/ConfigModel.cs | 188 +++++++++--------- STranslate/Properties/AssemblyInfo.cs | 4 +- STranslate/STranslate.csproj | 2 +- STranslate/Util/Util.cs | 29 ++- STranslate/ViewModel/SettingsVM.cs | 8 +- 7 files changed, 156 insertions(+), 107 deletions(-) rename STranslate/Helper/{StartupHelper.cs => ShortcutHelper.cs} (83%) diff --git a/STranslate/Helper/ConfigHelper.cs b/STranslate/Helper/ConfigHelper.cs index 9866b56..910c04e 100644 --- a/STranslate/Helper/ConfigHelper.cs +++ b/STranslate/Helper/ConfigHelper.cs @@ -34,6 +34,7 @@ namespace STranslate.Helper if (!Directory.Exists(_ApplicationData))//判断是否存在 { Directory.CreateDirectory(_ApplicationData);//创建新路径 + ShortcutHelper.SetDesktopShortcut();//创建桌面快捷方式 } if (!File.Exists(_CnfName))//文件不存在 { diff --git a/STranslate/Helper/StartupHelper.cs b/STranslate/Helper/ShortcutHelper.cs similarity index 83% rename from STranslate/Helper/StartupHelper.cs rename to STranslate/Helper/ShortcutHelper.cs index 96cacb3..ba45df4 100644 --- a/STranslate/Helper/StartupHelper.cs +++ b/STranslate/Helper/ShortcutHelper.cs @@ -6,7 +6,7 @@ using IWshRuntimeLibrary; namespace STranslate.Helper { - public class StartupHelper + public class ShortcutHelper { #region public method @@ -32,6 +32,13 @@ namespace STranslate.Helper { ShortCutDelete(appPath, StartUpPath); } + /// + /// 设置桌面快捷方式 + /// + public static void SetDesktopShortcut() + { + ShortCutCreate(true); + } #endregion #region params @@ -40,6 +47,11 @@ namespace STranslate.Helper /// private static readonly string StartUpPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); + /// + /// 用户桌面目录 + /// + private static readonly string DesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + /// /// 当前程序二进制文件路径 /// @@ -49,6 +61,8 @@ namespace STranslate.Helper /// 组合的开机启动目录中的快捷方式路径 /// private static readonly string appShortcutPath = Path.Combine(StartUpPath, Path.GetFileNameWithoutExtension(appPath) + ".lnk"); + + private static readonly string desktopShortcutPath = Path.Combine(DesktopPath, Path.GetFileNameWithoutExtension(appPath) + ".lnk"); #endregion #region native method @@ -136,18 +150,25 @@ namespace STranslate.Helper return Result; } /// - /// 为本程序创建一个开机启动快捷方式 + /// 为本程序创建一个快捷方式 /// - private static bool ShortCutCreate() + /// 是否为桌面快捷方式 + /// + private static bool ShortCutCreate(bool isDesktop = false) { bool Result = false; try { - ShortCutDelete(appPath, StartUpPath); + if (!isDesktop) + ShortCutDelete(appPath, StartUpPath); var shellType = Type.GetTypeFromProgID("WScript.Shell"); dynamic shell = Activator.CreateInstance(shellType); - var shortcut = shell.CreateShortcut(appShortcutPath); + IWshShortcut shortcut; + if (!isDesktop) + shortcut = shell.CreateShortcut(appShortcutPath); + else + shortcut = shell.CreateShortcut(desktopShortcutPath); shortcut.TargetPath = Assembly.GetEntryAssembly().Location; shortcut.Arguments = string.Empty; shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs index 57d009e..8242723 100644 --- a/STranslate/Model/ConfigModel.cs +++ b/STranslate/Model/ConfigModel.cs @@ -7,8 +7,102 @@ using System.Threading.Tasks; namespace STranslate.Model { + + public class Hotkeys + { + [JsonProperty("inputTranslate")] + public InputTranslate InputTranslate { get; set; } + + [JsonProperty("crosswordTranslate")] + public CrosswordTranslate CrosswordTranslate { get; set; } + + [JsonProperty("screenShotTranslate")] + public ScreenShotTranslate ScreenShotTranslate { get; set; } + + [JsonProperty("openMainWindow")] + public OpenMainWindow OpenMainWindow { get; set; } + } + public class InputTranslate + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } + public class CrosswordTranslate + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } + public class ScreenShotTranslate + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } + public class OpenMainWindow + { + public byte Modifiers { get; set; } + public int Key { get; set; } + public String Text { get; set; } + public bool Conflict { get; set; } + } + public class Server + { + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("api")] + public string Api { get; set; } + } public class ConfigModel { + /// + /// 最大历史记录数量 + /// + [JsonProperty("maxHistoryCount")] + public int MaxHistoryCount { get; set; } + /// + /// 自动识别语种标度 + /// + [JsonProperty("autoScale")] + public double AutoScale { get; set; } + /// + /// 取词间隔 + /// + [JsonProperty("wordPickupInterval")] + public double WordPickupInterval { get; set; } + /// + /// 是否亮色模式 + /// + [JsonProperty("isBright")] + public bool IsBright { get; set; } + + [JsonProperty("sourceLanguage")] + public string SourceLanguage { get; set; } + + [JsonProperty("targetLanguage")] + public string TargetLanguage { get; set; } + + [JsonProperty("selectServer")] + public int SelectServer { get; set; } + + /// + /// 服务 + /// + [JsonProperty("servers")] + public Server[] Servers { get; set; } + + /// + /// 热键 + /// + [JsonProperty("hotkeys")] + public Hotkeys Hotkeys { get; set; } + + public ConfigModel() { } @@ -75,99 +169,5 @@ namespace STranslate.Model } }; } - - /// - /// 最大历史记录数量 - /// - [JsonProperty("maxHistoryCount")] - public int MaxHistoryCount { get; set; } - /// - /// 自动识别语种标度 - /// - [JsonProperty("autoScale")] - public double AutoScale { get; set; } - /// - /// 取词间隔 - /// - [JsonProperty("wordPickupInterval")] - public double WordPickupInterval { get; set; } - /// - /// 是否亮色模式 - /// - [JsonProperty("isBright")] - public bool IsBright { get; set; } - - [JsonProperty("sourceLanguage")] - public string SourceLanguage { get; set; } - - [JsonProperty("targetLanguage")] - public string TargetLanguage { get; set; } - - [JsonProperty("selectServer")] - public int SelectServer { get; set; } - - /// - /// 服务 - /// - [JsonProperty("servers")] - public Server[] Servers { get; set; } - - /// - /// 热键 - /// - [JsonProperty("hotkeys")] - public Hotkeys Hotkeys { get; set; } - - } - - public class Hotkeys - { - [JsonProperty("inputTranslate")] - public InputTranslate InputTranslate { get; set; } - - [JsonProperty("crosswordTranslate")] - public CrosswordTranslate CrosswordTranslate { get; set; } - - [JsonProperty("screenShotTranslate")] - public ScreenShotTranslate ScreenShotTranslate { get; set; } - - [JsonProperty("openMainWindow")] - public OpenMainWindow OpenMainWindow { get; set; } - } - public class InputTranslate - { - public byte Modifiers { get; set; } - public int Key { get; set; } - public String Text { get; set; } - public bool Conflict { get; set; } - } - public class CrosswordTranslate - { - public byte Modifiers { get; set; } - public int Key { get; set; } - public String Text { get; set; } - public bool Conflict { get; set; } - } - public class ScreenShotTranslate - { - public byte Modifiers { get; set; } - public int Key { get; set; } - public String Text { get; set; } - public bool Conflict { get; set; } - } - public class OpenMainWindow - { - public byte Modifiers { get; set; } - public int Key { get; set; } - public String Text { get; set; } - public bool Conflict { get; set; } - } - public class Server - { - [JsonProperty("name")] - public string Name { get; set; } - - [JsonProperty("api")] - public string Api { get; set; } } } diff --git a/STranslate/Properties/AssemblyInfo.cs b/STranslate/Properties/AssemblyInfo.cs index 7fe7b94..247529b 100644 --- a/STranslate/Properties/AssemblyInfo.cs +++ b/STranslate/Properties/AssemblyInfo.cs @@ -47,6 +47,6 @@ using System.Windows; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.2.2.0")] -[assembly: AssemblyFileVersion("0.2.2.0")] +[assembly: AssemblyVersion("0.2.3.0")] +[assembly: AssemblyFileVersion("0.2.3.0")] [assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")] \ No newline at end of file diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index ffc4022..3395abc 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -124,7 +124,7 @@ - + diff --git a/STranslate/Util/Util.cs b/STranslate/Util/Util.cs index 7b0f417..ab50751 100644 --- a/STranslate/Util/Util.cs +++ b/STranslate/Util/Util.cs @@ -1,10 +1,12 @@ -using Newtonsoft.Json; +using IWshRuntimeLibrary; +using Newtonsoft.Json; using STranslate.Model; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Net.Http; +using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; @@ -295,5 +297,30 @@ namespace STranslate.Util } } #endregion + + #region Shortcut + public static void CreateShortcut() + { + string deskTop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop); + string dirPath = System.Environment.CurrentDirectory; + string exePath = Assembly.GetExecutingAssembly().Location; + System.Diagnostics.FileVersionInfo exeInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(exePath); + if (System.IO.File.Exists(string.Format(@"{0}\STranslate.lnk", deskTop))) + { + System.IO.File.Delete(string.Format(@"{0}\STranslate.lnk",deskTop));//删除原来的桌面快捷键方式 + return; + } + WshShell shell = new WshShell(); + IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + "STranslate.lnk"); + shortcut.TargetPath = @exePath; //目标文件 + shortcut.WorkingDirectory = dirPath; //目标文件夹 + shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】 + shortcut.Description = "自动更新程序"; //描述 + shortcut.IconLocation = string.Format(@"{0}\64.ico", dirPath); //快捷方式图标 + shortcut.Arguments = ""; + shortcut.Hotkey = "SHIFT+DELETE"; // 快捷键 + shortcut.Save(); + } + #endregion } } diff --git a/STranslate/ViewModel/SettingsVM.cs b/STranslate/ViewModel/SettingsVM.cs index cad32bd..ab8a920 100644 --- a/STranslate/ViewModel/SettingsVM.cs +++ b/STranslate/ViewModel/SettingsVM.cs @@ -14,7 +14,7 @@ namespace STranslate.ViewModel { public SettingsVM() { - IsStartup = StartupHelper.IsStartup(); + IsStartup = ShortcutHelper.IsStartup(); Version = HandleVersion(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() ?? "1.0.0.0"); @@ -63,9 +63,9 @@ namespace STranslate.ViewModel StartupCmd = new RelayCommand((_) => true, (_) => { - if (StartupHelper.IsStartup()) StartupHelper.UnSetStartup(); - else StartupHelper.SetStartup(); - IsStartup = StartupHelper.IsStartup(); + if (ShortcutHelper.IsStartup()) ShortcutHelper.UnSetStartup(); + else ShortcutHelper.SetStartup(); + IsStartup = ShortcutHelper.IsStartup(); }); EscCmd = new RelayCommand((_) => true, (o) => {