gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:09:38 31 4
gpt4 key购买 nike

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

谢谢!

最佳答案

我刚刚发现 Custom Squirrel Events我们可以处理这些以创建/删除适当的注册表,以便我们的应用程序在 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();
});
}

关于windows - 如何使用 Squirrel.Windows 注册应用程序以在 Windows 启动时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53027782/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com