Skip to content

Commit 04882ad

Browse files
committed
add run as administrator when holding ctrl and shift and to context menu for UWP applications
1 parent ffd22d7 commit 04882ad

File tree

1 file changed

+76
-1
lines changed
  • Plugins/Flow.Launcher.Plugin.Program/Programs

1 file changed

+76
-1
lines changed

Plugins/Flow.Launcher.Plugin.Program/Programs/UWP.cs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public class Application : IProgram
267267
public string Location => Package.Location;
268268

269269
public bool Enabled { get; set; }
270+
public bool CanRunElevated { get; set; }
270271

271272
public string LogoUri { get; set; }
272273
public string LogoPath { get; set; }
@@ -320,7 +321,27 @@ public Result Result(string query, IPublicAPI api)
320321
ContextData = this,
321322
Action = e =>
322323
{
323-
Launch(api);
324+
var elevated = (
325+
e.SpecialKeyState.CtrlPressed &&
326+
e.SpecialKeyState.ShiftPressed &&
327+
!e.SpecialKeyState.AltPressed &&
328+
!e.SpecialKeyState.WinPressed
329+
);
330+
331+
if (elevated)
332+
{
333+
if (!CanRunElevated)
334+
{
335+
return false;
336+
}
337+
338+
LaunchElevated();
339+
}
340+
else
341+
{
342+
Launch(api);
343+
}
344+
324345
return true;
325346
}
326347
};
@@ -354,6 +375,21 @@ public List<Result> ContextMenus(IPublicAPI api)
354375
IcoPath = "Images/folder.png"
355376
}
356377
};
378+
379+
if (CanRunElevated)
380+
{
381+
contextMenus.Add(new Result
382+
{
383+
Title = api.GetTranslation("flowlauncher_plugin_program_run_as_administrator"),
384+
Action = _ =>
385+
{
386+
LaunchElevated();
387+
return true;
388+
},
389+
IcoPath = "Images/cmd.png"
390+
});
391+
}
392+
357393
return contextMenus;
358394
}
359395

@@ -377,6 +413,20 @@ await Task.Run(() =>
377413
});
378414
}
379415

416+
private async void LaunchElevated()
417+
{
418+
string command = "shell:AppsFolder\\" + UniqueIdentifier;
419+
command = Environment.ExpandEnvironmentVariables(command.Trim());
420+
421+
var info = new ProcessStartInfo(command)
422+
{
423+
UseShellExecute = true,
424+
Verb = "runas",
425+
};
426+
427+
Main.StartProcess(Process.Start, info);
428+
}
429+
380430
public Application(AppxPackageHelper.IAppxManifestApplication manifestApp, UWP package)
381431
{
382432
// This is done because we cannot use the keyword 'out' along with a property
@@ -403,6 +453,31 @@ public Application(AppxPackageHelper.IAppxManifestApplication manifestApp, UWP p
403453
LogoPath = LogoPathFromUri(LogoUri);
404454

405455
Enabled = true;
456+
CanRunElevated = CanApplicationRunElevated();
457+
}
458+
459+
private bool CanApplicationRunElevated()
460+
{
461+
if (EntryPoint == "Windows.FullTrustApplication")
462+
{
463+
return true;
464+
}
465+
else
466+
{
467+
var manifest = Package.Location + "\\AppxManifest.xml";
468+
if (File.Exists(manifest))
469+
{
470+
var file = File.ReadAllText(manifest);
471+
472+
// Using OrdinalIgnoreCase since this is used internally
473+
if (file.Contains("TrustLevel=\"mediumIL\"", StringComparison.OrdinalIgnoreCase))
474+
{
475+
return true;
476+
}
477+
}
478+
}
479+
480+
return false;
406481
}
407482

408483
internal string ResourceFromPri(string packageFullName, string packageName, string rawReferenceValue)

0 commit comments

Comments
 (0)