From 0f5b02fa38241bce2f86bc7a961395de01ccc19e Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:22:29 +0800 Subject: [PATCH] Open file with associated program Prevent opening zip in Explorer Fix working directory issue --- .../SharedCommands/FilesFolders.cs | 30 ++++++++++++++++ .../Search/ResultManager.cs | 34 ++++--------------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index 6b7f0c2d3..dd4dcc7a4 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -170,6 +170,36 @@ namespace Flow.Launcher.Plugin.SharedCommands } } + /// + /// Open a file with associated application + /// + /// File path + /// Working directory + /// Open as Administrator + public static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false) + { + var psi = new ProcessStartInfo + { + FileName = filePath, + UseShellExecute = true, + WorkingDirectory = workingDir, + Verb = asAdmin ? "runas" : string.Empty + }; + try + { + if (FileExists(filePath)) + Process.Start(psi); + } + catch (Exception) + { +#if DEBUG + throw; +#else + MessageBox.Show(string.Format("Unable to open the path {0}, please check if it exists", filePath)); +#endif + } + } + /// /// This checks whether a given string is a directory path or network location string. /// It does not check if location actually exists. diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs index 5ac01c3ad..76bf54acd 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs @@ -123,8 +123,8 @@ namespace Flow.Launcher.Plugin.Explorer.Search } else { - // or make this folder the current query - Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword)); + // or make this folder the current query + Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword)); } return false; @@ -255,7 +255,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search { if (c.SpecialKeyState.ToModifierKeys() == (ModifierKeys.Control | ModifierKeys.Shift)) { - OpenFileAsAdmin(filePath); + OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, true); } else if (c.SpecialKeyState.ToModifierKeys() == ModifierKeys.Control) { @@ -263,7 +263,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search } else { - OpenFile(filePath); + OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty); } } catch (Exception ex) @@ -287,10 +287,10 @@ namespace Flow.Launcher.Plugin.Explorer.Search return MediaExtensions.Contains(extension.ToLowerInvariant()); } - private static void OpenFile(string filePath) + private static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false) { IncrementEverythingRunCounterIfNeeded(filePath); - FilesFolders.OpenPath(filePath); + FilesFolders.OpenFile(filePath, workingDir, asAdmin); } private static void OpenFolder(string folderPath, string fileNameOrFilePath = null) @@ -299,28 +299,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search Context.API.OpenDirectory(folderPath, fileNameOrFilePath); } - private static void OpenFileAsAdmin(string filePath) - { - _ = Task.Run(() => - { - try - { - IncrementEverythingRunCounterIfNeeded(filePath); - Process.Start(new ProcessStartInfo - { - FileName = filePath, - UseShellExecute = true, - WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, - Verb = "runas", - }); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Could not start " + filePath); - } - }); - } - private static void IncrementEverythingRunCounterIfNeeded(string fileOrFolder) { if (Settings.EverythingEnabled)