選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DirectoryInfo.CreateSubdirectory.cs 9.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.Security;
  24. using System.Security.AccessControl;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. partial class DirectoryInfo
  28. {
  29. #region .NET
  30. /// <summary>Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the <see cref="DirectoryInfo"/> class.</summary>
  31. /// <param name="path">The specified path. This cannot be a different disk volume.</param>
  32. /// <returns>The last directory specified in <paramref name="path"/>.</returns>
  33. /// <remarks>
  34. /// Any and all directories specified in path are created, unless some part of path is invalid.
  35. /// The path parameter specifies a directory path, not a file path.
  36. /// If the subdirectory already exists, this method does nothing.
  37. /// </remarks>
  38. [SecurityCritical]
  39. public DirectoryInfo CreateSubdirectory(string path)
  40. {
  41. return CreateSubdirectoryCore(path, null, null, false);
  42. }
  43. /// <summary>Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the <see cref="DirectoryInfo"/> class.</summary>
  44. /// <param name="path">The specified path. This cannot be a different disk volume.</param>
  45. /// <param name="directorySecurity">The <see cref="DirectorySecurity"/> security to apply.</param>
  46. /// <returns>The last directory specified in <paramref name="path"/>.</returns>
  47. /// <remarks>
  48. /// Any and all directories specified in path are created, unless some part of path is invalid.
  49. /// The path parameter specifies a directory path, not a file path.
  50. /// If the subdirectory already exists, this method does nothing.
  51. /// </remarks>
  52. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  53. [SecurityCritical]
  54. public DirectoryInfo CreateSubdirectory(string path, DirectorySecurity directorySecurity)
  55. {
  56. return CreateSubdirectoryCore(path, null, directorySecurity, false);
  57. }
  58. #endregion // .NET
  59. #region AlphaFS
  60. /// <summary>[AlphaFS] Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the <see cref="DirectoryInfo"/> class.</summary>
  61. /// <returns>The last directory specified in <paramref name="path"/>.</returns>
  62. /// <remarks>
  63. /// Any and all directories specified in path are created, unless some part of path is invalid.
  64. /// The path parameter specifies a directory path, not a file path.
  65. /// If the subdirectory already exists, this method does nothing.
  66. /// </remarks>
  67. /// <param name="path">The specified path. This cannot be a different disk volume.</param>
  68. /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
  69. [SecurityCritical]
  70. public DirectoryInfo CreateSubdirectory(string path, bool compress)
  71. {
  72. return CreateSubdirectoryCore(path, null, null, compress);
  73. }
  74. /// <summary>[AlphaFS] Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the <see cref="DirectoryInfo"/> class.</summary>
  75. /// <param name="path">The specified path. This cannot be a different disk volume.</param>
  76. /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
  77. /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
  78. /// <returns>The last directory specified in <paramref name="path"/>.</returns>
  79. /// <remarks>
  80. /// Any and all directories specified in path are created, unless some part of path is invalid.
  81. /// The path parameter specifies a directory path, not a file path.
  82. /// If the subdirectory already exists, this method does nothing.
  83. /// </remarks>
  84. [SecurityCritical]
  85. public DirectoryInfo CreateSubdirectory(string path, string templatePath, bool compress)
  86. {
  87. return CreateSubdirectoryCore(path, templatePath, null, compress);
  88. }
  89. /// <summary>[AlphaFS] Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the <see cref="DirectoryInfo"/> class.</summary>
  90. /// <param name="path">The specified path. This cannot be a different disk volume.</param>
  91. /// <param name="directorySecurity">The <see cref="DirectorySecurity"/> security to apply.</param>
  92. /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
  93. /// <returns>The last directory specified in <paramref name="path"/>.</returns>
  94. /// <remarks>
  95. /// Any and all directories specified in path are created, unless some part of path is invalid.
  96. /// The path parameter specifies a directory path, not a file path.
  97. /// If the subdirectory already exists, this method does nothing.
  98. /// </remarks>
  99. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  100. [SecurityCritical]
  101. public DirectoryInfo CreateSubdirectory(string path, DirectorySecurity directorySecurity, bool compress)
  102. {
  103. return CreateSubdirectoryCore(path, null, directorySecurity, compress);
  104. }
  105. /// <summary>[AlphaFS] Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the <see cref="DirectoryInfo"/> class.</summary>
  106. /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
  107. /// <param name="path">The specified path. This cannot be a different disk volume.</param>
  108. /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
  109. /// <param name="directorySecurity">The <see cref="DirectorySecurity"/> security to apply.</param>
  110. /// <returns>The last directory specified in <paramref name="path"/>.</returns>
  111. /// <remarks>
  112. /// Any and all directories specified in path are created, unless some part of path is invalid.
  113. /// The path parameter specifies a directory path, not a file path.
  114. /// If the subdirectory already exists, this method does nothing.
  115. /// </remarks>
  116. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  117. [SecurityCritical]
  118. public DirectoryInfo CreateSubdirectory(string path, string templatePath, DirectorySecurity directorySecurity, bool compress)
  119. {
  120. return CreateSubdirectoryCore(path, templatePath, directorySecurity, compress);
  121. }
  122. #endregion // AlphaFS
  123. #region Internal Methods
  124. /// <summary>Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.</summary>
  125. /// <returns>The last directory specified in path as an <see cref="DirectoryInfo"/> object.</returns>
  126. /// <remarks>
  127. /// Any and all directories specified in path are created, unless some part of path is invalid.
  128. /// The path parameter specifies a directory path, not a file path.
  129. /// If the subdirectory already exists, this method does nothing.
  130. /// </remarks>
  131. /// <param name="path">The specified path. This cannot be a different disk volume or Universal Naming Convention (UNC) name.</param>
  132. /// <param name="templatePath">The path of the directory to use as a template when creating the new directory.</param>
  133. /// <param name="directorySecurity">The <see cref="DirectorySecurity"/> security to apply.</param>
  134. /// <param name="compress">When <see langword="true"/> compresses the directory.</param>
  135. [SecurityCritical]
  136. private DirectoryInfo CreateSubdirectoryCore(string path, string templatePath, ObjectSecurity directorySecurity, bool compress)
  137. {
  138. string pathLp = Path.CombineCore(false, LongFullName, path);
  139. string templatePathLp = templatePath == null ? null :
  140. Path.GetExtendedLengthPathCore(Transaction, templatePath, PathFormat.RelativePath, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator);
  141. if (string.Compare(LongFullName, 0, pathLp, 0, LongFullName.Length, StringComparison.OrdinalIgnoreCase) != 0)
  142. throw new ArgumentException(Resources.Invalid_Subpath, pathLp);
  143. return Directory.CreateDirectoryCore(Transaction, pathLp, templatePathLp, directorySecurity, compress, PathFormat.LongFullPath);
  144. }
  145. #endregion // Internal Methods
  146. }
  147. }