如何使用 Squirrel.Windows 注册应用程序以在 Windows 启动时运行?

Jan*_* Go 3 windows windows-installer squirrel.windows

使用 Squirrel.Windows 构建安装程序时,有没有办法注册已安装的应用程序以在 Windows 启动时运行?

谢谢!

Jan*_* Go 5

我刚刚发现了自定义松鼠事件,我们可以处理这些事件来创建/删除适当的注册表,以便我们的应用程序在 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用案例

string updateUrl = //...

using (var mgr = new UpdateManager(updateUrl)))
{
    SquirrelAwareApp.HandleEvents(
        onInitialInstall: v => 
        {
            mgr.CreateShortcutForThisExe();
            mgr.CreateRunAtWindowsStartupRegistry();
        },
        onAppUninstall: v =>
        {
            mgr.RemoveShortcutForThisExe();
            mgr.RemoveRunAtWindowsStartupRegistry();
        });
}
Run Code Online (Sandbox Code Playgroud)