Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

761 řádky
61 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 Alphaleonis.Win32.Security;
  22. using Microsoft.Win32.SafeHandles;
  23. using System;
  24. using System.Diagnostics.CodeAnalysis;
  25. using System.IO;
  26. using System.Runtime.InteropServices;
  27. using System.Security;
  28. using System.Security.AccessControl;
  29. using System.Text;
  30. namespace Alphaleonis.Win32.Filesystem
  31. {
  32. internal static partial class NativeMethods
  33. {
  34. /// <summary>
  35. /// Copies an existing file to a new file, notifying the application of its progress through a callback function.
  36. /// </summary>
  37. /// <remarks>
  38. /// <para>This function fails with ERROR_ACCESS_DENIED if the destination file already exists and has the FILE_ATTRIBUTE_HIDDEN or
  39. /// FILE_ATTRIBUTE_READONLY attribute set.</para>
  40. /// <para>This function preserves extended attributes, OLE structured storage, NTFS file system alternate data streams, security
  41. /// resource attributes, and file attributes.</para>
  42. /// <para>Windows 7, Windows Server 2008 R2, Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP:
  43. /// Security resource attributes (ATTRIBUTE_SECURITY_INFORMATION) for the existing file are not copied to the new file until
  44. /// Windows 8 and Windows Server 2012.</para>
  45. /// <para>Minimum supported client: Windows XP [desktop apps only]</para>
  46. /// <para>Minimum supported server: Windows Server 2003 [desktop apps only]</para>
  47. /// </remarks>
  48. /// <param name="lpExistingFileName">Filename of the existing file.</param>
  49. /// <param name="lpNewFileName">Filename of the new file.</param>
  50. /// <param name="lpProgressRoutine">The progress routine.</param>
  51. /// <param name="lpData">The data.</param>
  52. /// <param name="pbCancel">[out] The pb cancel.</param>
  53. /// <param name="dwCopyFlags">The copy flags.</param>
  54. /// <returns>
  55. /// <para>If the function succeeds, the return value is nonzero.</para>
  56. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  57. /// </returns>
  58. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  59. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CopyFileExW"), SuppressUnmanagedCodeSecurity]
  60. [return: MarshalAs(UnmanagedType.Bool)]
  61. internal static extern bool CopyFileEx([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyMoveProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.Bool)] out bool pbCancel, CopyOptions dwCopyFlags);
  62. /// <summary>
  63. /// Copies an existing file to a new file as a transacted operation, notifying the application of its progress through a callback
  64. /// function.
  65. /// </summary>
  66. /// <remarks>
  67. /// <para>This function fails with ERROR_ACCESS_DENIED if the destination file already exists and has the FILE_ATTRIBUTE_HIDDEN or
  68. /// FILE_ATTRIBUTE_READONLY attribute set.</para>
  69. /// <para>This function preserves extended attributes, OLE structured storage, NTFS file system alternate data streams, security
  70. /// resource attributes, and file attributes.</para>
  71. /// <para>Windows 7, Windows Server 2008 R2, Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP:
  72. /// Security resource attributes (ATTRIBUTE_SECURITY_INFORMATION) for the existing file are not copied to the new file until
  73. /// Windows 8 and Windows Server 2012.</para>
  74. /// <para>Minimum supported client: Windows Vista [desktop apps only]</para>
  75. /// <para>Minimum supported server: Windows Server 2008 [desktop apps only]</para>
  76. /// </remarks>
  77. /// <returns>
  78. /// <para>If the function succeeds, the return value is nonzero.</para>
  79. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  80. /// </returns>
  81. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  82. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CopyFileTransactedW"), SuppressUnmanagedCodeSecurity]
  83. [return: MarshalAs(UnmanagedType.Bool)]
  84. internal static extern bool CopyFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyMoveProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.Bool)] out bool pbCancel, CopyOptions dwCopyFlags, SafeHandle hTransaction);
  85. /// <summary>
  86. /// Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: file, file stream, directory, physical
  87. /// disk, volume, console buffer, tape drive, communications resource, mailslot, and pipe.
  88. /// </summary>
  89. /// <remarks>Minimum supported client: Windows XP.</remarks>
  90. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  91. /// <returns>
  92. /// If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot. If the
  93. /// function fails, the return value is Win32Errors.ERROR_INVALID_HANDLE. To get extended error information, call GetLastError.
  94. /// </returns>
  95. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  96. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateFileW"), SuppressUnmanagedCodeSecurity]
  97. internal static extern SafeFileHandle CreateFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileSystemRights dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition, [MarshalAs(UnmanagedType.U4)] ExtendedFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);
  98. /// <summary>
  99. /// Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: file, file stream, directory, physical
  100. /// disk, volume, console buffer, tape drive, communications resource, mailslot, and pipe.
  101. /// </summary>
  102. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  103. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  104. /// <returns>
  105. /// If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot. If the
  106. /// function fails, the return value is Win32Errors.ERROR_INVALID_HANDLE". To get extended error information, call GetLastError.
  107. /// </returns>
  108. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  109. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateFileTransactedW"), SuppressUnmanagedCodeSecurity]
  110. internal static extern SafeFileHandle CreateFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileSystemRights dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, [MarshalAs(UnmanagedType.LPStruct)] Security.NativeMethods.SecurityAttributes lpSecurityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition, [MarshalAs(UnmanagedType.U4)] ExtendedFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile, SafeHandle hTransaction, IntPtr pusMiniVersion, IntPtr pExtendedParameter);
  111. /// <summary>Creates or opens a named or unnamed file mapping object for a specified file.</summary>
  112. /// <remarks>Minimum supported client: Windows XP.</remarks>
  113. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  114. /// <returns>
  115. /// If the function succeeds, the return value is a handle to the newly created file mapping object. If the function fails, the return
  116. /// value is <see langword="null"/>.
  117. /// </returns>
  118. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  119. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode, EntryPoint = "CreateFileMappingW"), SuppressUnmanagedCodeSecurity]
  120. internal static extern SafeFileHandle CreateFileMapping(SafeFileHandle hFile, SafeHandle lpSecurityAttributes, [MarshalAs(UnmanagedType.U4)] uint flProtect, [MarshalAs(UnmanagedType.U4)] uint dwMaximumSizeHigh, [MarshalAs(UnmanagedType.U4)] uint dwMaximumSizeLow, [MarshalAs(UnmanagedType.LPWStr)] string lpName);
  121. /// <summary>
  122. /// Establishes a hard link between an existing file and a new file. This function is only supported on the NTFS file system, and only
  123. /// for files, not directories.
  124. /// </summary>
  125. /// <remarks>Minimum supported client: Windows XP [desktop apps only].</remarks>
  126. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only].</remarks>
  127. /// <returns>
  128. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error
  129. /// information, call GetLastError.
  130. /// </returns>
  131. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  132. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateHardLinkW"), SuppressUnmanagedCodeSecurity]
  133. [return: MarshalAs(UnmanagedType.Bool)]
  134. internal static extern bool CreateHardLink([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, IntPtr lpSecurityAttributes);
  135. /// <summary>
  136. /// Establishes a hard link between an existing file and a new file as a transacted operation. This function is only supported on the
  137. /// NTFS file system, and only for files, not directories.
  138. /// </summary>
  139. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  140. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  141. /// <returns>
  142. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error
  143. /// information, call GetLastError.
  144. /// </returns>
  145. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  146. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateHardLinkTransactedW"), SuppressUnmanagedCodeSecurity]
  147. [return: MarshalAs(UnmanagedType.Bool)]
  148. internal static extern bool CreateHardLinkTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, IntPtr lpSecurityAttributes, SafeHandle hTransaction);
  149. /// <summary>Creates a symbolic link.</summary>
  150. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  151. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  152. /// <remarks>
  153. /// The unmanaged prototype contains a return directive because the CreateSymbolicLink API function returns BOOLEAN, a one-byte data type.
  154. /// The default marshaling for bool is four bytes (to allow seamless integration with BOOL return values).
  155. /// If you were to use the default marshaling for BOOLEAN values, it's likely that you will get erroneous results.
  156. /// The return directive forces PInvoke to marshal just one byte of the return value.
  157. /// Source: http://www.informit.com/guides/content.aspx?g=dotnet&amp;seqNum=762&amp;ns=16196
  158. /// </remarks>
  159. /// <returns>
  160. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error
  161. /// information, call GetLastError.
  162. /// </returns>
  163. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  164. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateSymbolicLinkW"), SuppressUnmanagedCodeSecurity]
  165. [return: MarshalAs(UnmanagedType.I1)]
  166. internal static extern bool CreateSymbolicLink([MarshalAs(UnmanagedType.LPWStr)] string lpSymlinkFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpTargetFileName, [MarshalAs(UnmanagedType.U4)] SymbolicLinkTarget dwFlags);
  167. /// <summary>Creates a symbolic link as a transacted operation.</summary>
  168. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  169. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  170. /// <remarks>
  171. /// The unmanaged prototype contains a return directive because the CreateSymbolicLink API function returns BOOLEAN, a one-byte data type.
  172. /// The default marshaling for bool is four bytes (to allow seamless integration with BOOL return values).
  173. /// If you were to use the default marshaling for BOOLEAN values, it's likely that you will get erroneous results.
  174. /// The return directive forces PInvoke to marshal just one byte of the return value.
  175. /// Source: http://www.informit.com/guides/content.aspx?g=dotnet&amp;seqNum=762&amp;ns=16196
  176. /// </remarks>
  177. /// <returns>
  178. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error
  179. /// information, call GetLastError.
  180. /// </returns>
  181. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  182. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateSymbolicLinkTransactedW"), SuppressUnmanagedCodeSecurity]
  183. [return: MarshalAs(UnmanagedType.I1)]
  184. internal static extern bool CreateSymbolicLinkTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpSymlinkFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpTargetFileName, [MarshalAs(UnmanagedType.U4)] SymbolicLinkTarget dwFlags, SafeHandle hTransaction);
  185. /// <summary>Decrypts an encrypted file or directory.</summary>
  186. /// <remarks>
  187. /// The DecryptFile function requires exclusive access to the file being decrypted, and will fail if another process is using the file.
  188. /// If the file is not encrypted, DecryptFile simply returns a nonzero value, which indicates success. If lpFileName specifies a read-
  189. /// only file, the function fails and GetLastError returns ERROR_FILE_READ_ONLY. If lpFileName specifies a directory that contains a
  190. /// read-only file, the functions succeeds but the directory is not decrypted.
  191. /// </remarks>
  192. /// <remarks>Minimum supported client: Windows XP.</remarks>
  193. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  194. /// <returns>
  195. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  196. /// information, call GetLastError.
  197. /// </returns>
  198. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  199. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DecryptFileW"), SuppressUnmanagedCodeSecurity]
  200. [return: MarshalAs(UnmanagedType.Bool)]
  201. internal static extern bool DecryptFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] uint dwReserved);
  202. /// <summary>Deletes an existing file.</summary>
  203. /// <remarks>
  204. /// If an application attempts to delete a file that does not exist, the DeleteFile function fails with ERROR_FILE_NOT_FOUND.
  205. /// </remarks>
  206. /// <remarks>If the file is a read-only file, the function fails with ERROR_ACCESS_DENIED.</remarks>
  207. /// <remarks>
  208. /// If the path points to a symbolic link, the symbolic link is deleted, not the target. To delete a target, you must call CreateFile
  209. /// and specify FILE_FLAG_DELETE_ON_CLOSE.
  210. /// </remarks>
  211. /// <remarks>Minimum supported client: Windows XP [desktop apps | Windows Store apps].</remarks>
  212. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps].</remarks>
  213. /// <returns>
  214. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error
  215. /// information, call GetLastError.
  216. /// </returns>
  217. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  218. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DeleteFileW"), SuppressUnmanagedCodeSecurity]
  219. [return: MarshalAs(UnmanagedType.Bool)]
  220. internal static extern bool DeleteFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
  221. /// <summary>Deletes an existing file as a transacted operation.</summary>
  222. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  223. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  224. /// <returns>
  225. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error
  226. /// information, call GetLastError.
  227. /// </returns>
  228. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  229. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DeleteFileTransactedW"), SuppressUnmanagedCodeSecurity]
  230. [return: MarshalAs(UnmanagedType.Bool)]
  231. internal static extern bool DeleteFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, SafeHandle hTransaction);
  232. /// <summary>
  233. /// Encrypts a file or directory. All data streams in a file are encrypted. All new files created in an encrypted directory are
  234. /// encrypted.
  235. /// </summary>
  236. /// <remarks>
  237. /// The EncryptFile function requires exclusive access to the file being encrypted, and will fail if another process is using the file.
  238. /// If the file is already encrypted, EncryptFile simply returns a nonzero value, which indicates success. If the file is compressed,
  239. /// EncryptFile will decompress the file before encrypting it. If lpFileName specifies a read-only file, the function fails and
  240. /// GetLastError returns ERROR_FILE_READ_ONLY. If lpFileName specifies a directory that contains a read-only file, the functions
  241. /// succeeds but the directory is not encrypted.
  242. /// </remarks>
  243. /// <remarks>Minimum supported client: Windows XP.</remarks>
  244. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  245. /// <returns>
  246. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  247. /// information, call GetLastError.
  248. /// </returns>
  249. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  250. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EncryptFileW"), SuppressUnmanagedCodeSecurity]
  251. [return: MarshalAs(UnmanagedType.Bool)]
  252. internal static extern bool EncryptFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
  253. /// <summary>
  254. /// Disables or enables encryption of the specified directory and the files in it. It does not affect encryption of subdirectories
  255. /// below the indicated directory.
  256. /// </summary>
  257. /// <remarks>
  258. /// EncryptionDisable() disables encryption of directories and files. It does not affect the visibility of files with the
  259. /// FILE_ATTRIBUTE_SYSTEM attribute set. This method will create/change the file "Desktop.ini" and wil set Encryption value:
  260. /// "Disable=0|1".
  261. /// </remarks>
  262. /// <remarks>Minimum supported client: Windows XP Professional [desktop apps only].</remarks>
  263. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only].</remarks>
  264. /// <returns>
  265. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  266. /// information, call GetLastError.
  267. /// </returns>
  268. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule"), SuppressUnmanagedCodeSecurity]
  269. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  270. [return: MarshalAs(UnmanagedType.Bool)]
  271. internal static extern bool EncryptionDisable([MarshalAs(UnmanagedType.LPWStr)] string dirPath, [MarshalAs(UnmanagedType.Bool)] bool disable);
  272. /// <summary>Retrieves the encryption status of the specified file.</summary>
  273. /// <remarks>Minimum supported client: Windows XP Professional [desktop apps only].</remarks>
  274. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only].</remarks>
  275. /// <returns>
  276. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  277. /// information, call GetLastError.
  278. /// </returns>
  279. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  280. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FileEncryptionStatusW"), SuppressUnmanagedCodeSecurity]
  281. [return: MarshalAs(UnmanagedType.Bool)]
  282. internal static extern bool FileEncryptionStatus([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, out FileEncryptionStatus lpStatus);
  283. /// <summary>
  284. /// Closes a file search handle opened by the FindFirstFile, FindFirstFileEx, FindFirstFileNameW, FindFirstFileNameTransactedW,
  285. /// FindFirstFileTransacted, FindFirstStreamTransactedW, or FindFirstStreamW functions.
  286. /// </summary>
  287. /// <remarks>Minimum supported client: Windows XP [desktop apps | Windows Store apps].</remarks>
  288. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps].</remarks>
  289. /// <returns>
  290. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  291. /// information, call GetLastError.
  292. /// </returns>
  293. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule"), SuppressUnmanagedCodeSecurity]
  294. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  295. [return: MarshalAs(UnmanagedType.Bool)]
  296. internal static extern bool FindClose(IntPtr hFindFile);
  297. /// <summary>Searches a directory for a file or subdirectory with a name and attributes that match those specified.</summary>
  298. /// <remarks>A trailing backslash is not allowed and will be removed.</remarks>
  299. /// <remarks>Minimum supported client: Windows XP [desktop apps | Windows Store apps].</remarks>
  300. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps].</remarks>
  301. /// <returns>
  302. /// If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose, and the
  303. /// lpFindFileData parameter contains information about the first file or directory found. If the function fails or fails to locate
  304. /// files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of
  305. /// lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
  306. /// </returns>
  307. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  308. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileExW"), SuppressUnmanagedCodeSecurity]
  309. internal static extern SafeFindFileHandle FindFirstFileEx([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, out WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, FindExAdditionalFlags dwAdditionalFlags);
  310. /// <summary>
  311. /// Searches a directory for a file or subdirectory with a name that matches a specific name as a transacted operation.
  312. /// </summary>
  313. /// <remarks>A trailing backslash is not allowed and will be removed.</remarks>
  314. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  315. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  316. /// <returns>
  317. /// If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose, and the
  318. /// lpFindFileData parameter contains information about the first file or directory found. If the function fails or fails to locate
  319. /// files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of
  320. /// lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
  321. /// </returns>
  322. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  323. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileTransactedW"), SuppressUnmanagedCodeSecurity]
  324. internal static extern SafeFindFileHandle FindFirstFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, out WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, FindExAdditionalFlags dwAdditionalFlags, SafeHandle hTransaction);
  325. /// <summary>
  326. /// Creates an enumeration of all the hard links to the specified file. The FindFirstFileNameW function returns a handle to the
  327. /// enumeration that can be used on subsequent calls to the FindNextFileNameW function.
  328. /// </summary>
  329. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  330. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  331. /// <returns>
  332. /// If the function succeeds, the return value is a search handle that can be used with the FindNextFileNameW function or closed with
  333. /// the FindClose function. If the function fails, the return value is INVALID_HANDLE_VALUE (0xffffffff). To get extended error
  334. /// information, call the GetLastError function.
  335. /// </returns>
  336. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  337. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileNameW"), SuppressUnmanagedCodeSecurity]
  338. internal static extern SafeFindFileHandle FindFirstFileName([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] uint dwFlags, [MarshalAs(UnmanagedType.U4)] out uint stringLength, StringBuilder linkName);
  339. /// <summary>
  340. /// Creates an enumeration of all the hard links to the specified file as a transacted operation. The function returns a handle to the
  341. /// enumeration that can be used on subsequent calls to the FindNextFileNameW function.
  342. /// </summary>
  343. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  344. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  345. /// <returns>
  346. /// If the function succeeds, the return value is a search handle that can be used with the FindNextFileNameW function or closed with
  347. /// the FindClose function. If the function fails, the return value is INVALID_HANDLE_VALUE (0xffffffff). To get extended error
  348. /// information, call the GetLastError function.
  349. /// </returns>
  350. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  351. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileNameTransactedW"), SuppressUnmanagedCodeSecurity]
  352. internal static extern SafeFindFileHandle FindFirstFileNameTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] uint dwFlags, [MarshalAs(UnmanagedType.U4)] out uint stringLength, StringBuilder linkName, SafeHandle hTransaction);
  353. /// <summary>
  354. /// Continues a file search from a previous call to the FindFirstFile, FindFirstFileEx, or FindFirstFileTransacted functions.
  355. /// </summary>
  356. /// <remarks>Minimum supported client: Windows XP [desktop apps | Windows Store apps].</remarks>
  357. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps].</remarks>
  358. /// <returns>
  359. /// If the function succeeds, the return value is nonzero and the lpFindFileData parameter contains information about the next file or
  360. /// directory found. If the function fails, the return value is zero and the contents of lpFindFileData are indeterminate. To get
  361. /// extended error information, call the GetLastError function. If the function fails because no more matching files can be found, the
  362. /// GetLastError function returns ERROR_NO_MORE_FILES.
  363. /// </returns>
  364. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  365. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextFileW"), SuppressUnmanagedCodeSecurity]
  366. [return: MarshalAs(UnmanagedType.Bool)]
  367. internal static extern bool FindNextFile(SafeFindFileHandle hFindFile, out WIN32_FIND_DATA lpFindFileData);
  368. /// <summary>
  369. /// Continues enumerating the hard links to a file using the handle returned by a successful call to the FindFirstFileName function.
  370. /// </summary>
  371. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  372. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  373. /// <returns>
  374. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero (0). To get extended error
  375. /// information, call GetLastError. If no matching files can be found, the GetLastError function returns ERROR_HANDLE_EOF.
  376. /// </returns>
  377. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  378. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextFileNameW"), SuppressUnmanagedCodeSecurity]
  379. [return: MarshalAs(UnmanagedType.Bool)]
  380. internal static extern bool FindNextFileName(SafeFindFileHandle hFindStream, [MarshalAs(UnmanagedType.U4)] out uint stringLength, StringBuilder linkName);
  381. /// <summary>Flushes the buffers of a specified file and causes all buffered data to be written to a file.</summary>
  382. /// <remarks>Minimum supported client: Windows XP [desktop apps | Windows Store apps].</remarks>
  383. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps].</remarks>
  384. /// <returns>
  385. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  386. /// information, call GetLastError.
  387. /// </returns>
  388. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  389. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  390. [return: MarshalAs(UnmanagedType.Bool)]
  391. internal static extern bool FlushFileBuffers(SafeFileHandle hFile);
  392. /// <summary>Retrieves the actual number of bytes of disk storage used to store a specified file.</summary>
  393. /// <remarks>Minimum supported client: Windows XP [desktop apps only].</remarks>
  394. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only].</remarks>
  395. /// <returns>
  396. /// If the function succeeds, the return value is the low-order DWORD of the actual number of bytes of disk storage used to store the
  397. /// specified file, and if lpFileSizeHigh is non-NULL, the function puts the high-order DWORD of that actual value into the DWORD
  398. /// pointed to by that parameter. This is the compressed file size for compressed files, the actual file size for noncompressed files.
  399. /// If the function fails, and lpFileSizeHigh is NULL, the return value is INVALID_FILE_SIZE. To get extended error information, call
  400. /// GetLastError. If the return value is INVALID_FILE_SIZE and lpFileSizeHigh is non-NULL, an application must call GetLastError to
  401. /// determine whether the function has succeeded (value is NO_ERROR) or failed (value is other than NO_ERROR).
  402. /// </returns>
  403. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  404. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetCompressedFileSizeW"), SuppressUnmanagedCodeSecurity]
  405. [return: MarshalAs(UnmanagedType.U4)]
  406. internal static extern uint GetCompressedFileSize([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);
  407. /// <summary>Retrieves the actual number of bytes of disk storage used to store a specified file as a transacted operation.</summary>
  408. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  409. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  410. /// <returns>
  411. /// If the function succeeds, the return value is the low-order DWORD of the actual number of bytes of disk storage used to store the
  412. /// specified file, and if lpFileSizeHigh is non-NULL, the function puts the high-order DWORD of that actual value into the DWORD
  413. /// pointed to by that parameter. This is the compressed file size for compressed files, the actual file size for noncompressed files.
  414. /// If the function fails, and lpFileSizeHigh is NULL, the return value is INVALID_FILE_SIZE. To get extended error information, call
  415. /// GetLastError. If the return value is INVALID_FILE_SIZE and lpFileSizeHigh is non-NULL, an application must call GetLastError to
  416. /// determine whether the function has succeeded (value is NO_ERROR) or failed (value is other than NO_ERROR).
  417. /// </returns>
  418. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  419. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetCompressedFileSizeTransactedW"), SuppressUnmanagedCodeSecurity]
  420. [return: MarshalAs(UnmanagedType.U4)]
  421. internal static extern uint GetCompressedFileSizeTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh, SafeHandle hTransaction);
  422. /// <summary>
  423. /// Retrieves attributes for a specified file or directory.
  424. /// </summary>
  425. /// <remarks>
  426. /// <para>The GetFileAttributes function retrieves file system attribute information.</para>
  427. /// <para>GetFileAttributesEx can obtain other sets of file or directory attribute information.</para>
  428. /// <para>Currently, GetFileAttributesEx retrieves a set of standard attributes that is a superset of the file system attribute
  429. /// information.
  430. /// When the GetFileAttributesEx function is called on a directory that is a mounted folder, it returns the attributes of the directory,
  431. /// not those of the root directory in the volume that the mounted folder associates with the directory. To obtain the attributes of
  432. /// the associated volume, call GetVolumeNameForVolumeMountPoint to obtain the name of the associated volume. Then use the resulting
  433. /// name in a call to GetFileAttributesEx. The results are the attributes of the root directory on the associated volume.</para>
  434. /// <para>Symbolic link behavior: If the path points to a symbolic link, the function returns attributes for the symbolic link.</para>
  435. /// <para>Minimum supported client: Windows XP [desktop apps only]</para>
  436. /// <para>Minimum supported server: Windows Server 2003 [desktop apps only]</para>
  437. /// </remarks>
  438. /// <returns>
  439. /// <para>If the function succeeds, the return value is nonzero.</para>
  440. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  441. /// </returns>
  442. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  443. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFileAttributesExW"), SuppressUnmanagedCodeSecurity]
  444. [return: MarshalAs(UnmanagedType.Bool)]
  445. internal static extern bool GetFileAttributesEx([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] GetFileExInfoLevels fInfoLevelId, out WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);
  446. /// <summary>Retrieves attributes for a specified file or directory.</summary>
  447. /// <remarks>
  448. /// <para>The GetFileAttributes function retrieves file system attribute information.</para>
  449. /// <para>GetFileAttributesEx can obtain other sets of file or directory attribute information.</para>
  450. /// <para>
  451. /// Currently, GetFileAttributesEx retrieves a set of standard attributes that is a superset of the file system attribute information.
  452. /// When the GetFileAttributesEx function is called on a directory that is a mounted folder, it returns the attributes of the directory,
  453. /// not those of the root directory in the volume that the mounted folder associates with the directory. To obtain the attributes of
  454. /// the associated volume, call GetVolumeNameForVolumeMountPoint to obtain the name of the associated volume. Then use the resulting
  455. /// name in a call to GetFileAttributesEx. The results are the attributes of the root directory on the associated volume.</para>
  456. /// <para>Symbolic link behavior: If the path points to a symbolic link, the function returns attributes for the symbolic link.</para>
  457. /// <para>Transacted Operations</para>
  458. /// <para>If a file is open for modification in a transaction, no other thread can open the file for modification until the transaction
  459. /// is committed. Conversely, if a file is open for modification outside of a transaction, no transacted thread can open the file for
  460. /// modification until the non-transacted handle is closed. If a non-transacted thread has a handle opened to modify a file, a call to
  461. /// GetFileAttributesTransacted for that file will fail with an ERROR_TRANSACTIONAL_CONFLICT error.</para>
  462. /// <para>Minimum supported client: Windows Vista [desktop apps only]</para>
  463. /// <para>Minimum supported server: Windows Server 2008 [desktop apps only]</para>
  464. /// </remarks>
  465. /// <returns>
  466. /// <para>If the function succeeds, the return value is nonzero.</para>
  467. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  468. /// </returns>
  469. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  470. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFileAttributesTransactedW"), SuppressUnmanagedCodeSecurity]
  471. [return: MarshalAs(UnmanagedType.Bool)]
  472. internal static extern bool GetFileAttributesTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] GetFileExInfoLevels fInfoLevelId, out WIN32_FILE_ATTRIBUTE_DATA lpFileInformation, SafeHandle hTransaction);
  473. /// <summary>Retrieves file information for the specified file.</summary>
  474. /// <returns>
  475. /// If the function succeeds, the return value is nonzero and file information data is contained in the buffer pointed to by the lpByHandleFileInformation parameter.
  476. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  477. /// </returns>
  478. /// <remarks>
  479. /// Depending on the underlying network features of the operating system and the type of server connected to,
  480. /// the GetFileInformationByHandle function may fail, return partial information, or full information for the given file.
  481. /// </remarks>
  482. /// <remarks>Minimum supported client: Windows XP [desktop apps only]</remarks>
  483. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only]</remarks>
  484. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  485. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  486. [return: MarshalAs(UnmanagedType.Bool)]
  487. internal static extern bool GetFileInformationByHandle(SafeFileHandle hFile, [MarshalAs(UnmanagedType.Struct)] out BY_HANDLE_FILE_INFORMATION lpByHandleFileInformation);
  488. /// <summary>
  489. /// Retrieves file information for the specified file.
  490. /// </summary>
  491. /// <remarks>
  492. /// <para>Minimum supported client: Windows Vista [desktop apps | Windows Store apps]</para>
  493. /// <para>Minimum supported server: Windows Server 2008 [desktop apps | Windows Store apps]</para>
  494. /// <para>Redistributable: Windows SDK on Windows Server 2003 and Windows XP.</para>
  495. /// </remarks>
  496. /// <param name="hFile">The file.</param>
  497. /// <param name="fileInfoByHandleClass">The file information by handle class.</param>
  498. /// <param name="lpFileInformation">Information describing the file.</param>
  499. /// <param name="dwBufferSize">Size of the buffer.</param>
  500. /// <returns>
  501. /// <para>If the function succeeds, the return value is nonzero and file information data is contained in the buffer pointed to by the
  502. /// lpByHandleFileInformation parameter.</para>
  503. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  504. /// </returns>
  505. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  506. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  507. [return: MarshalAs(UnmanagedType.Bool)]
  508. internal static extern bool GetFileInformationByHandleEx(SafeFileHandle hFile, [MarshalAs(UnmanagedType.I4)] FileInfoByHandleClass fileInfoByHandleClass, SafeGlobalMemoryBufferHandle lpFileInformation, [MarshalAs(UnmanagedType.U4)] uint dwBufferSize);
  509. /// <summary>Retrieves file information for the specified file.</summary>
  510. /// <remarks>
  511. /// <para>Minimum supported client: Windows Vista [desktop apps | Windows Store apps]</para>
  512. /// <para>Minimum supported server: Windows Server 2008 [desktop apps | Windows Store apps]</para>
  513. /// <para>Redistributable: Windows SDK on Windows Server 2003 and Windows XP.</para>
  514. /// </remarks>
  515. /// <returns>
  516. /// <para>If the function succeeds, the return value is nonzero and file information data is contained in the buffer pointed to by the
  517. /// lpByHandleFileInformation parameter.</para>
  518. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  519. /// </returns>
  520. /// <param name="hFile">The file.</param>
  521. /// <param name="fileInfoByHandleClass">The file information by handle class.</param>
  522. /// <param name="lpFileInformation">Information describing the file.</param>
  523. /// <param name="dwBufferSize">Size of the buffer.</param>
  524. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  525. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFileInformationByHandleEx"), SuppressUnmanagedCodeSecurity]
  526. [return: MarshalAs(UnmanagedType.Bool)]
  527. internal static extern bool GetFileInformationByHandleEx_FileBasicInfo(SafeFileHandle hFile, [MarshalAs(UnmanagedType.I4)] FileInfoByHandleClass fileInfoByHandleClass, [MarshalAs(UnmanagedType.Struct)] out FILE_BASIC_INFO lpFileInformation, [MarshalAs(UnmanagedType.U4)] uint dwBufferSize);
  528. /// <summary>
  529. /// Retrieves the size of the specified file.
  530. /// </summary>
  531. /// <remarks>
  532. /// <para>Minimum supported client: Windows XP [desktop apps only]</para>
  533. /// <para>Minimum supported server: Windows Server 2003 [desktop apps only]</para>
  534. /// </remarks>
  535. /// <returns>
  536. /// <para>If the function succeeds, the return value is nonzero.</para>
  537. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  538. /// </returns>
  539. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  540. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  541. [return: MarshalAs(UnmanagedType.Bool)]
  542. internal static extern bool GetFileSizeEx(SafeFileHandle hFile, out long lpFileSize);
  543. /// <summary>Retrieves the final path for the specified file.</summary>
  544. /// <remarks>Minimum supported client: Windows Vista [desktop apps only].</remarks>
  545. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only].</remarks>
  546. /// <returns>
  547. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  548. /// information, call GetLastError.
  549. /// </returns>
  550. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  551. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFinalPathNameByHandleW"), SuppressUnmanagedCodeSecurity]
  552. [return: MarshalAs(UnmanagedType.U4)]
  553. internal static extern uint GetFinalPathNameByHandle(SafeFileHandle hFile, StringBuilder lpszFilePath, [MarshalAs(UnmanagedType.U4)] uint cchFilePath, FinalPathFormats dwFlags);
  554. /// <summary>
  555. /// Checks whether the specified address is within a memory-mapped file in the address space of the specified process. If so, the
  556. /// function returns the name of the memory-mapped file.
  557. /// </summary>
  558. /// <remarks>Minimum supported client: Windows XP.</remarks>
  559. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  560. /// <returns>
  561. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  562. /// information, call GetLastError.
  563. /// </returns>
  564. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  565. [DllImport("psapi.dll", SetLastError = false, CharSet = CharSet.Unicode, EntryPoint = "GetMappedFileNameW"), SuppressUnmanagedCodeSecurity]
  566. [return: MarshalAs(UnmanagedType.Bool)]
  567. internal static extern bool GetMappedFileName(IntPtr hProcess, SafeLocalMemoryBufferHandle lpv, StringBuilder lpFilename, [MarshalAs(UnmanagedType.U4)] uint nSize);
  568. /// <summary>Locks the specified file for exclusive access by the calling process.</summary>
  569. /// <remarks>Minimum supported client: Windows XP.</remarks>
  570. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  571. /// <returns>
  572. /// If the function succeeds, the return value is nonzero (TRUE). If the function fails, the return value is zero (FALSE). To get
  573. /// extended error information, call GetLastError.
  574. /// </returns>
  575. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  576. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  577. [return: MarshalAs(UnmanagedType.Bool)]
  578. internal static extern bool LockFile(SafeFileHandle hFile, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetLow, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetHigh, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToLockLow, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToLockHigh);
  579. /// <summary>Maps a view of a file mapping into the address space of a calling process.</summary>
  580. /// <remarks>Minimum supported client: Windows XP.</remarks>
  581. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  582. /// <returns>
  583. /// If the function succeeds, the return value is the starting address of the mapped view. If the function fails, the return value is
  584. /// <see langword="null"/>.
  585. /// </returns>
  586. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  587. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  588. internal static extern SafeLocalMemoryBufferHandle MapViewOfFile(SafeFileHandle hFileMappingObject, [MarshalAs(UnmanagedType.U4)] uint dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetHigh, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);
  589. /// <summary>
  590. /// Moves a file or directory, including its children.
  591. /// <para>You can provide a callback function that receives progress notifications.</para>
  592. /// </summary>
  593. /// <remarks>
  594. /// <para>The MoveFileWithProgress function coordinates its operation with the link tracking service, so link sources can be tracked as they are moved.</para>
  595. /// <para>Minimum supported client: Windows XP [desktop apps only]</para>
  596. /// <para>Minimum supported server: Windows Server 2003 [desktop apps only]</para>
  597. /// </remarks>
  598. /// <param name="lpExistingFileName">Filename of the existing file.</param>
  599. /// <param name="lpNewFileName">Filename of the new file.</param>
  600. /// <param name="lpProgressRoutine">The progress routine.</param>
  601. /// <param name="lpData">The data.</param>
  602. /// <param name="dwFlags">The flags.</param>
  603. /// <returns>
  604. /// <para>If the function succeeds, the return value is nonzero.</para>
  605. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  606. /// </returns>
  607. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  608. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "MoveFileWithProgressW"), SuppressUnmanagedCodeSecurity]
  609. [return: MarshalAs(UnmanagedType.Bool)]
  610. internal static extern bool MoveFileWithProgress([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyMoveProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.U4)] MoveOptions dwFlags);
  611. /// <summary>
  612. /// Moves an existing file or a directory, including its children, as a transacted operation.
  613. /// <para>You can provide a callback function that receives progress notifications.</para>
  614. /// </summary>
  615. /// <remarks>
  616. /// <para>Minimum supported client: Windows Vista [desktop apps only]</para>
  617. /// <para>Minimum supported server: Windows Server 2008 [desktop apps only]</para>
  618. /// </remarks>
  619. /// <returns>
  620. /// <para>If the function succeeds, the return value is nonzero.</para>
  621. /// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para>
  622. /// </returns>
  623. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  624. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "MoveFileTransactedW"), SuppressUnmanagedCodeSecurity]
  625. [return: MarshalAs(UnmanagedType.Bool)]
  626. internal static extern bool MoveFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyMoveProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.U4)] MoveOptions dwCopyFlags, SafeHandle hTransaction);
  627. /// <summary>An application-defined callback function used with the CopyFileEx, MoveFileTransacted, and MoveFileWithProgress functions.
  628. /// <para>It is called when a portion of a copy or move operation is completed.</para>
  629. /// <para>The LPPROGRESS_ROUTINE type defines a pointer to this callback function.</para>
  630. /// <para>NativeCopyMoveProgressRoutine (NativeCopyMoveProgressRoutine) is a placeholder for the application-defined function name.</para>
  631. /// </summary>
  632. [SuppressUnmanagedCodeSecurity]
  633. internal delegate CopyMoveProgressResult NativeCopyMoveProgressRoutine(long totalFileSize, long totalBytesTransferred, long streamSize, long streamBytesTransferred, [MarshalAs(UnmanagedType.U4)] int dwStreamNumber, CopyMoveProgressCallbackReason dwCallbackReason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData);
  634. /// <summary>Replaces one file with another file, with the option of creating a backup copy of the original file. The replacement file assumes the name of the replaced file and its identity.</summary>
  635. /// <returns>
  636. /// If the function succeeds, the return value is nonzero.
  637. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  638. /// </returns>
  639. /// <remarks>Minimum supported client: Windows XP [desktop apps only]</remarks>
  640. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only]</remarks>
  641. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  642. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "ReplaceFileW"), SuppressUnmanagedCodeSecurity]
  643. [return: MarshalAs(UnmanagedType.Bool)]
  644. internal static extern bool ReplaceFile([MarshalAs(UnmanagedType.LPWStr)] string lpReplacedFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpReplacementFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpBackupFileName, FileSystemRights dwReplaceFlags, IntPtr lpExclude, IntPtr lpReserved);
  645. /// <summary>Sets the attributes for a file or directory.</summary>
  646. /// <returns>
  647. /// If the function succeeds, the return value is nonzero.
  648. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  649. /// </returns>
  650. /// <remarks>Minimum supported client: Windows XP</remarks>
  651. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  652. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  653. [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api")]
  654. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetFileAttributesW"), SuppressUnmanagedCodeSecurity]
  655. [return: MarshalAs(UnmanagedType.Bool)]
  656. internal static extern bool SetFileAttributes([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileAttributes dwFileAttributes);
  657. /// <summary>Sets the attributes for a file or directory as a transacted operation.</summary>
  658. /// <returns>
  659. /// If the function succeeds, the return value is nonzero.
  660. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  661. /// </returns>
  662. /// <remarks>Minimum supported client: Windows Vista [desktop apps only]</remarks>
  663. /// <remarks>Minimum supported server: Windows Server 2008 [desktop apps only]</remarks>
  664. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  665. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetFileAttributesTransactedW"), SuppressUnmanagedCodeSecurity]
  666. [return: MarshalAs(UnmanagedType.Bool)]
  667. internal static extern bool SetFileAttributesTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileAttributes dwFileAttributes, SafeHandle hTransaction);
  668. /// <summary>Sets the date and time that the specified file or directory was created, last accessed, or last modified.</summary>
  669. /// <returns>
  670. /// If the function succeeds, the return value is nonzero.
  671. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  672. /// </returns>
  673. /// <remarks>Minimum supported client: Windows XP [desktop apps only]</remarks>
  674. /// <remarks>Minimum supported server: Windows Server 2003 [desktop apps only]</remarks>
  675. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  676. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  677. [return: MarshalAs(UnmanagedType.Bool)]
  678. internal static extern bool SetFileTime(SafeFileHandle hFile, SafeGlobalMemoryBufferHandle lpCreationTime, SafeGlobalMemoryBufferHandle lpLastAccessTime, SafeGlobalMemoryBufferHandle lpLastWriteTime);
  679. /// <summary>Unlocks a region in an open file. Unlocking a region enables other processes to access the region.</summary>
  680. /// <returns>
  681. /// If the function succeeds, the return value is nonzero.
  682. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  683. /// </returns>
  684. /// <remarks>Minimum supported client: Windows XP</remarks>
  685. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  686. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  687. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  688. [return: MarshalAs(UnmanagedType.Bool)]
  689. internal static extern bool UnlockFile(SafeFileHandle hFile, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetLow, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetHigh, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToUnlockLow, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToUnlockHigh);
  690. /// <summary>Unmaps a mapped view of a file from the calling process's address space.</summary>
  691. /// <remarks>Minimum supported client: Windows XP.</remarks>
  692. /// <remarks>Minimum supported server: Windows Server 2003.</remarks>
  693. /// <param name="lpBaseAddress">The base address.</param>
  694. /// <returns>
  695. /// If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error
  696. /// information, call GetLastError.
  697. /// </returns>
  698. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  699. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  700. [return: MarshalAs(UnmanagedType.Bool)]
  701. internal static extern bool UnmapViewOfFile(SafeLocalMemoryBufferHandle lpBaseAddress);
  702. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  703. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  704. internal static extern SafeFindFileHandle FindFirstStreamTransactedW(string fileName, STREAM_INFO_LEVELS infoLevel, SafeGlobalMemoryBufferHandle lpFindStreamData, int flags, SafeHandle hTransaction);
  705. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  706. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  707. internal static extern SafeFindFileHandle FindFirstStreamW(string fileName, STREAM_INFO_LEVELS infoLevel, SafeGlobalMemoryBufferHandle lpFindStreamData, int flags);
  708. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  709. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
  710. [return: MarshalAs(UnmanagedType.Bool)]
  711. internal static extern bool FindNextStreamW(SafeFindFileHandle handle, SafeGlobalMemoryBufferHandle lpFindStreamData);
  712. }
  713. }