From 5e4eb971213093e00a7eeb99b5b58357974dcd32 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 1 Jun 2021 00:25:26 +0100 Subject: [PATCH] Added: first and last environment path --- Models/TileModel.cs | 76 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/Models/TileModel.cs b/Models/TileModel.cs index 53f8dcf..ba7aa02 100644 --- a/Models/TileModel.cs +++ b/Models/TileModel.cs @@ -20,11 +20,81 @@ namespace AppLauncher.Models public override string ToString() => this.Title ?? string.Empty; - public string CleanProcessFilename => (string.IsNullOrWhiteSpace(this.ProcessFilename) ? string.Empty : Environment.ExpandEnvironmentVariables(this.ProcessFilename)); + public string CleanProcessFilename => (string.IsNullOrWhiteSpace(this.ProcessFilename) ? string.Empty : resolvePath(this.ProcessFilename)); - public string CleanProcessArgument => (string.IsNullOrWhiteSpace(this.ProcessArgument) ? string.Empty : Environment.ExpandEnvironmentVariables(this.ProcessArgument)); + public string CleanProcessArgument => (string.IsNullOrWhiteSpace(this.ProcessArgument) ? string.Empty : resolvePath(this.ProcessArgument)); - public string CleanProcessWorkingDirectory => (string.IsNullOrWhiteSpace(this.ProcessWorkingDirectory) ? string.Empty : Environment.ExpandEnvironmentVariables(this.ProcessWorkingDirectory)); + public string CleanProcessWorkingDirectory => (string.IsNullOrWhiteSpace(this.ProcessWorkingDirectory) ? string.Empty : resolvePath(this.ProcessWorkingDirectory)); + + protected string resolvePath(string value) + { + string rv = Environment.ExpandEnvironmentVariables(value); + + rv = resolveFirstPath(rv); + rv = resolveLastPath(rv); + + return rv; + } + + protected string resolveFirstPath(string value) + { + const string last = "%FIRST%"; + if (!value.Contains(last)) + { + return value; + } + + string head = value.Substring(0, value.IndexOf(last)); + string tail = value.Substring(value.IndexOf(last) + last.Length); + + string[] dirList = new string[0]; + + try + { + dirList = System.IO.Directory.GetDirectories(head, "*", System.IO.SearchOption.TopDirectoryOnly); + } + catch + { + // do nothing + } + + if (dirList.Length <= 0) + { + return value; + } + + return dirList[0] + tail; + } + + protected string resolveLastPath(string value) + { + const string last = "%LAST%"; + if (!value.Contains(last)) + { + return value; + } + + string head = value.Substring(0, value.IndexOf(last)); + string tail = value.Substring(value.IndexOf(last) + last.Length); + + string[] dirList = new string[0]; + + try + { + dirList = System.IO.Directory.GetDirectories(head, "*", System.IO.SearchOption.TopDirectoryOnly); + } + catch + { + // do nothing + } + + if (dirList.Length <= 0) + { + return value; + } + + return dirList[(dirList.Length - 1)] + tail; + } } }