Skip to content

Commit 52c36ff

Browse files
committed
Only restart from non-admin to admin & Fix restart as administrator issue
1 parent eae4a95 commit 52c36ff

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
<system:String x:Key="alwaysRunAsAdministrator">Always run as administrator</system:String>
136136
<system:String x:Key="alwaysRunAsAdministratorToolTip">Run Flow Launcher as administrator on system startup</system:String>
137137
<system:String x:Key="runAsAdministratorChange">Administrator Mode Change</system:String>
138-
<system:String x:Key="runAsAdministratorChangeAndRestart">Do you want to restart to apply administrator mode change?</system:String>
138+
<system:String x:Key="runAsAdministratorChangeAndRestart">Do you want to restart as administrator to apply this change? Or you need to run as administrator during next start manually.</system:String>
139139

140140
<!-- Setting Plugin -->
141141
<system:String x:Key="searchplugin">Search Plugin</system:String>

Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
5+
using System.Threading.Tasks;
46
using System.Windows;
57
using System.Windows.Forms;
68
using CommunityToolkit.Mvvm.Input;
@@ -9,6 +11,7 @@
911
using Flow.Launcher.Core.Resource;
1012
using Flow.Launcher.Helper;
1113
using Flow.Launcher.Infrastructure;
14+
using Flow.Launcher.Infrastructure.Image;
1215
using Flow.Launcher.Infrastructure.UserSettings;
1316
using Flow.Launcher.Plugin;
1417
using Flow.Launcher.Plugin.SharedModels;
@@ -75,7 +78,7 @@ public bool StartFlowLauncherOnSystemStartup
7578
// even if we encounter an error while setting the startup method
7679
if (value && UseLogonTaskForStartup)
7780
{
78-
CheckAdminChangeAndAskForRestart();
81+
_ = CheckAdminChangeAndAskForRestartAsync();
7982
}
8083
}
8184
}
@@ -112,7 +115,7 @@ public bool UseLogonTaskForStartup
112115
// even if we encounter an error while setting the startup method
113116
if (StartFlowLauncherOnSystemStartup && value)
114117
{
115-
CheckAdminChangeAndAskForRestart();
118+
_ = CheckAdminChangeAndAskForRestartAsync();
116119
}
117120
}
118121
}
@@ -139,22 +142,41 @@ public bool AlwaysRunAsAdministrator
139142

140143
// If we have enabled logon task startup, we need to check if we need to restart the app
141144
// even if we encounter an error while setting the startup method
142-
CheckAdminChangeAndAskForRestart();
145+
_ = CheckAdminChangeAndAskForRestartAsync();
143146
}
144147
}
145148
}
146149

147-
private void CheckAdminChangeAndAskForRestart()
150+
private async Task CheckAdminChangeAndAskForRestartAsync()
148151
{
149-
if ((AlwaysRunAsAdministrator && !_isAdministrator) || // Change from non-admin to admin
150-
(!AlwaysRunAsAdministrator && _isAdministrator)) // Change from admin to non-admin
152+
// When we change from non-admin to admin, we need to restart the app as administrator to apply the changes
153+
// Under non-administrator, we cannot delete or set the logon task which is run as administrator
154+
if (AlwaysRunAsAdministrator && !_isAdministrator)
151155
{
152156
if (App.API.ShowMsgBox(
153157
App.API.GetTranslation("runAsAdministratorChangeAndRestart"),
154158
App.API.GetTranslation("runAsAdministratorChange"),
155159
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
156160
{
157-
App.API.RestartApp(AlwaysRunAsAdministrator ? "runas" : string.Empty);
161+
App.API.HideMainWindow();
162+
163+
// We must manually save because of Environment.Exit(0)
164+
// which will cause ungraceful exit
165+
App.API.SaveAppAllSettings();
166+
167+
// Wait for all image caches to be saved before restarting
168+
await ImageLoader.WaitSaveAsync();
169+
170+
// Restart the app as administrator
171+
var startInfo = new ProcessStartInfo
172+
{
173+
UseShellExecute = true,
174+
WorkingDirectory = Environment.CurrentDirectory,
175+
FileName = Constant.ExecutablePath,
176+
Verb = "runas"
177+
};
178+
Process.Start(startInfo);
179+
Environment.Exit(0);
158180
}
159181
}
160182
}

0 commit comments

Comments
 (0)