在使用Squirrel.Windows构建安装程序时,是否有一种方法可以在Windows启动时注册已安装的应用程序以运行?
谢谢!
发布于 2018-10-31 01:32:43
我刚刚发现了定制松鼠事件,我们可以处理这些来创建/删除应用程序在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();
});
}发布于 2021-02-12 03:14:03
还可以通过向用户的启动文件夹添加快捷方式来完成此操作:
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();
}https://stackoverflow.com/questions/53027782
复制相似问题