/* 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.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Runtime.InteropServices; using System.Security; using System.Security.AccessControl; namespace Alphaleonis.Win32.Filesystem { partial class Directory { #region .NET /// Creates all directories and subdirectories in the specified path unless they already exist. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. [SecurityCritical] public static DirectoryInfo CreateDirectory(string path) { return CreateDirectoryCore(null, path, null, null, false, PathFormat.RelativePath); } #endregion // .NET /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, PathFormat pathFormat) { return CreateDirectoryCore(null, path, null, null, false, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, bool compress) { return CreateDirectoryCore(null, path, null, null, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(null, path, null, null, compress, pathFormat); } /// Creates all the directories in the specified path, unless the already exist, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The access control to apply to the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, DirectorySecurity directorySecurity) { return CreateDirectoryCore(null, path, null, directorySecurity, false, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The access control to apply to the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, DirectorySecurity directorySecurity, PathFormat pathFormat) { return CreateDirectoryCore(null, path, null, directorySecurity, false, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The access control to apply to the directory. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, DirectorySecurity directorySecurity, bool compress) { return CreateDirectoryCore(null, path, null, directorySecurity, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The access control to apply to the directory. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, DirectorySecurity directorySecurity, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(null, path, null, directorySecurity, compress, pathFormat); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath) { return CreateDirectoryCore(null, path, templatePath, null, false, PathFormat.RelativePath); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath, PathFormat pathFormat) { return CreateDirectoryCore(null, path, templatePath, null, false, pathFormat); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath, bool compress) { return CreateDirectoryCore(null, path, templatePath, null, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(null, path, templatePath, null, compress, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath, DirectorySecurity directorySecurity) { return CreateDirectoryCore(null, path, templatePath, directorySecurity, false, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath, DirectorySecurity directorySecurity, PathFormat pathFormat) { return CreateDirectoryCore(null, path, templatePath, directorySecurity, false, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath, DirectorySecurity directorySecurity, bool compress) { return CreateDirectoryCore(null, path, templatePath, directorySecurity, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectory(string path, string templatePath, DirectorySecurity directorySecurity, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(null, path, templatePath, directorySecurity, compress, pathFormat); } #region Transactional /// Creates all directories and subdirectories in the specified path unless they already exist. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path) { return CreateDirectoryCore(transaction, path, null, null, false, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, null, null, false, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, bool compress) { return CreateDirectoryCore(transaction, path, null, null, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, null, null, compress, pathFormat); } /// Creates all the directories in the specified path, unless the already exist, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The access control to apply to the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, DirectorySecurity directorySecurity) { return CreateDirectoryCore(transaction, path, null, directorySecurity, false, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The access control to apply to the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, DirectorySecurity directorySecurity, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, null, directorySecurity, false, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The access control to apply to the directory. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, DirectorySecurity directorySecurity, bool compress) { return CreateDirectoryCore(transaction, path, null, directorySecurity, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path, applying the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The access control to apply to the directory. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, DirectorySecurity directorySecurity, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, null, directorySecurity, compress, pathFormat); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath) { return CreateDirectoryCore(transaction, path, templatePath, null, false, PathFormat.RelativePath); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, templatePath, null, false, pathFormat); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath, bool compress) { return CreateDirectoryCore(transaction, path, templatePath, null, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates a new directory, with the attributes of a specified template directory. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, templatePath, null, compress, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath, DirectorySecurity directorySecurity) { return CreateDirectoryCore(transaction, path, templatePath, directorySecurity, false, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath, DirectorySecurity directorySecurity, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, templatePath, directorySecurity, false, pathFormat); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. /// When compresses the directory. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath, DirectorySecurity directorySecurity, bool compress) { return CreateDirectoryCore(transaction, path, templatePath, directorySecurity, compress, PathFormat.RelativePath); } /// [AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security. /// An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. /// The access control to apply to the directory. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] [SecurityCritical] public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, string templatePath, DirectorySecurity directorySecurity, bool compress, PathFormat pathFormat) { return CreateDirectoryCore(transaction, path, templatePath, directorySecurity, compress, pathFormat); } #endregion // Transactional #region Internal Methods /// Creates a new directory with the attributes of a specified template directory (if one is specified). /// If the underlying file system supports security on files and directories, the function applies the specified security descriptor to the new directory. /// The new directory retains the other attributes of the specified template directory. /// /// /// Returns an object that represents the directory at the specified path. /// This object is returned regardless of whether a directory at the specified path already exists. /// /// /// /// /// /// /// /// The transaction. /// The directory to create. /// The path of the directory to use as a template when creating the new directory. May be to indicate that no template should be used. /// The access control to apply to the directory, may be null. /// When compresses the directory. /// Indicates the format of the path parameter(s). [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] [SecurityCritical] internal static DirectoryInfo CreateDirectoryCore(KernelTransaction transaction, string path, string templatePath, ObjectSecurity directorySecurity, bool compress, PathFormat pathFormat) { bool fullCheck = pathFormat == PathFormat.RelativePath; Path.CheckSupportedPathFormat(path, fullCheck, fullCheck); Path.CheckSupportedPathFormat(templatePath, fullCheck, fullCheck); string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator); // Return DirectoryInfo instance if the directory specified by path already exists. if (File.ExistsCore(true, transaction, pathLp, PathFormat.LongFullPath)) return new DirectoryInfo(transaction, pathLp, PathFormat.LongFullPath); // MSDN: .NET 3.5+: IOException: The directory specified by path is a file or the network name was not found. if (File.ExistsCore(false, transaction, pathLp, PathFormat.LongFullPath)) NativeError.ThrowException(Win32Errors.ERROR_ALREADY_EXISTS, pathLp); string templatePathLp = Utils.IsNullOrWhiteSpace(templatePath) ? null : Path.GetExtendedLengthPathCore(transaction, templatePath, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator); #region Construct Full Path string longPathPrefix = Path.IsUncPathCore(path, false, false) ? Path.LongPathUncPrefix : Path.LongPathPrefix; path = Path.GetRegularPathCore(pathLp, GetFullPathOptions.None, false); int length = path.Length; if (length >= 2 && Path.IsDVsc(path[length - 1], false)) --length; int rootLength = Path.GetRootLength(path, false); if (length == 2 && Path.IsDVsc(path[1], false)) throw new ArgumentException(Resources.Cannot_Create_Directory, path); // Check if directories are missing. var list = new Stack(100); if (length > rootLength) { for (int index = length - 1; index >= rootLength; --index) { string path1 = path.Substring(0, index + 1); string path2 = longPathPrefix + path1.TrimStart('\\'); if (!File.ExistsCore(true, transaction, path2, PathFormat.LongFullPath)) list.Push(path2); while (index > rootLength && !Path.IsDVsc(path[index], false)) --index; } } #endregion // Construct Full Path // Directory security. using (var securityAttributes = new Security.NativeMethods.SecurityAttributes(directorySecurity)) { // Create the directory paths. while (list.Count > 0) { string folderLp = list.Pop(); // In the ANSI version of this function, the name is limited to 248 characters. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. // 2013-01-13: MSDN confirms LongPath usage. if (!(transaction == null || !NativeMethods.IsAtLeastWindowsVista ? (templatePathLp == null ? NativeMethods.CreateDirectory(folderLp, securityAttributes) : NativeMethods.CreateDirectoryEx(templatePathLp, folderLp, securityAttributes)) : NativeMethods.CreateDirectoryTransacted(templatePathLp, folderLp, securityAttributes, transaction.SafeHandle))) { int lastError = Marshal.GetLastWin32Error(); switch ((uint) lastError) { // MSDN: .NET 3.5+: If the directory already exists, this method does nothing. // MSDN: .NET 3.5+: IOException: The directory specified by path is a file. case Win32Errors.ERROR_ALREADY_EXISTS: if (File.ExistsCore(false, transaction, pathLp, PathFormat.LongFullPath)) NativeError.ThrowException(lastError, pathLp); if (File.ExistsCore(false, transaction, folderLp, PathFormat.LongFullPath)) NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, folderLp); break; case Win32Errors.ERROR_BAD_NET_NAME: NativeError.ThrowException(lastError, pathLp); break; case Win32Errors.ERROR_DIRECTORY: // MSDN: .NET 3.5+: NotSupportedException: path contains a colon character (:) that is not part of a drive label ("C:\"). throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.Unsupported_Path_Format, path)); default: NativeError.ThrowException(lastError, folderLp); break; } } else if (compress) Device.ToggleCompressionCore(true, transaction, folderLp, true, PathFormat.LongFullPath); } return new DirectoryInfo(transaction, pathLp, PathFormat.LongFullPath); } } #endregion // Internal Methods } }