Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

File.SetAttributes.cs 9.0 KiB

vor 8 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, false, 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, false, 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, false, 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, false, 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="continueOnNotExist">
  115. /// <see langword="true"/> does not throw an Exception when the file system object does not exist.
  116. /// </param>
  117. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  118. [SecurityCritical]
  119. internal static void SetAttributesCore(bool isFolder, KernelTransaction transaction, string path, FileAttributes fileAttributes, bool continueOnNotExist, PathFormat pathFormat)
  120. {
  121. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  122. if (!(transaction == null || !NativeMethods.IsAtLeastWindowsVista
  123. // SetFileAttributes()
  124. // In the ANSI version of this function, the name is limited to MAX_PATH characters.
  125. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  126. // 2013-01-13: MSDN confirms LongPath usage.
  127. ? NativeMethods.SetFileAttributes(pathLp, fileAttributes)
  128. : NativeMethods.SetFileAttributesTransacted(pathLp, fileAttributes, transaction.SafeHandle)))
  129. {
  130. if (continueOnNotExist)
  131. return;
  132. uint lastError = (uint)Marshal.GetLastWin32Error();
  133. switch (lastError)
  134. {
  135. // MSDN: .NET 3.5+: ArgumentException: FileSystemInfo().Attributes
  136. case Win32Errors.ERROR_INVALID_PARAMETER:
  137. throw new ArgumentException(Resources.Invalid_File_Attribute);
  138. case Win32Errors.ERROR_FILE_NOT_FOUND:
  139. if (isFolder)
  140. lastError = (int)Win32Errors.ERROR_PATH_NOT_FOUND;
  141. // MSDN: .NET 3.5+: DirectoryNotFoundException: The specified path is invalid, (for example, it is on an unmapped drive).
  142. // MSDN: .NET 3.5+: FileNotFoundException: The file cannot be found.
  143. NativeError.ThrowException(lastError, pathLp);
  144. break;
  145. }
  146. NativeError.ThrowException(lastError, pathLp);
  147. }
  148. }
  149. #endregion // Internal Methods
  150. }
  151. }