From e47cab2a3fbde1544e6237439b23596a146e83c8 Mon Sep 17 00:00:00 2001 From: Rainer Koschke Date: Mon, 16 Sep 2024 15:00:55 +0200 Subject: [PATCH] #726 Added InnermostDirectoryName. --- Assets/SEE/Utils/Filenames.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Assets/SEE/Utils/Filenames.cs b/Assets/SEE/Utils/Filenames.cs index af0d74f4ee..4afbd1a8fa 100644 --- a/Assets/SEE/Utils/Filenames.cs +++ b/Assets/SEE/Utils/Filenames.cs @@ -273,5 +273,32 @@ public static void DeleteReadOnlyDirectory(string directory) } Directory.Delete(directory); } + + /// + /// Returns the innermost directory name of the given + /// where is a (possibly nested) platform-dependent + /// path to a directory + /// + /// platform-dependent directory path + /// innermost directory name + /// if is null or empty + /// If is C:\Users\someone\develop\SEE\ + /// while running on a Windows computer, then SEE will be returned; likewise if it + /// is C:\Users\someone\develop\SEE. If is + /// /home/someone/develop/SEE/ while running on a Unix computer, then SEE will be returned; + /// likewise if it is /home/someone/develop/SEE. + /// + public static string InnermostDirectoryName(string directoryPath) + { + if (string.IsNullOrWhiteSpace(directoryPath)) + { + throw new ArgumentException("Directory path must neither be null nor empty."); + } + string path = directoryPath[^1] == Path.DirectorySeparatorChar ? + directoryPath[..^1] : directoryPath; + + return Path.GetFullPath(path).TrimEnd(Path.DirectorySeparatorChar) + .Split(Path.DirectorySeparatorChar).Last(); + } } }