From 4d267fe71ee0f3aeb88f8781cb9aa4f5739cb49d Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:27:12 +0800 Subject: [PATCH] Fix incorrect %homepath% parsing Parse environment variables before checking path existence --- .../SharedCommands/FilesFolders.cs | 10 ++++++++++ .../Search/EnvironmentVariables.cs | 20 ++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index 2ccc35884..42a0e0c73 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -261,5 +261,15 @@ namespace Flow.Launcher.Plugin.SharedCommands && !rel.StartsWith(@"..\") && !Path.IsPathRooted(rel); } + + /// + /// Returns path ended with "\" + /// + /// + /// + public static string EnsureTrailingSlash(this string path) + { + return path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; + } } } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs index 130de2ab1..a649de58e 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs @@ -2,7 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; -using System.Text; +using Flow.Launcher.Plugin.SharedCommands; namespace Flow.Launcher.Plugin.Explorer.Search { @@ -37,20 +37,22 @@ namespace Flow.Launcher.Plugin.Explorer.Search internal static Dictionary LoadEnvironmentStringPaths() { var envStringPaths = new Dictionary(StringComparer.InvariantCultureIgnoreCase); + var homedrive = Environment.GetEnvironmentVariable("HOMEDRIVE")?.EnsureTrailingSlash() ?? "C:\\"; foreach (DictionaryEntry special in Environment.GetEnvironmentVariables()) { var path = special.Value.ToString(); + // we add a trailing slash to the path to make sure drive paths become valid absolute paths. + // for example, if %systemdrive% is C: we turn it to C:\ + path = path.EnsureTrailingSlash(); + + // if we don't have an absolute path, we use Path.GetFullPath to get one. + // for example, if %homepath% is \Users\John we turn it to C:\Users\John + // Add basepath for GetFullPath() to parse %HOMEPATH% correctly + path = Path.IsPathFullyQualified(path) ? path : Path.GetFullPath(path, homedrive); + if (Directory.Exists(path)) { - // we add a trailing slash to the path to make sure drive paths become valid absolute paths. - // for example, if %systemdrive% is C: we turn it to C:\ - path = path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; - - // if we don't have an absolute path, we use Path.GetFullPath to get one. - // for example, if %homepath% is \Users\John we turn it to C:\Users\John - path = Path.IsPathFullyQualified(path) ? path : Path.GetFullPath(path); - // Variables are returned with a mixture of all upper/lower case. // Call ToUpper() to make the results look consistent envStringPaths.Add(special.Key.ToString().ToUpper(), path);