44using System . Diagnostics ;
55using System . IO ;
66using System . Linq ;
7- using System . Security . Principal ;
87using System . Threading . Tasks ;
9- using System . Windows ;
108using Flow . Launcher . Plugin . SharedCommands ;
119using WindowsInput ;
1210using WindowsInput . Native ;
@@ -25,32 +23,6 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, IDispo
2523 private bool _winRStroked ;
2624 private readonly KeyboardSimulator _keyboardSimulator = new ( new InputSimulator ( ) ) ;
2725
28- private static readonly string [ ] possibleCmdPaths = new [ ]
29- {
30- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "cmd.exe" ) ,
31- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . Windows ) , @"System32\cmd.exe" )
32- } ;
33-
34- private static readonly string [ ] possiblePowershellPaths = new [ ]
35- {
36- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , @"WindowsPowerShell\v1.0\powershell.exe" ) ,
37- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . Windows ) , @"System32\WindowsPowerShell\v1.0\powershell.exe" )
38- } ;
39-
40- private static readonly string [ ] possiblePwshPaths = new [ ]
41- {
42- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFiles ) , @"PowerShell\7\pwsh.exe" ) ,
43- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , @"Microsoft\WindowsApps\pwsh.exe" ) ,
44- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , @"scoop\apps\pwsh\current\pwsh.exe" ) // if using Scoop
45- } ;
46-
47- private static readonly List < string > possibleWindowsTerminalPaths = new ( )
48- {
49- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , @"Microsoft\WindowsApps\wt.exe" ) ,
50- } ;
51-
52- private static readonly bool IsAdmin = IsAdministrator ( ) ;
53-
5426 private Settings _settings ;
5527
5628 public List < Result > Query ( Query query )
@@ -337,111 +309,8 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin
337309
338310 private static Process StartProcess ( ProcessStartInfo info )
339311 {
340- switch ( info . FileName )
341- {
342- case "cmd.exe" :
343- // Check if cmd.exe exists in the system PATH or the specified directories
344- var cmdPath = possibleCmdPaths . FirstOrDefault ( File . Exists ) ;
345- if ( string . IsNullOrEmpty ( cmdPath ) )
346- {
347- Context . API . LogError ( ClassName , $ "Cannot find cmd.exe") ;
348- // Fall back to using the Process.Start method directly
349- // Show user account control dialog if Flow is running as administrator
350- if ( IsAdmin )
351- {
352- var uacDialog = Context . API . ShowUACDialog ( Context . API . GetTranslation ( "flowlauncher_plugin_cmd_cmd" ) , cmdPath , cmdPath ) ;
353- if ( uacDialog != MessageBoxResult . Yes )
354- {
355- return null ;
356- }
357- }
358-
359- return Process . Start ( info ) ;
360- }
361-
362- // If Flow is running as administrator and the command is run as administrator, we need to use UAC dialog
363- if ( IsAdmin && info . Verb == "runas" )
364- {
365- var uacDialog = Context . API . ShowUACDialog ( Context . API . GetTranslation ( "flowlauncher_plugin_cmd_cmd" ) , cmdPath , cmdPath ) ;
366- if ( uacDialog != MessageBoxResult . Yes )
367- {
368- return null ;
369- }
370- }
371-
372- Context . API . StartProcess ( cmdPath , info . WorkingDirectory , string . Join ( " " , info . ArgumentList ) , info . Verb == "runas" ) ;
373- return null ;
374-
375- case "powershell.exe" :
376- var powershellPath = possiblePowershellPaths . FirstOrDefault ( File . Exists ) ;
377- if ( string . IsNullOrEmpty ( powershellPath ) )
378- {
379- Context . API . LogError ( ClassName , $ "Cannot find powershell.exe") ;
380- // Fall back to using the Process.Start method directly
381- // Show user account control dialog if Flow is running as administrator
382- if ( IsAdmin )
383- {
384- var uacDialog = Context . API . ShowUACDialog ( Context . API . GetTranslation ( "flowlauncher_plugin_cmd_powershell" ) , powershellPath , powershellPath ) ;
385- if ( uacDialog != MessageBoxResult . Yes )
386- {
387- return null ;
388- }
389- }
390-
391- return Process . Start ( info ) ;
392- }
393-
394- // If Flow is running as administrator and the command is run as administrator, we need to use UAC dialog
395- if ( IsAdmin && info . Verb == "runas" )
396- {
397- var uacDialog = Context . API . ShowUACDialog ( Context . API . GetTranslation ( "flowlauncher_plugin_cmd_powershell" ) , powershellPath , powershellPath ) ;
398- if ( uacDialog != MessageBoxResult . Yes )
399- {
400- return null ;
401- }
402- }
403-
404- Context . API . StartProcess ( powershellPath , info . WorkingDirectory , string . Join ( " " , info . ArgumentList ) , info . Verb == "runas" ) ;
405- return null ;
406-
407- // Due to arguments pass issues, powershell 7 fails to work with deelevate model
408- // So we use shell to execuete them which means if Flow is running as administrator,
409- // all commands must be running as administrator
410- case "pwsh.exe" :
411- // Fall back to using the Process.Start method directly
412- // Show user account control dialog if Flow is running as administrator
413- if ( IsAdmin )
414- {
415- var pwshPath = possiblePwshPaths . FirstOrDefault ( File . Exists ) ;
416- var uacDialog = Context . API . ShowUACDialog ( Context . API . GetTranslation ( "flowlauncher_plugin_cmd_pwsh" ) , pwshPath , pwshPath ?? "pwsh.exe" ) ;
417- if ( uacDialog != MessageBoxResult . Yes )
418- {
419- return null ;
420- }
421- }
422-
423- return Process . Start ( info ) ;
424-
425- // Due to arguments pass issues, windows terminal fails to work with deelevate model
426- // So we use shell to execuete them which means if Flow is running as administrator,
427- // all commands must be running as administrator
428- case "wt.exe" :
429- // Fall back to using the Process.Start method directly
430- // Show user account control dialog if Flow is running as administrator
431- if ( IsAdmin )
432- {
433- var wtPath = possibleWindowsTerminalPaths . FirstOrDefault ( File . Exists ) ;
434- var uacDialog = Context . API . ShowUACDialog ( Context . API . GetTranslation ( "flowlauncher_plugin_cmd_wt" ) , wtPath , wtPath ?? "wt.exe" ) ;
435- if ( uacDialog != MessageBoxResult . Yes )
436- {
437- return null ;
438- }
439- }
440-
441- return Process . Start ( info ) ;
442- }
443-
444- return Process . Start ( info ) ;
312+ Context . API . StartProcess ( info . FileName , info . WorkingDirectory , info . ArgumentList , info . UseShellExecute , info . Verb ) ;
313+ return null ;
445314 }
446315
447316 private static void Execute ( Func < ProcessStartInfo , Process > startProcess , ProcessStartInfo info )
@@ -498,21 +367,6 @@ public void Init(PluginInitContext context)
498367 Context = context ;
499368 _settings = context . API . LoadSettingJsonStorage < Settings > ( ) ;
500369 context . API . RegisterGlobalKeyboardCallback ( API_GlobalKeyboardEvent ) ;
501-
502- // Search for all folders with Microsoft.WindowsTerminal_ prefix
503- /*var windowsAppsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WindowsApps");
504- var windowsTerminalFolders = Directory.EnumerateDirectories(windowsAppsPath, "Microsoft.WindowsTerminal_*",
505- SearchOption.TopDirectoryOnly);
506-
507- // Find wt.exe among them
508- foreach (var windowsTerminalFolder in windowsTerminalFolders)
509- {
510- var windowsTerminalPath = Path.Combine(windowsTerminalFolder, "wt.exe");
511- if (File.Exists(windowsTerminalPath))
512- {
513- possibleWindowsTerminalPaths.Insert(0, windowsTerminalPath);
514- }
515- }*/
516370 }
517371
518372 bool API_GlobalKeyboardEvent ( int keyevent , int vkcode , SpecialKeyState state )
@@ -612,12 +466,5 @@ public void Dispose()
612466 {
613467 Context . API . RemoveGlobalKeyboardCallback ( API_GlobalKeyboardEvent ) ;
614468 }
615-
616- private static bool IsAdministrator ( )
617- {
618- using var identity = WindowsIdentity . GetCurrent ( ) ;
619- var principal = new WindowsPrincipal ( identity ) ;
620- return principal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
621- }
622469 }
623470}
0 commit comments