首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何注册一个应用程序,以便使用Squirrel.Windows在Windows启动程序上运行?

如何注册一个应用程序,以便使用Squirrel.Windows在Windows启动程序上运行?
EN

Stack Overflow用户
提问于 2018-10-28 01:58:06
回答 2查看 1.2K关注 0票数 3

在使用Squirrel.Windows构建安装程序时,是否有一种方法可以在Windows启动时注册已安装的应用程序以运行?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-31 01:32:43

我刚刚发现了定制松鼠事件,我们可以处理这些来创建/删除应用程序在windows启动时运行的适当注册表。

代码语言:javascript
复制
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);
    }
}

用例

代码语言:javascript
复制
string updateUrl = //...

using (var mgr = new UpdateManager(updateUrl)))
{
    SquirrelAwareApp.HandleEvents(
        onInitialInstall: v => 
        {
            mgr.CreateShortcutForThisExe();
            mgr.CreateRunAtWindowsStartupRegistry();
        },
        onAppUninstall: v =>
        {
            mgr.RemoveShortcutForThisExe();
            mgr.RemoveRunAtWindowsStartupRegistry();
        });
}
票数 5
EN

Stack Overflow用户

发布于 2021-02-12 03:14:03

还可以通过向用户的启动文件夹添加快捷方式来完成此操作:

代码语言:javascript
复制
    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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53027782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档