|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800 |
- /* 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
-
- /// <summary>Creates all directories and subdirectories in the specified path unless they already exist.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path)
- {
- return CreateDirectoryCore(null, path, null, null, false, PathFormat.RelativePath);
- }
-
- #endregion // .NET
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path, PathFormat pathFormat)
- {
- return CreateDirectoryCore(null, path, null, null, false, pathFormat);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path, bool compress)
- {
- return CreateDirectoryCore(null, path, null, null, compress, PathFormat.RelativePath);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path, bool compress, PathFormat pathFormat)
- {
- return CreateDirectoryCore(null, path, null, null, compress, pathFormat);
- }
-
-
-
- /// <summary>Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path, DirectorySecurity directorySecurity)
- {
- return CreateDirectoryCore(null, path, null, directorySecurity, false, PathFormat.RelativePath);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path, DirectorySecurity directorySecurity, PathFormat pathFormat)
- {
- return CreateDirectoryCore(null, path, null, directorySecurity, false, pathFormat);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path, string templatePath)
- {
- return CreateDirectoryCore(null, path, templatePath, null, false, PathFormat.RelativePath);
- }
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectory(string path, string templatePath, PathFormat pathFormat)
- {
- return CreateDirectoryCore(null, path, templatePath, null, false, pathFormat);
- }
-
-
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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
-
- /// <summary>Creates all directories and subdirectories in the specified path unless they already exist.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- [SecurityCritical]
- public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path)
- {
- return CreateDirectoryCore(transaction, path, null, null, false, PathFormat.RelativePath);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
- [SecurityCritical]
- public static DirectoryInfo CreateDirectoryTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
- {
- return CreateDirectoryCore(transaction, path, null, null, false, pathFormat);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path, applying the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates a new directory, with the attributes of a specified template directory.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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);
- }
-
-
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- [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);
- }
-
- /// <summary>[AlphaFS] Creates all the directories in the specified path of a specified template directory and applies the specified Windows security.</summary>
- /// <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.</returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
- /// <param name="directorySecurity">The access control to apply to the directory.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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
-
- /// <summary>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.
- /// </summary>
- /// <returns>
- /// <para>Returns an object that represents the directory at the specified path.</para>
- /// <para>This object is returned regardless of whether a directory at the specified path already exists.</para>
- /// </returns>
- /// <exception cref="ArgumentException"/>
- /// <exception cref="ArgumentNullException"/>
- /// <exception cref="DirectoryNotFoundException"/>
- /// <exception cref="IOException"/>
- /// <exception cref="NotSupportedException"/>
- /// <exception cref="UnauthorizedAccessException"/>
- /// <param name="transaction">The transaction.</param>
- /// <param name="path">The directory to create.</param>
- /// <param name="templatePath">The path of the directory to use as a template when creating the new directory. May be <see langword="null"/> to indicate that no template should be used.</param>
- /// <param name="directorySecurity">The <see cref="DirectorySecurity"/> access control to apply to the directory, may be null.</param>
- /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
- /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
- [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<string>(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
- }
- }
|