3
3
using Files . Helpers ;
4
4
using System ;
5
5
using System . Linq ;
6
+ using System . Security . Principal ;
6
7
using System . Threading . Tasks ;
7
8
using Windows . ApplicationModel ;
8
9
using Windows . ApplicationModel . Activation ;
@@ -15,10 +16,24 @@ internal class Program
15
16
{
16
17
const string PrelaunchInstanceKey = "PrelaunchInstance" ;
17
18
19
+ private static bool IsAdministrator ( )
20
+ {
21
+ using var identity = WindowsIdentity . GetCurrent ( ) ;
22
+ var principal = new WindowsPrincipal ( identity ) ;
23
+ return principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
24
+ }
25
+
18
26
private static async Task Main ( )
19
27
{
20
28
var proc = System . Diagnostics . Process . GetCurrentProcess ( ) ;
21
29
30
+ if ( IsAdministrator ( ) )
31
+ {
32
+ // UWP can't start as admin, restart as normal user from FTP
33
+ await SpawnUnelevatedUwpAppInstance ( proc . Id ) ;
34
+ return ;
35
+ }
36
+
22
37
if ( ! ApplicationData . Current . LocalSettings . Values . Get ( "AlwaysOpenANewInstance" , false ) )
23
38
{
24
39
IActivatedEventArgs activatedArgs = AppInstance . GetActivatedEventArgs ( ) ;
@@ -128,6 +143,26 @@ public static async Task OpenShellCommandInExplorerAsync(string shellCommand, in
128
143
await FullTrustProcessLauncher . LaunchFullTrustProcessForCurrentAppAsync ( ) ;
129
144
}
130
145
146
+ public static async Task SpawnUnelevatedUwpAppInstance ( int pid )
147
+ {
148
+ IActivatedEventArgs activatedArgs = AppInstance . GetActivatedEventArgs ( ) ;
149
+ if ( activatedArgs is CommandLineActivatedEventArgs cmdLineArgs )
150
+ {
151
+ var parsedCommands = CommandLineParser . ParseUntrustedCommands ( cmdLineArgs . Operation . Arguments ) ;
152
+ switch ( parsedCommands . FirstOrDefault ( ) ? . Type )
153
+ {
154
+ case ParsedCommandType . ExplorerShellCommand :
155
+ case ParsedCommandType . OpenDirectory :
156
+ case ParsedCommandType . OpenPath :
157
+ ApplicationData . Current . LocalSettings . Values [ "Folder" ] = parsedCommands [ 0 ] . Payload ;
158
+ break ;
159
+ }
160
+ }
161
+ ApplicationData . Current . LocalSettings . Values [ "Arguments" ] = "StartUwp" ;
162
+ ApplicationData . Current . LocalSettings . Values [ "pid" ] = pid ;
163
+ await FullTrustProcessLauncher . LaunchFullTrustProcessForCurrentAppAsync ( ) ;
164
+ }
165
+
131
166
public static async Task TerminateUwpAppInstance ( int pid )
132
167
{
133
168
ApplicationData . Current . LocalSettings . Values [ "Arguments" ] = "TerminateUwp" ;
0 commit comments