perf: create desktop shorcut at first time

main 0.23
DESKTOP-3BO4HSG\ksat 2 years ago
parent 01b9af34dd
commit 9b2fbc6e49

@ -34,6 +34,7 @@ namespace STranslate.Helper
if (!Directory.Exists(_ApplicationData))//判断是否存在 if (!Directory.Exists(_ApplicationData))//判断是否存在
{ {
Directory.CreateDirectory(_ApplicationData);//创建新路径 Directory.CreateDirectory(_ApplicationData);//创建新路径
ShortcutHelper.SetDesktopShortcut();//创建桌面快捷方式
} }
if (!File.Exists(_CnfName))//文件不存在 if (!File.Exists(_CnfName))//文件不存在
{ {

@ -6,7 +6,7 @@ using IWshRuntimeLibrary;
namespace STranslate.Helper namespace STranslate.Helper
{ {
public class StartupHelper public class ShortcutHelper
{ {
#region public method #region public method
@ -32,6 +32,13 @@ namespace STranslate.Helper
{ {
ShortCutDelete(appPath, StartUpPath); ShortCutDelete(appPath, StartUpPath);
} }
/// <summary>
/// 设置桌面快捷方式
/// </summary>
public static void SetDesktopShortcut()
{
ShortCutCreate(true);
}
#endregion #endregion
#region params #region params
@ -40,6 +47,11 @@ namespace STranslate.Helper
/// </summary> /// </summary>
private static readonly string StartUpPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); private static readonly string StartUpPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
/// <summary>
/// 用户桌面目录
/// </summary>
private static readonly string DesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
/// <summary> /// <summary>
/// 当前程序二进制文件路径 /// 当前程序二进制文件路径
/// </summary> /// </summary>
@ -49,6 +61,8 @@ namespace STranslate.Helper
/// 组合的开机启动目录中的快捷方式路径 /// 组合的开机启动目录中的快捷方式路径
/// </summary> /// </summary>
private static readonly string appShortcutPath = Path.Combine(StartUpPath, Path.GetFileNameWithoutExtension(appPath) + ".lnk"); 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 #endregion
#region native method #region native method
@ -136,18 +150,25 @@ namespace STranslate.Helper
return Result; return Result;
} }
/// <summary> /// <summary>
/// 为本程序创建一个开机启动快捷方式 /// 为本程序创建一个快捷方式
/// </summary> /// </summary>
private static bool ShortCutCreate() /// <param name="isDesktop">是否为桌面快捷方式</param>
/// <returns></returns>
private static bool ShortCutCreate(bool isDesktop = false)
{ {
bool Result = false; bool Result = false;
try try
{ {
if (!isDesktop)
ShortCutDelete(appPath, StartUpPath); ShortCutDelete(appPath, StartUpPath);
var shellType = Type.GetTypeFromProgID("WScript.Shell"); var shellType = Type.GetTypeFromProgID("WScript.Shell");
dynamic shell = Activator.CreateInstance(shellType); 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.TargetPath = Assembly.GetEntryAssembly().Location;
shortcut.Arguments = string.Empty; shortcut.Arguments = string.Empty;
shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

@ -7,8 +7,102 @@ using System.Threading.Tasks;
namespace STranslate.Model 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 public class ConfigModel
{ {
/// <summary>
/// 最大历史记录数量
/// </summary>
[JsonProperty("maxHistoryCount")]
public int MaxHistoryCount { get; set; }
/// <summary>
/// 自动识别语种标度
/// </summary>
[JsonProperty("autoScale")]
public double AutoScale { get; set; }
/// <summary>
/// 取词间隔
/// </summary>
[JsonProperty("wordPickupInterval")]
public double WordPickupInterval { get; set; }
/// <summary>
/// 是否亮色模式
/// </summary>
[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; }
/// <summary>
/// 服务
/// </summary>
[JsonProperty("servers")]
public Server[] Servers { get; set; }
/// <summary>
/// 热键
/// </summary>
[JsonProperty("hotkeys")]
public Hotkeys Hotkeys { get; set; }
public ConfigModel() public ConfigModel()
{ {
} }
@ -75,99 +169,5 @@ namespace STranslate.Model
} }
}; };
} }
/// <summary>
/// 最大历史记录数量
/// </summary>
[JsonProperty("maxHistoryCount")]
public int MaxHistoryCount { get; set; }
/// <summary>
/// 自动识别语种标度
/// </summary>
[JsonProperty("autoScale")]
public double AutoScale { get; set; }
/// <summary>
/// 取词间隔
/// </summary>
[JsonProperty("wordPickupInterval")]
public double WordPickupInterval { get; set; }
/// <summary>
/// 是否亮色模式
/// </summary>
[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; }
/// <summary>
/// 服务
/// </summary>
[JsonProperty("servers")]
public Server[] Servers { get; set; }
/// <summary>
/// 热键
/// </summary>
[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; }
} }
} }

@ -47,6 +47,6 @@ using System.Windows;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示: //通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.2.0")] [assembly: AssemblyVersion("0.2.3.0")]
[assembly: AssemblyFileVersion("0.2.2.0")] [assembly: AssemblyFileVersion("0.2.3.0")]
[assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")] [assembly: Guid("CE252DD8-179F-4544-9989-453F5DEA378D")]

@ -124,7 +124,7 @@
<Compile Include="Helper\NativeMethodHelper.cs" /> <Compile Include="Helper\NativeMethodHelper.cs" />
<Compile Include="Helper\Processhelper.cs" /> <Compile Include="Helper\Processhelper.cs" />
<Compile Include="Helper\SqliteHelper.cs" /> <Compile Include="Helper\SqliteHelper.cs" />
<Compile Include="Helper\StartupHelper.cs" /> <Compile Include="Helper\ShortcutHelper.cs" />
<Compile Include="Model\ConfigModel.cs" /> <Compile Include="Model\ConfigModel.cs" />
<Compile Include="Model\SqliteModel.cs" /> <Compile Include="Model\SqliteModel.cs" />
<Compile Include="Model\VisibilityConverter.cs" /> <Compile Include="Model\VisibilityConverter.cs" />

@ -1,10 +1,12 @@
using Newtonsoft.Json; using IWshRuntimeLibrary;
using Newtonsoft.Json;
using STranslate.Model; using STranslate.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -295,5 +297,30 @@ namespace STranslate.Util
} }
} }
#endregion #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
} }
} }

@ -14,7 +14,7 @@ namespace STranslate.ViewModel
{ {
public SettingsVM() public SettingsVM()
{ {
IsStartup = StartupHelper.IsStartup(); IsStartup = ShortcutHelper.IsStartup();
Version = HandleVersion(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() ?? "1.0.0.0"); Version = HandleVersion(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() ?? "1.0.0.0");
@ -63,9 +63,9 @@ namespace STranslate.ViewModel
StartupCmd = new RelayCommand((_) => true, (_) => StartupCmd = new RelayCommand((_) => true, (_) =>
{ {
if (StartupHelper.IsStartup()) StartupHelper.UnSetStartup(); if (ShortcutHelper.IsStartup()) ShortcutHelper.UnSetStartup();
else StartupHelper.SetStartup(); else ShortcutHelper.SetStartup();
IsStartup = StartupHelper.IsStartup(); IsStartup = ShortcutHelper.IsStartup();
}); });
EscCmd = new RelayCommand((_) => true, (o) => EscCmd = new RelayCommand((_) => true, (o) =>
{ {

Loading…
Cancel
Save