|
|
@@ -1,15 +1,22 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Linq;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
+//############################################################
|
|
|
+// https://github.com/yuzhengyang
|
|
|
+// author:yuzhengyang
|
|
|
+//############################################################
|
|
|
+using System;
|
|
|
using Y.Utils.WindowsUtils.InfoUtils;
|
|
|
|
|
|
namespace Y.Utils.AppUtils
|
|
|
{
|
|
|
public class StartupTool
|
|
|
{
|
|
|
+ [Obsolete]
|
|
|
public static string RegeditRunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
|
|
|
+
|
|
|
+ public static string regAll = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
|
|
|
+ public static string regCurrent = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
|
|
|
+ public static string commonStartup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
|
|
|
+ public static string startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
|
|
|
+ [Obsolete]
|
|
|
public static bool Regedit(string appName, string appFile, bool start = true)
|
|
|
{
|
|
|
if (start)
|
|
|
@@ -26,15 +33,59 @@ namespace Y.Utils.AppUtils
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
- public static bool Shortcut(string appName, string appFile, bool start = true)
|
|
|
+ public static bool Register(string name, string file, bool start = true, bool allUser = true)
|
|
|
+ {
|
|
|
+ if (start)
|
|
|
+ {
|
|
|
+ //注册开机启动注册表项
|
|
|
+ if (allUser)
|
|
|
+ {
|
|
|
+ if (RegisterTool.SetValue(regAll, name, file)) return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (RegisterTool.SetValue(regCurrent, name, file)) return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //移除开机启动注册表项
|
|
|
+ if (allUser)
|
|
|
+ {
|
|
|
+ if (RegisterTool.DeleteValue(regAll, name)) return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (RegisterTool.DeleteValue(regCurrent, name)) return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ public static bool Shortcut(string name, string file, bool start = true, bool allUser = true)
|
|
|
{
|
|
|
if (start)
|
|
|
{
|
|
|
//添加开机启动开始菜单项
|
|
|
+ if (allUser)
|
|
|
+ {
|
|
|
+ if (ShortcutTool.Create(commonStartup, name, file)) return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (ShortcutTool.Create(startup, name, file)) return true;
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
//删除开机启动开始菜单项
|
|
|
+ if (allUser)
|
|
|
+ {
|
|
|
+ if (ShortcutTool.Delete(commonStartup, name)) return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (ShortcutTool.Delete(startup, name)) return true;
|
|
|
+ }
|
|
|
}
|
|
|
return false;
|
|
|
}
|