/* 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.Diagnostics.CodeAnalysis; using System.Security; namespace Alphaleonis.Win32.Filesystem { public static partial class Path { #region ChangeExtension (.NET) /// Changes the extension of a path string. /// The modified path information. /// /// The path information to modify. The path cannot contain any of the characters defined in . /// The new extension (with or without a leading period). Specify to remove an existing extension from path. [SecurityCritical] public static string ChangeExtension(string path, string extension) { return System.IO.Path.ChangeExtension(path, extension); } #endregion // ChangeExtension (.NET) #region GetDirectoryName #region .NET /// Returns the directory information for the specified path string. /// /// Directory information for , or if denotes a root directory or is /// . /// Returns if does not contain directory information. /// /// /// The path of a file or directory. [SecurityCritical] public static string GetDirectoryName(string path) { return GetDirectoryName(path, true); } #endregion // .NET #region AlphaFS /// [AlphaFS] Returns the directory information for the specified path string. /// /// Directory information for , or if denotes a root directory or is /// . Returns if does not contain directory information. /// /// /// The path of a file or directory. /// will check for invalid path characters. [SecurityCritical] public static string GetDirectoryName(string path, bool checkInvalidPathChars) { if (path != null) { int rootLength = GetRootLength(path, checkInvalidPathChars); if (path.Length > rootLength) { int length = path.Length; if (length == rootLength) return null; while (length > rootLength && path[--length] != DirectorySeparatorChar && path[length] != AltDirectorySeparatorChar) { } return path.Substring(0, length).Replace(AltDirectorySeparatorChar, DirectorySeparatorChar); } } return null; } #endregion // AlphaFS #endregion // GetDirectoryName #region GetDirectoryNameWithoutRoot #region AlphaFS /// [AlphaFS] Returns the directory information for the specified path string without the root information, for example: "C:\Windows\system32" returns: "Windows". /// The without the file name part and without the root information (if any), or if is or if denotes a root (such as "\", "C:", or * "\\server\share"). /// The path. [SecurityCritical] public static string GetDirectoryNameWithoutRoot(string path) { return GetDirectoryNameWithoutRootTransacted(null, path); } /// [AlphaFS] Returns the directory information for the specified path string without the root information, for example: "C:\Windows\system32" returns: "Windows". /// The without the file name part and without the root information (if any), or if is or if denotes a root (such as "\", "C:", or * "\\server\share"). /// The transaction. /// The path. [SecurityCritical] public static string GetDirectoryNameWithoutRootTransacted(KernelTransaction transaction, string path) { if (path == null) return null; DirectoryInfo di = Directory.GetParentCore(transaction, path, PathFormat.RelativePath); return di != null && di.Parent != null ? di.Name : null; } #endregion // AlphaFS #endregion // GetDirectoryNameWithoutRoot #region GetExtension #region .NET /// Returns the extension of the specified path string. /// /// The extension of the specified path (including the period "."), or null, or . /// If is null, this method returns null. /// If does not have extension information, /// this method returns . /// /// /// /// The path string from which to get the extension. The path cannot contain any of the characters defined in . [SecurityCritical] public static string GetExtension(string path) { return GetExtension(path, !Utils.IsNullOrWhiteSpace(path)); } #endregion // .NET #region AlphaFS /// Returns the extension of the specified path string. /// /// The extension of the specified path (including the period "."), or null, or . /// If is null, this method returns null. /// If does not have extension information, /// this method returns . /// /// /// The path string from which to get the extension. The path cannot contain any of the characters defined in . /// will check for invalid path characters. [SecurityCritical] public static string GetExtension(string path, bool checkInvalidPathChars) { if (path == null) return null; if (checkInvalidPathChars) CheckInvalidPathChars(path, false, true); int length = path.Length; int index = length; while (--index >= 0) { char ch = path[index]; if (ch == ExtensionSeparatorChar) return index != length - 1 ? path.Substring(index, length - index) : string.Empty; if (IsDVsc(ch, null)) break; } return string.Empty; } #endregion // AlphaFS #endregion // GetExtension #region GetFileName #region .NET /// Returns the file name and extension of the specified path string. /// /// The characters after the last directory character in . If the last character of is a /// directory or volume separator character, this method returns string.Empty. If path is null, this method returns null. /// /// /// The path string from which to obtain the file name and extension. The path cannot contain any of the characters defined in . [SecurityCritical] public static string GetFileName(string path) { return GetFileName(path, true); } #endregion // .NET #region AlphaFS /// [AlphaFS] Returns the file name and extension of the specified path string. /// /// The characters after the last directory character in . If the last character of is a /// directory or volume separator character, this method returns string.Empty. If path is null, this method returns null. /// /// /// The path string from which to obtain the file name and extension. /// will check for invalid path characters. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Utils.IsNullOrWhiteSpace validates arguments.")] public static string GetFileName(string path, bool checkInvalidPathChars) { if (Utils.IsNullOrWhiteSpace(path)) return path; if (checkInvalidPathChars) CheckInvalidPathChars(path, false, true); int length = path.Length; int index = length; while (--index >= 0) { char ch = path[index]; if (IsDVsc(ch, null)) return path.Substring(index + 1, length - index - 1); } return path; } #endregion // AlphaFS #endregion // GetFileName #region GetFileNameWithoutExtension #region .NET /// Returns the file name of the specified path string without the extension. /// The string returned by GetFileName, minus the last period (.) and all characters following it. /// /// The path of the file. The path cannot contain any of the characters defined in . [SecurityCritical] public static string GetFileNameWithoutExtension(string path) { return GetFileNameWithoutExtension(path, true); } #endregion // .NET #region AlphaFS /// [AlphaFS] Returns the file name of the specified path string without the extension. /// The string returned by GetFileName, minus the last period (.) and all characters following it. /// /// The path of the file. The path cannot contain any of the characters defined in . /// will check for invalid path characters. [SecurityCritical] public static string GetFileNameWithoutExtension(string path, bool checkInvalidPathChars) { path = GetFileName(path, checkInvalidPathChars); if (path != null) { int i; return (i = path.LastIndexOf('.')) == -1 ? path : path.Substring(0, i); } return null; } #endregion // AlphaFS #endregion // GetFileNameWithoutExtension #region GetInvalidFileNameChars (.NET) /// Gets an array containing the characters that are not allowed in file names. /// An array containing the characters that are not allowed in file names. [SecurityCritical] public static char[] GetInvalidFileNameChars() { return System.IO.Path.GetInvalidFileNameChars(); } #endregion // GetInvalidFileNameChars (.NET) #region GetInvalidPathChars (.NET) /// Gets an array containing the characters that are not allowed in path names. /// An array containing the characters that are not allowed in path names. [SecurityCritical] public static char[] GetInvalidPathChars() { return System.IO.Path.GetInvalidPathChars(); } #endregion // GetInvalidPathChars (.NET) #region GetPathRoot #region .NET /// Gets the root directory information of the specified path. /// /// Returns the root directory of , such as "C:\", /// or if is , /// or an empty string if does not contain root directory information. /// /// /// The path from which to obtain root directory information. [SecurityCritical] public static string GetPathRoot(string path) { return GetPathRoot(path, true); } #endregion // .NET /// [AlphaFS] Gets the root directory information of the specified path. /// /// Returns the root directory of , such as "C:\", /// or if is , /// or an empty string if does not contain root directory information. /// /// /// The path from which to obtain root directory information. /// will check for invalid path characters. [SecurityCritical] public static string GetPathRoot(string path, bool checkInvalidPathChars) { if (path == null) return null; if (path.Trim().Length == 0) throw new ArgumentException(Resources.Path_Is_Zero_Length_Or_Only_White_Space, "path"); string pathRp = GetRegularPathCore(path, checkInvalidPathChars ? GetFullPathOptions.CheckInvalidPathChars : GetFullPathOptions.None, false); var rootLengthPath = GetRootLength(path, false); var rootLengthPathRp = GetRootLength(pathRp, false); // Check if pathRp is an empty string. if (rootLengthPathRp == 0) if (path.StartsWith(LongPathPrefix, StringComparison.OrdinalIgnoreCase)) return GetLongPathCore(path.Substring(0, rootLengthPath), GetFullPathOptions.None); return path.StartsWith(LongPathUncPrefix, StringComparison.OrdinalIgnoreCase) ? GetLongPathCore(pathRp.Substring(0, rootLengthPathRp), GetFullPathOptions.None) : path.Substring(0, rootLengthPath); } #endregion // GetPathRoot } }