使用 Squirrel.Windows 构建安装程序时,是否可以注册已安装的应用程序以在 Windows 启动时运行?
谢谢!
使用 Squirrel.Windows 构建安装程序时,是否可以注册已安装的应用程序以在 Windows 启动时运行?
谢谢!
我刚刚发现了自定义松鼠事件,我们可以处理这些事件来创建/删除适当的注册表,以便我们的应用程序在 Windows 启动时运行。
using Microsoft.Win32;
using Squirrel;
using System.IO;
public static class UpdateManagerExtensions
{
private static RegistryKey OpenRunAtWindowsStartupRegistryKey() =>
Registry.CurrentUser.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
public static void CreateRunAtWindowsStartupRegistry(this UpdateManager updateManager)
{
using (var startupRegistryKey = OpenRunAtWindowsStartupRegistryKey())
startupRegistryKey.SetValue(
updateManager.ApplicationName,
Path.Combine(updateManager.RootAppDirectory, $"{updateManager.ApplicationName}.exe"));
}
public static void RemoveRunAtWindowsStartupRegistry(this UpdateManager updateManager)
{
using (var startupRegistryKey = OpenRunAtWindowsStartupRegistryKey())
startupRegistryKey.DeleteValue(updateManager.ApplicationName);
}
}
string updateUrl = //...
using (var mgr = new UpdateManager(updateUrl)))
{
SquirrelAwareApp.HandleEvents(
onInitialInstall: v =>
{
mgr.CreateShortcutForThisExe();
mgr.CreateRunAtWindowsStartupRegistry();
},
onAppUninstall: v =>
{
mgr.RemoveShortcutForThisExe();
mgr.RemoveRunAtWindowsStartupRegistry();
});
}
也可以通过向用户的启动文件夹添加快捷方式来完成:
private void OnInitialInstall(UpdateManager mgr)
{
mgr.CreateShortcutForThisExe();
mgr.CreateShortcutsForExecutable("MyApp.exe", ShortcutLocation.StartUp, false);
mgr.CreateShortcutsForExecutable("MyApp.exe", ShortcutLocation.Desktop, false);
mgr.CreateShortcutsForExecutable("MyApp.exe", ShortcutLocation.StartMenu, false);
mgr.CreateUninstallerRegistryEntry();
}