Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

209 rindas
15 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.Diagnostics.CodeAnalysis;
  22. using System.Runtime.InteropServices;
  23. using System.Security;
  24. using System.Text;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. internal static partial class NativeMethods
  28. {
  29. /// <summary>
  30. /// Creates a new directory.
  31. /// <para>If the underlying file system supports security on files and directories,</para>
  32. /// <para>the function applies a specified security descriptor to the new directory.</para>
  33. /// </summary>
  34. /// <remarks>
  35. /// <para>Some file systems, such as the NTFS file system, support compression or encryption for individual files and
  36. /// directories.</para>
  37. /// <para>On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent
  38. /// directory.</para>
  39. /// <para>An application can obtain a handle to a directory by calling <see cref="CreateFile"/> with the FILE_FLAG_BACKUP_SEMANTICS
  40. /// flag set.</para>
  41. /// <para>Minimum supported client: Windows XP [desktop apps | Windows Store apps]</para>
  42. /// <para>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps]</para>
  43. /// </remarks>
  44. /// <param name="lpPathName">Full pathname of the file.</param>
  45. /// <param name="lpSecurityAttributes">The security attributes.</param>
  46. /// <returns>
  47. /// <para>If the function succeeds, the return value is nonzero.</para>
  48. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  49. /// </returns>
  50. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  51. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryW"), SuppressUnmanagedCodeSecurity]
  52. [return: MarshalAs(UnmanagedType.Bool)]
  53. internal static extern bool CreateDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes);
  54. /// <summary>
  55. /// Creates a new directory with the attributes of a specified template directory.
  56. /// <para>If the underlying file system supports security on files and directories,</para>
  57. /// <para>the function applies a specified security descriptor to the new directory.</para>
  58. /// <para>The new directory retains the other attributes of the specified template directory.</para>
  59. /// </summary>
  60. /// <remarks>
  61. /// <para>The CreateDirectoryEx function allows you to create directories that inherit stream information from other directories.</para>
  62. /// <para>This function is useful, for example, when you are using Macintosh directories, which have a resource stream</para>
  63. /// <para>that is needed to properly identify directory contents as an attribute.</para>
  64. /// <para>Some file systems, such as the NTFS file system, support compression or encryption for individual files and
  65. /// directories.</para>
  66. /// <para>On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent
  67. /// directory.</para>
  68. /// <para>You can obtain a handle to a directory by calling the <see cref="CreateFile"/> function with the FILE_FLAG_BACKUP_SEMANTICS
  69. /// flag set.</para>
  70. /// <para>Minimum supported client: Windows XP [desktop apps only]</para>
  71. /// <para>Minimum supported server: Windows Server 2003 [desktop apps only]</para>
  72. /// </remarks>
  73. /// <param name="lpTemplateDirectory">Pathname of the template directory.</param>
  74. /// <param name="lpPathName">Full pathname of the file.</param>
  75. /// <param name="lpSecurityAttributes">The security attributes.</param>
  76. /// <returns>
  77. /// <para>If the function succeeds, the return value is nonzero.</para>
  78. /// <para>If the function fails, the return value is zero (0). To get extended error information, call GetLastError.</para>
  79. /// </returns>
  80. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  81. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryExW"), SuppressUnmanagedCodeSecurity]
  82. [return: MarshalAs(UnmanagedType.Bool)]
  83. internal static extern bool CreateDirectoryEx([MarshalAs(UnmanagedType.LPWStr)] string lpTemplateDirectory, [MarshalAs(UnmanagedType.LPWStr)] string lpPathName, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes);
  84. /// <summary>
  85. /// Creates a new directory as a transacted operation, with the attributes of a specified template directory.
  86. /// <para>If the underlying file system supports security on files and directories,</para>
  87. /// <para>the function applies a specified security descriptor to the new directory.</para>
  88. /// <para>The new directory retains the other attributes of the specified template directory.</para>
  89. /// </summary>
  90. /// <remarks>
  91. /// <para>The CreateDirectoryTransacted function allows you to create directories that inherit stream information from other
  92. /// directories.</para>
  93. /// <para>This function is useful, for example, when you are using Macintosh directories, which have a resource stream</para>
  94. /// <para>that is needed to properly identify directory contents as an attribute.</para>
  95. /// <para>Some file systems, such as the NTFS file system, support compression or encryption for individual files and
  96. /// directories.</para>
  97. /// <para>On volumes formatted for such a file system, a new directory inherits the compression and encryption attributes of its parent
  98. /// directory.</para>
  99. /// <para>You can obtain a handle to a directory by calling the <see cref="CreateFileTransacted"/> function with the
  100. /// FILE_FLAG_BACKUP_SEMANTICS flag set.</para>
  101. /// <para>Minimum supported client: Windows XP [desktop apps only]</para>
  102. /// <para>Minimum supported server: Windows Server 2003 [desktop apps only]</para>
  103. /// </remarks>
  104. /// <param name="lpTemplateDirectory">Pathname of the template directory.</param>
  105. /// <param name="lpNewDirectory">Pathname of the new directory.</param>
  106. /// <param name="lpSecurityAttributes">The security attributes.</param>
  107. /// <param name="hTransaction">The transaction.</param>
  108. /// <returns>
  109. /// <para>If the function succeeds, the return value is nonzero.</para>
  110. /// <para>If the function fails, the return value is zero (0). To get extended error information, call GetLastError.</para>
  111. /// <para>This function fails with ERROR_EFS_NOT_ALLOWED_IN_TRANSACTION if you try to create a</para>
  112. /// <para>child directory with a parent directory that has encryption disabled.</para>
  113. /// </returns>
  114. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  115. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryTransactedW"), SuppressUnmanagedCodeSecurity]
  116. [return: MarshalAs(UnmanagedType.Bool)]
  117. internal static extern bool CreateDirectoryTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpTemplateDirectory, [MarshalAs(UnmanagedType.LPWStr)] string lpNewDirectory, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes, SafeHandle hTransaction);
  118. /// <summary>
  119. /// Retrieves the current directory for the current process.
  120. /// </summary>
  121. /// <remarks>
  122. /// <para>The RemoveDirectory function marks a directory for deletion on close.</para>
  123. /// <para>Therefore, the directory is not removed until the last handle to the directory is closed.</para>
  124. /// <para>RemoveDirectory removes a directory junction, even if the contents of the target are not empty;</para>
  125. /// <para>the function removes directory junctions regardless of the state of the target object.</para>
  126. /// <para>Minimum supported client: Windows XP [desktop apps | Windows Store apps]</para>
  127. /// <para>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps]</para>
  128. /// </remarks>
  129. /// <param name="nBufferLength">The length of the buffer for the current directory string, in TCHARs. The buffer length must include room for a terminating null character.</param>
  130. /// <param name="lpBuffer">
  131. /// <para>A pointer to the buffer that receives the current directory string. This null-terminated string specifies the absolute path to the current directory.</para>
  132. /// <para>To determine the required buffer size, set this parameter to NULL and the nBufferLength parameter to 0.</para>
  133. /// </param>
  134. /// <returns>
  135. /// <para>If the function succeeds, the return value specifies the number of characters that are written to the buffer, not including the terminating null character.</para>
  136. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  137. /// </returns>
  138. [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api")]
  139. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  140. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetCurrentDirectoryW"), SuppressUnmanagedCodeSecurity]
  141. [return: MarshalAs(UnmanagedType.U4)]
  142. internal static extern uint GetCurrentDirectory([MarshalAs(UnmanagedType.U4)] uint nBufferLength, StringBuilder lpBuffer);
  143. /// <summary>
  144. /// Deletes an existing empty directory.
  145. /// </summary>
  146. /// <remarks>
  147. /// <para>The RemoveDirectory function marks a directory for deletion on close.</para>
  148. /// <para>Therefore, the directory is not removed until the last handle to the directory is closed.</para>
  149. /// <para>RemoveDirectory removes a directory junction, even if the contents of the target are not empty;</para>
  150. /// <para>the function removes directory junctions regardless of the state of the target object.</para>
  151. /// <para>Minimum supported client: Windows XP [desktop apps | Windows Store apps]</para>
  152. /// <para>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps]</para>
  153. /// </remarks>
  154. /// <param name="lpPathName">Full pathname of the file.</param>
  155. /// <returns>
  156. /// <para>If the function succeeds, the return value is nonzero.</para>
  157. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  158. /// </returns>
  159. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  160. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RemoveDirectoryW"), SuppressUnmanagedCodeSecurity]
  161. [return: MarshalAs(UnmanagedType.Bool)]
  162. internal static extern bool RemoveDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName);
  163. /// <summary>
  164. /// Deletes an existing empty directory as a transacted operation.
  165. /// </summary>
  166. /// <remarks>
  167. /// <para>The RemoveDirectoryTransacted function marks a directory for deletion on close.</para>
  168. /// <para>Therefore, the directory is not removed until the last handle to the directory is closed.</para>
  169. /// <para>RemoveDirectory removes a directory junction, even if the contents of the target are not empty;</para>
  170. /// <para>the function removes directory junctions regardless of the state of the target object.</para>
  171. /// <para>Minimum supported client: Windows Vista [desktop apps only]</para>
  172. /// <para>Minimum supported server: Windows Server 2008 [desktop apps only]</para>
  173. /// </remarks>
  174. /// <param name="lpPathName">Full pathname of the file.</param>
  175. /// <param name="hTransaction">The transaction.</param>
  176. /// <returns>
  177. /// <para>If the function succeeds, the return value is nonzero.</para>
  178. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  179. /// </returns>
  180. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  181. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RemoveDirectoryTransactedW"), SuppressUnmanagedCodeSecurity]
  182. [return: MarshalAs(UnmanagedType.Bool)]
  183. internal static extern bool RemoveDirectoryTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, SafeHandle hTransaction);
  184. /// <summary>
  185. /// Changes the current directory for the current process.
  186. /// </summary>
  187. /// <param name="lpPathName">
  188. /// <para>The path to the new current directory. This parameter may specify a relative path or a full path. In either case, the full path of the specified directory is calculated and stored as the current directory.</para>
  189. /// </param>
  190. /// <returns>
  191. /// <para>If the function succeeds, the return value is nonzero.</para>
  192. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  193. /// </returns>
  194. [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api")]
  195. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  196. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetCurrentDirectoryW"), SuppressUnmanagedCodeSecurity]
  197. [return: MarshalAs(UnmanagedType.Bool)]
  198. internal static extern bool SetCurrentDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName);
  199. }
  200. }