You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

169 lines
8.7 KiB

  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.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. public static partial class File
  28. {
  29. #region SetAttributes
  30. /// <summary>Sets the specified <see cref="FileAttributes"/> of the file or directory on the specified path.</summary>
  31. /// <remarks>
  32. /// Certain file attributes, such as <see cref="FileAttributes.Hidden"/> and <see cref="FileAttributes.ReadOnly"/>, can be combined.
  33. /// Other attributes, such as <see cref="FileAttributes.Normal"/>, must be used alone.
  34. /// </remarks>
  35. /// <remarks>
  36. /// It is not possible to change the <see cref="FileAttributes.Compressed"/> status of a File object using this method.
  37. /// </remarks>
  38. /// <param name="path">The path to the file or directory.</param>
  39. /// <param name="fileAttributes">A bitwise combination of the enumeration values.</param>
  40. /// <overloads>Sets the specified <see cref="FileAttributes"/> of the file or directory on the specified path.</overloads>
  41. [SecurityCritical]
  42. public static void SetAttributes(string path, FileAttributes fileAttributes)
  43. {
  44. SetAttributesCore(false, null, path, fileAttributes, PathFormat.RelativePath);
  45. }
  46. /// <summary>[AlphaFS] Sets the specified <see cref="FileAttributes"/> of the file or directory on the specified path.</summary>
  47. /// <remarks>
  48. /// Certain file attributes, such as <see cref="FileAttributes.Hidden"/> and <see cref="FileAttributes.ReadOnly"/>, can be combined.
  49. /// Other attributes, such as <see cref="FileAttributes.Normal"/>, must be used alone.
  50. /// </remarks>
  51. /// <remarks>
  52. /// It is not possible to change the <see cref="FileAttributes.Compressed"/> status of a File object using this method.
  53. /// </remarks>
  54. /// <param name="path">The path to the file or directory.</param>
  55. /// <param name="fileAttributes">A bitwise combination of the enumeration values.</param>
  56. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  57. [SecurityCritical]
  58. public static void SetAttributes(string path, FileAttributes fileAttributes, PathFormat pathFormat)
  59. {
  60. SetAttributesCore(false, null, path, fileAttributes, pathFormat);
  61. }
  62. #region Transactional
  63. /// <summary>[AlphaFS] Sets the specified <see cref="FileAttributes"/> of the file on the specified path.</summary>
  64. /// <remarks>
  65. /// Certain file attributes, such as <see cref="FileAttributes.Hidden"/> and <see cref="FileAttributes.ReadOnly"/>, can be combined.
  66. /// Other attributes, such as <see cref="FileAttributes.Normal"/>, must be used alone.
  67. /// </remarks>
  68. /// <remarks>
  69. /// It is not possible to change the <see cref="FileAttributes.Compressed"/> status of a File object using this method.
  70. /// </remarks>
  71. /// <param name="transaction">The transaction.</param>
  72. /// <param name="path">The path to the file.</param>
  73. /// <param name="fileAttributes">A bitwise combination of the enumeration values.</param>
  74. [SecurityCritical]
  75. public static void SetAttributesTransacted(KernelTransaction transaction, string path, FileAttributes fileAttributes)
  76. {
  77. SetAttributesCore(false, transaction, path, fileAttributes, PathFormat.RelativePath);
  78. }
  79. /// <summary>[AlphaFS] Sets the specified <see cref="FileAttributes"/> of the file on the specified path.</summary>
  80. /// <remarks>
  81. /// Certain file attributes, such as <see cref="FileAttributes.Hidden"/> and <see cref="FileAttributes.ReadOnly"/>, can be combined.
  82. /// Other attributes, such as <see cref="FileAttributes.Normal"/>, must be used alone.
  83. /// </remarks>
  84. /// <remarks>
  85. /// It is not possible to change the <see cref="FileAttributes.Compressed"/> status of a File object using this method.
  86. /// </remarks>
  87. /// <param name="transaction">The transaction.</param>
  88. /// <param name="path">The path to the file.</param>
  89. /// <param name="fileAttributes">A bitwise combination of the enumeration values.</param>
  90. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  91. [SecurityCritical]
  92. public static void SetAttributesTransacted(KernelTransaction transaction, string path, FileAttributes fileAttributes, PathFormat pathFormat)
  93. {
  94. SetAttributesCore(false, transaction, path, fileAttributes, pathFormat);
  95. }
  96. #endregion // Transacted
  97. #endregion // SetAttributes
  98. #region Internal Methods
  99. /// <summary>Sets the attributes for a Non-/Transacted file/directory.</summary>
  100. /// <remarks>
  101. /// Certain file attributes, such as <see cref="FileAttributes.Hidden"/> and <see cref="FileAttributes.ReadOnly"/>, can be combined.
  102. /// Other attributes, such as <see cref="FileAttributes.Normal"/>, must be used alone.
  103. /// </remarks>
  104. /// <remarks>
  105. /// It is not possible to change the <see cref="FileAttributes.Compressed"/> status of a File object using the SetAttributes method.
  106. /// </remarks>
  107. /// <exception cref="ArgumentException"/>
  108. /// <param name="isFolder">Specifies that <paramref name="path"/> is a file or directory.</param>
  109. /// <param name="transaction">The transaction.</param>
  110. /// <param name="path">The name of the file or directory whose attributes are to be set.</param>
  111. /// <param name="fileAttributes">
  112. /// The attributes to set for the file or directory. Note that all other values override <see cref="FileAttributes.Normal"/>.
  113. /// </param>
  114. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  115. [SecurityCritical]
  116. internal static void SetAttributesCore(bool isFolder, KernelTransaction transaction, string path, FileAttributes fileAttributes, PathFormat pathFormat)
  117. {
  118. var pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  119. var success = transaction == null || !NativeMethods.IsAtLeastWindowsVista
  120. // SetFileAttributes()
  121. // In the ANSI version of this function, the name is limited to MAX_PATH characters.
  122. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  123. // 2013-01-13: MSDN confirms LongPath usage.
  124. ? NativeMethods.SetFileAttributes(pathLp, fileAttributes)
  125. : NativeMethods.SetFileAttributesTransacted(pathLp, fileAttributes, transaction.SafeHandle);
  126. var lastError = (uint) Marshal.GetLastWin32Error();
  127. if (!success)
  128. {
  129. switch (lastError)
  130. {
  131. // MSDN: .NET 3.5+: ArgumentException: FileSystemInfo().Attributes
  132. case Win32Errors.ERROR_INVALID_PARAMETER:
  133. throw new ArgumentException(Resources.Invalid_File_Attribute);
  134. case Win32Errors.ERROR_FILE_NOT_FOUND:
  135. if (isFolder)
  136. lastError = (int) Win32Errors.ERROR_PATH_NOT_FOUND;
  137. // MSDN: .NET 3.5+: DirectoryNotFoundException: The specified path is invalid, (for example, it is on an unmapped drive).
  138. // MSDN: .NET 3.5+: FileNotFoundException: The file cannot be found.
  139. NativeError.ThrowException(lastError, pathLp);
  140. break;
  141. }
  142. NativeError.ThrowException(lastError, pathLp);
  143. }
  144. }
  145. #endregion // Internal Methods
  146. }
  147. }