From 8cff4365fc7e4e3a55c7de3bd24b802ce947e212 Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Thu, 21 Oct 2021 20:51:24 +0200 Subject: [PATCH] [BUG] Microsoft Store Autostart not working (#131), version 1.0.22.2 --- Packaging/Package.appxmanifest | 10 +++++ SystemTrayMenu.csproj | 3 +- UserInterface/SettingsForm.cs | 67 +++++++++++++++++++++++++++++----- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/Packaging/Package.appxmanifest b/Packaging/Package.appxmanifest index cd2fbb7..179f706 100644 --- a/Packaging/Package.appxmanifest +++ b/Packaging/Package.appxmanifest @@ -3,6 +3,7 @@ @@ -40,6 +41,15 @@ + + + + + diff --git a/SystemTrayMenu.csproj b/SystemTrayMenu.csproj index 6a38c21..f5f7a8c 100644 --- a/SystemTrayMenu.csproj +++ b/SystemTrayMenu.csproj @@ -286,11 +286,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/UserInterface/SettingsForm.cs b/UserInterface/SettingsForm.cs index 6e3e1b2..5816953 100644 --- a/UserInterface/SettingsForm.cs +++ b/UserInterface/SettingsForm.cs @@ -10,10 +10,12 @@ namespace SystemTrayMenu.UserInterface using System.IO; using System.Reflection; using System.Text; + using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; using SystemTrayMenu.Properties; using SystemTrayMenu.Utilities; + using Windows.ApplicationModel; using static SystemTrayMenu.UserInterface.HotkeyTextboxControl.HotkeyControl; public partial class SettingsForm : Form @@ -203,6 +205,12 @@ namespace SystemTrayMenu.UserInterface { checkBoxAutostart.Checked = Settings.Default.IsAutostartActivated; +#if RELEASEPACKAGE + if (Settings.Default.IsAutostartActivated) + { + checkBoxAutostart.Enabled = false; + } +#endif } InitializeHotkey(); @@ -563,23 +571,64 @@ namespace SystemTrayMenu.UserInterface Settings.Default.UseIconFromRootFolder = checkBoxUseIconFromRootFolder.Checked; - SaveAutostart(); - void SaveAutostart() + _ = SaveAutostartAsync().Wait(8000); + async Task SaveAutostartAsync() { + bool useStartupTask = false; +#if RELEASEPACKAGE + useStartupTask = true; +#endif if (checkBoxAutostart.Checked) { - RegistryKey key = Registry.CurrentUser.OpenSubKey( - @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); - key.SetValue( - Assembly.GetExecutingAssembly().GetName().Name, - System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); + if (useStartupTask) + { + // Pass the task ID you specified in the appxmanifest file + StartupTask startupTask = await StartupTask.GetAsync("MyStartupId"); + Log.Info($"Autostart {startupTask.State}."); + + switch (startupTask.State) + { + case StartupTaskState.Enabled: + break; + case StartupTaskState.Disabled: + // Task is disabled but can be enabled. + StartupTaskState newState = await startupTask.RequestEnableAsync(); + break; + case StartupTaskState.DisabledByUser: + Log.Info($"Please enable autostart via Task Manager."); + break; + case StartupTaskState.DisabledByPolicy: + Log.Info($"Autostart disabled by policy. Please add autostart manually, see issue #131"); + break; + case StartupTaskState.EnabledByPolicy: + default: + break; + } + } + else + { + RegistryKey key = Registry.CurrentUser.OpenSubKey( + @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); + key.SetValue( + Assembly.GetExecutingAssembly().GetName().Name, + System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); + } + Settings.Default.IsAutostartActivated = true; } else { - RegistryKey key = Registry.CurrentUser.OpenSubKey( + if (useStartupTask) + { + // when added by StartupTask, then only possible to disable autostart via Task Manager. + } + else + { + RegistryKey key = Registry.CurrentUser.OpenSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); - key.DeleteValue("SystemTrayMenu", false); + key.DeleteValue("SystemTrayMenu", false); + } + Settings.Default.IsAutostartActivated = false; } }