diff --git a/KeyGo/AppAutoStart.cs b/KeyGo/AppAutoStart.cs
new file mode 100644
index 0000000..78acb88
--- /dev/null
+++ b/KeyGo/AppAutoStart.cs
@@ -0,0 +1,178 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using IWshRuntimeLibrary;
+using System.Diagnostics;
+
+namespace KeyGo
+{
+ ///
+ /// 摘自:https://blog.csdn.net/liyu3519/article/details/81257839
+ /// 应用开机自启动帮助类
+ ///
+ static class AppAutoStart
+ {
+ ///
+ /// 快捷方式名称-任意自定义
+ ///
+ private static string QuickName { get; }
+
+ ///
+ /// 自动获取系统自动启动目录
+ ///
+ private static string SystemStartPath { get; }
+
+ ///
+ /// 自动获取程序完整路径
+ ///
+ private static string AppAllPath { get; }
+
+ static AppAutoStart()
+ {
+ var thisProcess = Process.GetCurrentProcess();
+ QuickName = thisProcess.ProcessName;
+ AppAllPath = thisProcess.MainModule.FileName;
+ SystemStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
+ Console.WriteLine(SystemStartPath);
+ }
+
+ ///
+ /// 设置开机自动启动-只需要调用改方法就可以了参数里面的bool变量是控制开机启动的开关的,默认为开启自启启动
+ ///
+ /// 自启开关
+ public static void SetMeAutoStart(bool onOff = true)
+ {
+ if (onOff)//开机启动
+ {
+ //获取启动路径应用程序快捷方式的路径集合
+ List shortcutPaths = GetQuickFromFolder(SystemStartPath, AppAllPath);
+ //存在2个以快捷方式则保留一个快捷方式-避免重复多于
+ if (shortcutPaths.Count >= 2)
+ {
+ for (int i = 1; i < shortcutPaths.Count; i++)
+ {
+ DeleteFile(shortcutPaths[i]);
+ }
+ }
+ else if (shortcutPaths.Count < 1)//不存在则创建快捷方式
+ {
+ CreateShortcut(SystemStartPath, QuickName, AppAllPath);
+ }
+ }
+ else//开机不启动
+ {
+ //获取启动路径应用程序快捷方式的路径集合
+ List shortcutPaths = GetQuickFromFolder(SystemStartPath, AppAllPath);
+ //存在快捷方式则遍历全部删除
+ if (shortcutPaths.Count > 0)
+ {
+ for (int i = 0; i < shortcutPaths.Count; i++)
+ {
+ DeleteFile(shortcutPaths[i]);
+ }
+ }
+ }
+ }
+
+ ///
+ /// 向目标路径创建指定文件的快捷方式
+ ///
+ /// 目标目录
+ /// 快捷方式名字
+ /// 文件完全路径
+ /// 描述
+ /// 图标地址
+ /// 成功或失败
+ private static bool CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
+ {
+ try
+ {
+ if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); //目录不存在则创建
+ //添加引用 Com 中搜索 Windows Script Host Object Model
+ string shortcutPath = Path.Combine(directory, $"{shortcutName}.lnk"); //合成路径
+ WshShell shell = new WshShell();
+ IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); //创建快捷方式对象
+ shortcut.TargetPath = targetPath; //指定目标路径
+ shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath); //设置起始位置
+ shortcut.WindowStyle = 1; //设置运行方式,默认为常规窗口
+ shortcut.Description = description; //设置备注
+ shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation; //设置图标路径
+ shortcut.Save(); //保存快捷方式
+ return true;
+ }
+ catch (Exception ex)
+ {
+ string temp = ex.Message;
+ temp = "";
+ }
+ return false;
+ }
+
+ ///
+ /// 获取指定文件夹下指定应用程序的快捷方式路径集合
+ ///
+ /// 文件夹
+ /// 目标应用程序路径
+ /// 目标应用程序的快捷方式
+ private static List GetQuickFromFolder(string directory, string targetPath)
+ {
+ List tempStrs = new List();
+ tempStrs.Clear();
+ string tempStr = null;
+ string[] files = Directory.GetFiles(directory, "*.lnk");
+ if (files == null || files.Length < 1)
+ {
+ return tempStrs;
+ }
+ for (int i = 0; i < files.Length; i++)
+ {
+ //files[i] = string.Format("{0}\\{1}", directory, files[i]);
+ tempStr = GetAppPathFromQuick(files[i]);
+ if (tempStr == targetPath)
+ {
+ tempStrs.Add(files[i]);
+ }
+ }
+ return tempStrs;
+ }
+
+ ///
+ /// 获取快捷方式的目标文件路径-用于判断是否已经开启了自动启动
+ ///
+ ///
+ ///
+ private static string GetAppPathFromQuick(string shortcutPath)
+ {
+ //快捷方式文件的路径 = @"d:\Test.lnk";
+ if (System.IO.File.Exists(shortcutPath))
+ {
+ WshShell shell = new WshShell();
+ IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
+ //快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath;
+ //快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;
+ return shortct.TargetPath;
+ }
+ else
+ {
+ return "";
+ }
+ }
+
+ ///
+ /// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式
+ ///
+ /// 路径
+ private static void DeleteFile(string path)
+ {
+ FileAttributes attr = System.IO. File.GetAttributes(path);
+ if (attr == FileAttributes.Directory)
+ {
+ Directory.Delete(path, true);
+ }
+ else
+ {
+ System.IO.File.Delete(path);
+ }
+ }
+ }
+}
diff --git a/KeyGo/FormMain.cs b/KeyGo/FormMain.cs
index 2ac1d56..38cf232 100644
--- a/KeyGo/FormMain.cs
+++ b/KeyGo/FormMain.cs
@@ -4,8 +4,6 @@ using System.IO;
using System.Reflection;
using System.Windows.Forms;
-using Microsoft.Win32;
-
namespace KeyGo
{
public partial class FormMain : Form
@@ -302,26 +300,11 @@ namespace KeyGo
{
try
{
- if (enable)
- {
- RegistryKey R_local = Registry.CurrentUser;
- RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
- R_run.SetValue("KeyGo", Application.ExecutablePath);
- R_run.Close();
- R_local.Close();
- }
- else
- {
- RegistryKey R_local = Registry.CurrentUser;
- RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
- R_run.DeleteValue("KeyGo", false);
- R_run.Close();
- R_local.Close();
- }
+ AppAutoStart.SetMeAutoStart(enable);
}
catch (Exception ex)
{
- MessageBox.Show("注册表编辑失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ MessageBox.Show("设置开机自启时异常:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
diff --git a/KeyGo/KeyGo.csproj b/KeyGo/KeyGo.csproj
index 4e30feb..6bef67c 100644
--- a/KeyGo/KeyGo.csproj
+++ b/KeyGo/KeyGo.csproj
@@ -61,9 +61,7 @@
false
-
- Properties\app.manifest
-
+
@@ -78,6 +76,7 @@
+
@@ -131,7 +130,6 @@
UCHotKeyItem.cs
-
SettingsSingleFileGenerator
Settings.Designer.cs
@@ -145,7 +143,17 @@
-
+
+
+ {F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}
+ 1
+ 0
+ 0
+ tlbimp
+ False
+ True
+
+
diff --git a/KeyGo/Properties/AssemblyInfo.cs b/KeyGo/Properties/AssemblyInfo.cs
index cfb63ec..2bef735 100644
--- a/KeyGo/Properties/AssemblyInfo.cs
+++ b/KeyGo/Properties/AssemblyInfo.cs
@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("0.1.2")]
+[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
\ No newline at end of file
diff --git a/KeyGo/Properties/app.manifest b/KeyGo/Properties/app.manifest
deleted file mode 100644
index d66e033..0000000
--- a/KeyGo/Properties/app.manifest
+++ /dev/null
@@ -1,69 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file