/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using System; using System.Globalization; using System.Security; namespace Alphaleonis.Win32.Filesystem { public static partial class Path { #region AddTrailingDirectorySeparator /// [AlphaFS] Adds a trailing character to the string, when absent. /// A text string with a trailing character. The function returns when is . /// A text string to which the trailing is to be added, when absent. [SecurityCritical] public static string AddTrailingDirectorySeparator(string path) { return AddTrailingDirectorySeparator(path, false); } /// [AlphaFS] Adds a trailing or character to the string, when absent. /// A text string with a trailing or character. The function returns when is . /// A text string to which the trailing or is to be added, when absent. /// If the character will be added instead. [SecurityCritical] public static string AddTrailingDirectorySeparator(string path, bool addAlternateSeparator) { return path == null ? null : (addAlternateSeparator ? ((!path.EndsWith(AltDirectorySeparatorChar.ToString(CultureInfo.CurrentCulture), StringComparison.OrdinalIgnoreCase)) ? path + AltDirectorySeparatorChar : path) : ((!path.EndsWith(DirectorySeparatorChar.ToString(CultureInfo.CurrentCulture), StringComparison.OrdinalIgnoreCase)) ? path + DirectorySeparatorChar : path)); } #endregion // AddTrailingDirectorySeparator #region RemoveTrailingDirectorySeparator /// [AlphaFS] Removes the trailing character from the string, when present. /// A text string where the trailing character has been removed. The function returns when is . /// A text string from which the trailing is to be removed, when present. [SecurityCritical] public static string RemoveTrailingDirectorySeparator(string path) { return path == null ? null : path.TrimEnd(DirectorySeparatorChar, AltDirectorySeparatorChar); } /// [AlphaFS] Removes the trailing or character from the string, when present. /// A text string where the trailing or character has been removed. The function returns when is . /// A text string from which the trailing or is to be removed, when present. /// If the trailing character will be removed instead. [SecurityCritical] public static string RemoveTrailingDirectorySeparator(string path, bool removeAlternateSeparator) { return path == null ? null : path.TrimEnd(removeAlternateSeparator ? AltDirectorySeparatorChar : DirectorySeparatorChar); } #endregion // RemoveTrailingDirectorySeparator #region GetSuffixedDirectoryName /// [AlphaFS] Returns the directory information for the specified with a trailing character. /// /// The suffixed directory information for the specified with a trailing character, /// or if is or if denotes a root (such as "\", "C:", or * "\\server\share"). /// /// This method is similar to calling Path.GetDirectoryName() + Path.AddTrailingDirectorySeparator() /// The path. [SecurityCritical] public static string GetSuffixedDirectoryName(string path) { return GetSuffixedDirectoryNameCore(null, path); } /// [AlphaFS] Returns the directory information for the specified with a trailing character. /// /// The suffixed directory information for the specified with a trailing character, /// or if is or if denotes a root (such as "\", "C:", or * "\\server\share"). /// /// This method is similar to calling Path.GetDirectoryName() + Path.AddTrailingDirectorySeparator() /// The transaction. /// The path. [SecurityCritical] public static string GetSuffixedDirectoryNameTransacted(KernelTransaction transaction, string path) { return GetSuffixedDirectoryNameCore(transaction, path); } #endregion // GetSuffixedDirectoryName #region GetSuffixedDirectoryNameWithoutRoot /// [AlphaFS] Returns the directory information for the specified without the root and with a trailing character. /// /// The directory information for the specified without the root and with a trailing character, /// or if is or if is . /// /// The path. [SecurityCritical] public static string GetSuffixedDirectoryNameWithoutRoot(string path) { return GetSuffixedDirectoryNameWithoutRootCore(null, path); } /// [AlphaFS] Returns the directory information for the specified without the root and with a trailing character. /// /// The directory information for the specified without the root and with a trailing character, /// or if is or if is . /// /// The transaction. /// The path. [SecurityCritical] public static string GetSuffixedDirectoryNameWithoutRootTransacted(KernelTransaction transaction, string path) { return GetSuffixedDirectoryNameWithoutRootCore(transaction, path); } #endregion // GetSuffixedDirectoryNameWithoutRoot #region Internal Methods /// Returns the directory information for the specified with a trailing character. /// /// The suffixed directory information for the specified with a trailing character, /// or if is or if denotes a root (such as "\", "C:", or * "\\server\share"). /// /// This method is similar to calling Path.GetDirectoryName() + Path.AddTrailingDirectorySeparator() /// The transaction. /// The path. [SecurityCritical] private static string GetSuffixedDirectoryNameCore(KernelTransaction transaction, string path) { DirectoryInfo di = Directory.GetParentCore(transaction, path, PathFormat.RelativePath); return di != null && di.Parent != null && di.Name != null ? AddTrailingDirectorySeparator(CombineCore(false, di.Parent.FullName, di.Name), false) : null; } /// Returns the directory information for the specified without the root and with a trailing character. /// /// The directory information for the specified without the root and with a trailing character, /// or if is or if is . /// /// The transaction. /// The path. [SecurityCritical] private static string GetSuffixedDirectoryNameWithoutRootCore(KernelTransaction transaction, string path) { DirectoryInfo di = Directory.GetParentCore(transaction, path, PathFormat.RelativePath); if (di == null || di.Parent == null) return null; DirectoryInfo tmp = di; string suffixedDirectoryNameWithoutRoot; do { suffixedDirectoryNameWithoutRoot = tmp.DisplayPath.Replace(di.Root.ToString(), string.Empty); if (tmp.Parent != null) tmp = di.Parent.Parent; } while (tmp != null && tmp.Root.Parent != null && tmp.Parent != null && !Utils.IsNullOrWhiteSpace(tmp.Parent.ToString())); return Utils.IsNullOrWhiteSpace(suffixedDirectoryNameWithoutRoot) ? null : AddTrailingDirectorySeparator(suffixedDirectoryNameWithoutRoot.TrimStart(DirectorySeparatorChar), false); // TrimStart() for network-drive, like: C$ } #endregion // Internal Methods } }