/Development/AlphaFS/Filesystem/NativeMethods.cs

http://alphafs.codeplex.com · C# · 2051 lines · 639 code · 276 blank · 1136 comment · 40 complexity · f0bc28e275fbb7a20f6c1c35e70d8c88 MD5 · raw file

  1. /* Copyright (c) 2008-2012 Peter Palotas, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.Globalization;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Runtime.InteropServices;
  27. using System.Security.AccessControl;
  28. using System.Text;
  29. using Alphaleonis.Win32.Security;
  30. using Microsoft.Win32.SafeHandles;
  31. using SecurityNativeMethods = Alphaleonis.Win32.Security.NativeMethods;
  32. namespace Alphaleonis.Win32.Filesystem
  33. {
  34. internal static class NativeMethods
  35. {
  36. #region Internal Utility
  37. internal static uint GetLowOrderDword(long lowPart)
  38. {
  39. return (uint)(lowPart & 0xFFFFFFFF);
  40. }
  41. internal static uint GetHighOrderDword(long highPart)
  42. {
  43. return (uint)((highPart >> 32) & 0xFFFFFFFF);
  44. }
  45. internal static long ToLong(uint highPart, uint lowPart)
  46. {
  47. return (((long)highPart) << 32) | (((long)lowPart) & 0xFFFFFFFF);
  48. }
  49. internal static ulong LuidToLong(SecurityNativeMethods.Luid luid)
  50. {
  51. ulong high = (((ulong)luid.HighPart) << 32);
  52. ulong low = (((ulong)luid.LowPart) & 0x00000000FFFFFFFF);
  53. return high | low;
  54. }
  55. internal static SecurityNativeMethods.Luid LongToLuid(ulong lluid)
  56. {
  57. return new SecurityNativeMethods.Luid { HighPart = (uint)(lluid >> 32), LowPart = (uint)(lluid & 0xFFFFFFFF) };
  58. }
  59. internal static bool ContainsFileAttributesFlag(FileAttributes attributes, FileAttributes hasAttribute)
  60. {
  61. return (attributes & hasAttribute) == hasAttribute;
  62. }
  63. internal static bool ContainsFileSystemAttributesFlag(VolumeInfoAttributes attributes, VolumeInfoAttributes hasAttribute)
  64. {
  65. return (attributes & hasAttribute) == hasAttribute;
  66. }
  67. /// <summary>Check is the current handle is not null, not closed and not invalid.</summary>
  68. /// <param name="handle">The current handle to check.</param>
  69. /// <param name="throwException"><c>true</c> will throw an <exception cref="Resources.HandleInvalid"/>, <c>false</c> will not raise this exception..</param>
  70. /// <returns><c>true</c> on success, <c>false</c> otherwise.</returns>
  71. internal static bool IsValidHandle(SafeHandle handle, bool throwException = true)
  72. {
  73. if (handle == null || handle.IsClosed || handle.IsInvalid)
  74. {
  75. if (throwException)
  76. throw new ArgumentException(Resources.HandleInvalid);
  77. return false;
  78. }
  79. return true;
  80. }
  81. /// <summary>Check is the current stream is not null, not closed and not invalid.</summary>
  82. /// <param name="stream">The current stream to check.</param>
  83. /// <param name="throwException"><c>true</c> will throw an <exception cref="Resources.HandleInvalid"/>, <c>false</c> will not raise this exception.</param>
  84. /// <returns><c>true</c> on success, <c>false</c> otherwise.</returns>
  85. internal static bool IsValidStream(FileStream stream, bool throwException = true)
  86. {
  87. if (stream == null || stream.SafeFileHandle == null || stream.SafeFileHandle.IsClosed || stream.SafeFileHandle.IsInvalid)
  88. {
  89. if (throwException)
  90. throw new ArgumentException(Resources.StreamInvalid);
  91. return false;
  92. }
  93. return true;
  94. }
  95. #region UnitSizeToText
  96. /// <summary>Convert a number of type T to string with UnitSize or Percentage suffixed.</summary>
  97. internal static string UnitSizeToText<T>(T numberOfBytes, params bool[] options)
  98. {
  99. // Suffixes
  100. // bool[0] = false = "MB", True = "MiB"
  101. // bool[1] = true = %
  102. bool useMebi = options != null && options.Any() && options[0];
  103. bool usePercent = options != null && options.Count() == 2 && options[1];
  104. string template = "{0:0.00}{1}";
  105. string sfx = useMebi ? "Bi" : "bytes";
  106. double bytes = Convert.ToDouble(numberOfBytes, CultureInfo.InvariantCulture);
  107. if (bytes >= 1125899906842624) { sfx = useMebi ? "PiB" : "PB"; bytes /= 1125899906842624; }
  108. else if (bytes >= 1099511627776) { sfx = useMebi ? "TiB" : "TB"; bytes /= 1099511627776; }
  109. else if (bytes >= 1073741824) { sfx = useMebi ? "GiB" : "GB"; bytes /= 1073741824; }
  110. else if (bytes >= 1048576) { sfx = useMebi ? "MiB" : "MB"; bytes /= 1048576; }
  111. else if (bytes >= 1024) { sfx = useMebi ? "KiB" : "KB"; bytes /= 1024; }
  112. else if (!usePercent)
  113. // Will return "512 bytes" instead of "512,00 bytes".
  114. template = "{0:0}{1}";
  115. return string.Format(CultureInfo.CurrentCulture, template, bytes, usePercent ? "%" : " " + sfx);
  116. }
  117. /// <summary>Calculates a percentage value.</summary>
  118. /// <param name="currentValue"></param>
  119. /// <param name="minimumValue"></param>
  120. /// <param name="maximumValue"></param>
  121. internal static double PercentCalculate(double currentValue, double minimumValue, double maximumValue)
  122. {
  123. return (currentValue < 0 || maximumValue <= 0) ? 0 : currentValue * 100 / (maximumValue - minimumValue);
  124. }
  125. #endregion // UnitSizeToText
  126. #region SetErrorMode
  127. /// <summary>Controls whether the system will handle the specified types of serious errors or whether the process will handle them.</summary>
  128. /// <remarks>Not yet implemented: Windows 7, callers should favor SetThreadErrorMode over SetErrorMode since it is less disruptive to the normal behavior of the system.</remarks>
  129. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  130. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  131. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  132. internal struct ChangeErrorMode : IDisposable
  133. {
  134. private readonly NativeErrorMode _oldMode;
  135. internal ChangeErrorMode(NativeErrorMode mode)
  136. {
  137. _oldMode = SetErrorMode(mode);
  138. }
  139. void IDisposable.Dispose()
  140. {
  141. SetErrorMode(_oldMode);
  142. }
  143. }
  144. /// <summary>Controls whether the system will handle the specified types of serious errors or whether the process will handle them.</summary>
  145. /// <returns>The return value is the previous state of the error-mode bit attributes.</returns>
  146. /// <remarks>
  147. /// Because the error mode is set for the entire process, you must ensure that multi-threaded applications
  148. /// do not set different error-mode attributes. Doing so can lead to inconsistent error handling.
  149. /// </remarks>
  150. /// <remarks>SetLastError is set to false.</remarks>
  151. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  152. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
  153. [return: MarshalAs(UnmanagedType.U4)]
  154. private static extern NativeErrorMode SetErrorMode(NativeErrorMode nativeErrorMode);
  155. #endregion // SetErrorMode
  156. #region Platform
  157. #region HasFunction
  158. /// <summary>Functions to check if a specific Win32 API function is available to differentiate between "Windows XP / Windows 2003 Server" and "Windows Vista / Windows 2008 Server" platforms.</summary>
  159. /// <returns>true if the function exists; otherwise, false. To get extended error information, call GetLastError.</returns>
  160. internal static bool HasFunction(string dllName, string functionName)
  161. {
  162. IntPtr dll = LoadLibrary(dllName);
  163. if (dll == IntPtr.Zero)
  164. return false;
  165. IntPtr result = GetProcAddress(dll, functionName);
  166. return result != IntPtr.Zero && Marshal.GetLastWin32Error() != Win32Errors.ERROR_PROC_NOT_FOUND;
  167. }
  168. #endregion // HasFunction
  169. #region LoadLibrary
  170. /// <summary>Loads the specified module into the address space of the calling process. The specified module may cause other modules to be loaded.</summary>
  171. /// <returns>
  172. /// If the function succeeds, the return value is a handle to the module.
  173. /// If the function fails, the return value is <see langword="null"/>.
  174. /// </returns>
  175. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  176. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  177. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  178. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "LoadLibraryW")]
  179. private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
  180. #endregion // LoadLibrary
  181. #region GetProcAddress
  182. /// <summary>Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).</summary>
  183. /// <returns>
  184. /// If the function succeeds, the return value is the address of the exported function or variable.
  185. /// If the function fails, the return value is <see langword="null"/>.
  186. /// </returns>
  187. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  188. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  189. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  190. [SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", MessageId = "1", Justification = "Stil need to investigate if CharSet.Unicode will not break this.")]
  191. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetProcAddress")]
  192. private static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string procName);
  193. // Note: Leave as: UnmanagedType.LPStr, using UnmanagedType.LPWStr yields no results.
  194. #endregion // GetProcAddress
  195. #endregion // Platform
  196. #endregion // Internal Utility
  197. #region Constants
  198. #region Standard Values
  199. /// <summary>MaxPath = 260
  200. /// The specified path, file name, or both exceed the system-defined maximum length.
  201. /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
  202. /// </summary>
  203. internal const int MaxPath = 260;
  204. /// <summary>MaxPathUnicode = 32767</summary>
  205. internal const int MaxPathUnicode = 32767;
  206. /// <summary>DefaultFileEncoding = Encoding.UTF8; Default type of Encoding used for reading and writing files.</summary>
  207. internal static readonly Encoding DefaultFileEncoding = Encoding.UTF8;
  208. /// <summary>DefaultFileBufferSize = 4096; Default type buffer size used for reading and writing files.</summary>
  209. internal const int DefaultFileBufferSize = 4096;
  210. /// <summary>Combination of <see cref="CopyOptions.FailIfExists"/> and <see cref="CopyOptions.NoBuffering"/></summary>
  211. internal const CopyOptions CopyOptsFail = CopyOptions.FailIfExists | CopyOptions.NoBuffering;
  212. /// <summary>Combination of <see cref="CopyOptions.None"/> and <see cref="CopyOptions.NoBuffering"/></summary>
  213. internal const CopyOptions CopyOptsNone = CopyOptions.None | CopyOptions.NoBuffering;
  214. /// <summary>Combination of <see cref="MoveOptions.ReplaceExisting"/> and <see cref="MoveOptions.CopyAllowed"/></summary>
  215. internal const MoveOptions MoveOptsReplace = MoveOptions.ReplaceExisting | MoveOptions.CopyAllowed;
  216. #endregion // Standard Values
  217. #region Dos Device Flags (Used by DefineDosDevice)
  218. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\DosDeviceAttributes.cs as DosDeviceAttributes enum
  219. // Used by DefineDosDevice
  220. //internal const uint DDD_RAW_TARGET_PATH = 0x00000001;
  221. //internal const uint DDD_REMOVE_DEFINITION = 0x00000002;
  222. //internal const uint DDD_EXACT_MATCH_ON_REMOVE = 0x00000004;
  223. //internal const uint DDD_NO_BROADCAST_SYSTEM = 0x00000008;
  224. #endregion // Dos Device Flags (Used by DefineDosDevice)
  225. #region File System Flags (Used by GetVolumeInformation)
  226. // 2012-02-14: Yomodo; Moved to Filesystem\Enumerations\VolumeInfoAttributes.cs as VolumeInfoAttributes enum
  227. // Used by GetVolumeInformation
  228. //internal const uint FILE_CASE_SENSITIVE_SEARCH = 0x00000001;
  229. //internal const uint FILE_CASE_PRESERVED_NAMES = 0x00000002;
  230. //internal const uint FILE_UNICODE_ON_DISK = 0x00000004;
  231. //internal const uint FILE_PERSISTENT_ACLS = 0x00000008;
  232. //internal const uint FILE_FILE_COMPRESSION = 0x00000010;
  233. //internal const uint FILE_VOLUME_QUOTAS = 0x00000020;
  234. //internal const uint FILE_SUPPORTS_SPARSE_FILES = 0x00000040;
  235. //internal const uint FILE_SUPPORTS_REPARSE_POINTS = 0x00000080;
  236. //internal const uint FILE_SUPPORTS_REMOTE_STORAGE = 0x00000100;
  237. //internal const uint FILE_VOLUME_IS_COMPRESSED = 0x00008000;
  238. //internal const uint FILE_SUPPORTS_OBJECT_IDS = 0x00010000;
  239. //internal const uint FILE_SUPPORTS_ENCRYPTION = 0x00020000;
  240. //internal const uint FILE_NAMED_STREAMS = 0x00040000;
  241. //internal const uint FILE_READ_ONLY_VOLUME = 0x00080000;
  242. #endregion // File System Flags (Used by GetVolumeInformation)
  243. #region Drive Types (Used by GetDriveType)
  244. // 2012-02-14: Yomodo; Obsolete, .NET DriveType enum is used instead.
  245. //internal const uint DRIVE_UNKNOWN = 0;
  246. //internal const uint DRIVE_NO_ROOT_DIR = 1;
  247. //internal const uint DRIVE_REMOVABLE = 2;
  248. //internal const uint DRIVE_FIXED = 3;
  249. //internal const uint DRIVE_REMOTE = 4;
  250. //internal const uint DRIVE_CDROM = 5;
  251. //internal const uint DRIVE_RAMDISK = 6;
  252. #endregion // Drive Types (Used by GetDriveType)
  253. #region File Access and Rights
  254. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\FileSystemRights.cs as FileSystemRights enum
  255. // 2012-10-10: Yomodo; .NET already provides this enum.
  256. //internal const uint DRIVE_UNKNOWN = 0;
  257. //public const uint ACCESS_SYSTEM_SECURITY = 0x01000000;
  258. //public const uint DELETE = 0x00010000;
  259. //public const uint READ_CONTROL = 0x00020000;
  260. //public const uint WRITE_DAC = 0x00040000;
  261. //public const uint WRITE_OWNER = 0x00080000;
  262. //public const uint SYNCHRONIZE = 0x00100000;
  263. //public const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000;
  264. //public const uint STANDARD_RIGHTS_READ = READ_CONTROL;
  265. //public const uint STANDARD_RIGHTS_WRITE = READ_CONTROL;
  266. //public const uint STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
  267. //public const uint STANDARD_RIGHTS_ALL = 0x001F0000;
  268. //public const uint SPECIFIC_RIGHTS_ALL = 0x0000FFFF;
  269. //public const uint FILE_READ_DATA = 0x0001;
  270. //public const uint FILE_LIST_DIRECTORY = 0x0001;
  271. //public const uint FILE_WRITE_DATA = 0x0002;
  272. //public const uint FILE_ADD_FILE = 0x0002;
  273. //public const uint FILE_APPEND_DATA = 0x0004;
  274. //public const uint FILE_ADD_SUBDIRECTORY = 0x0004;
  275. //public const uint FILE_CREATE_PIPE_INSTANCE = 0x0004;
  276. //public const uint FILE_READ_EA = 0x0008;
  277. //public const uint FILE_WRITE_EA = 0x0010;
  278. //public const uint FILE_EXECUTE = 0x0020;
  279. //public const uint FILE_TRAVERSE = 0x0020;
  280. //public const uint FILE_DELETE_CHILD = 0x0040;
  281. //public const uint FILE_READ_ATTRIBUTES = 0x0080;
  282. //public const uint FILE_WRITE_ATTRIBUTES = 0x0100;
  283. //public const uint FILE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF;
  284. //public const uint FILE_GENERIC_READ = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;
  285. //public const uint FILE_GENERIC_WRITE = STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE;
  286. //public const uint FILE_GENERIC_EXECUTE = STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE;
  287. //public const uint FILE_SHARE_READ = 0x00000001;
  288. //public const uint FILE_SHARE_WRITE = 0x00000002;
  289. //public const uint FILE_SHARE_DELETE = 0x00000004;
  290. //public const uint FILE_ATTRIBUTE_READONLY = 0x00000001;
  291. //public const uint FILE_ATTRIBUTE_HIDDEN = 0x00000002;
  292. //public const uint FILE_ATTRIBUTE_SYSTEM = 0x00000004;
  293. //public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
  294. //public const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
  295. //public const uint FILE_ATTRIBUTE_DEVICE = 0x00000040;
  296. //public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
  297. //public const uint FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
  298. //public const uint FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
  299. //public const uint FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;
  300. //public const uint FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
  301. //public const uint FILE_ATTRIBUTE_OFFLINE = 0x00001000;
  302. //public const uint FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
  303. //public const uint FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
  304. //public const uint FILE_ATTRIBUTE_VIRTUAL = 0x00010000;
  305. //public const uint FILE_NOTIFY_CHANGE_FILE_NAME = 0x00000001;
  306. //public const uint FILE_NOTIFY_CHANGE_DIR_NAME = 0x00000002;
  307. //public const uint FILE_NOTIFY_CHANGE_ATTRIBUTES = 0x00000004;
  308. //public const uint FILE_NOTIFY_CHANGE_SIZE = 0x00000008;
  309. //public const uint FILE_NOTIFY_CHANGE_LAST_WRITE = 0x00000010;
  310. //public const uint FILE_NOTIFY_CHANGE_LAST_ACCESS = 0x00000020;
  311. //public const uint FILE_NOTIFY_CHANGE_CREATION = 0x00000040;
  312. //public const uint FILE_NOTIFY_CHANGE_SECURITY = 0x00000100;
  313. //public const uint FILE_ACTION_ADDED = 0x00000001;
  314. //public const uint FILE_ACTION_REMOVED = 0x00000002;
  315. //public const uint FILE_ACTION_MODIFIED = 0x00000003;
  316. //public const uint FILE_ACTION_RENAMED_OLD_NAME = 0x00000004;
  317. //public const uint FILE_ACTION_RENAMED_NEW_NAME = 0x00000005;
  318. //public const uint FILE_SEQUENTIAL_WRITE_ONCE = 0x00100000;
  319. //public const uint FILE_SUPPORTS_TRANSACTIONS = 0x00200000;
  320. //public const uint REPLACEFILE_WRITE_THROUGH = 0x01;
  321. //public const uint REPLACEFILE_IGNORE_MERGE_ERRORS = 0x02;
  322. //public const uint REPLACEFILE_IGNORE_ACL_ERRORS = 0x04;
  323. #endregion // File Access and Rights
  324. #region Security
  325. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\SecurityInformation.cs as SecurityInformation enum
  326. //public const int OWNER_SECURITY_INFORMATION = 0x00000001;
  327. //public const int GROUP_SECURITY_INFORMATION = 0x00000002;
  328. //public const int DACL_SECURITY_INFORMATION = 0x00000004;
  329. //public const int SACL_SECURITY_INFORMATION = 0x00000008;
  330. /* Not needed?
  331. public const uint LABEL_SECURITY_INFORMATION = 0x00000010;
  332. public const uint PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000;
  333. public const uint PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000;
  334. public const uint UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000;
  335. public const uint UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000;
  336. */
  337. #endregion
  338. #region Backup
  339. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\BackupStream.cs as BackupStreamAttributes enum
  340. // (Only moved these values, enum already existed)
  341. //public const uint STREAM_NORMAL_ATTRIBUTE = 0x00000000;
  342. //public const uint STREAM_MODIFIED_WHEN_READ = 0x00000001;
  343. //public const uint STREAM_CONTAINS_SECURITY = 0x00000002;
  344. //public const uint STREAM_CONTAINS_PROPERTIES = 0x00000004;
  345. //public const uint STREAM_SPARSE_ATTRIBUTE = 0x00000008;
  346. #endregion
  347. #endregion // Constants
  348. #region Network
  349. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\RemoteNameInfo.cs as RemoteNameInfo enum
  350. //[StructLayout(LayoutKind.Sequential)]
  351. //internal struct UNIVERSAL_NAME_INFO
  352. //{
  353. // /// <summary>
  354. // /// Network share name string that identifies a network resource.
  355. // /// A network share path identifies a network resource in an unambiguous, computer-independent manner.
  356. // /// You can pass the path to processes on other computers, allowing those processes to obtain access to the network resource.
  357. // /// </summary>
  358. // public string universalName;
  359. //}
  360. //public const uint UNIVERSAL_NAME_INFO_LEVEL = 0x00000001;
  361. //public const uint REMOTE_NAME_INFO_LEVEL = 0x00000002;
  362. //[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  363. //internal struct UNIVERSAL_NAME_INFO
  364. //{
  365. // [MarshalAs(UnmanagedType.LPWStr)] public string universalName;
  366. //}
  367. /// <summary>The WNetGetUniversalName function takes a drive-based path for a network resource and returns an information structure that contains a more universal form of the name.</summary>
  368. /// <returns>
  369. /// If the function succeeds, the return value is <see cref="Win32Errors.NO_ERROR"/>
  370. /// If the function fails, the return value is a system error code, <see cref="Win32Errors"/>
  371. /// </returns>
  372. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  373. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  374. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  375. [DllImport("mpr.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "WNetGetUniversalNameW")]
  376. [return: MarshalAs(UnmanagedType.U4)]
  377. internal static extern uint WNetGetUniversalName([MarshalAs(UnmanagedType.LPWStr)] string lpLocalPath, [MarshalAs(UnmanagedType.U4)] uint dwInfoLevel, SafeGlobalMemoryBufferHandle lpBuffer, [MarshalAs(UnmanagedType.U4)] out uint lpBufferSize);
  378. #endregion // Network
  379. #region Volume Management
  380. #region DosDevice
  381. /// <summary>Defines, redefines, or deletes MS-DOS device names.</summary>
  382. /// <returns>
  383. /// If the function succeeds, the return value is nonzero.
  384. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  385. /// To get extended error information call Win32Exception()
  386. /// </returns>
  387. /// <remarks>Minimum supported client: Windows XP</remarks>
  388. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  389. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  390. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DefineDosDeviceW")]
  391. [return: MarshalAs(UnmanagedType.Bool)]
  392. internal static extern bool DefineDosDevice(DosDeviceAttributes dwAttributes, [MarshalAs(UnmanagedType.LPWStr)] string lpDeviceName, [MarshalAs(UnmanagedType.LPWStr)] string lpTargetPath);
  393. /// <summary>Retrieves information about MS-DOS device names.</summary>
  394. /// <returns>
  395. /// If the function succeeds, the return value is nonzero.
  396. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  397. /// To get extended error information call Win32Exception()
  398. /// If the buffer is too small, the function fails and the last error code is ERROR_INSUFFICIENT_BUFFER.
  399. /// </returns>
  400. /// <remarks>
  401. /// Windows Server 2003 and Windows XP: QueryDosDevice first searches the Local MS-DOS Device namespace for the specified device name.
  402. /// If the device name is not found, the function will then search the Global MS-DOS Device namespace.
  403. /// </remarks>
  404. /// <remarks>Minimum supported client: Windows XP</remarks>
  405. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  406. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  407. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "QueryDosDeviceW")]
  408. [return: MarshalAs(UnmanagedType.U4)]
  409. internal static extern uint QueryDosDevice([MarshalAs(UnmanagedType.LPWStr)] string lpDeviceName, char[] lpTargetPath, [MarshalAs(UnmanagedType.U4)] uint ucchMax);
  410. #endregion // DosDevice
  411. #region Volume
  412. /// <summary>Retrieves the name of a volume on a computer. FindFirstVolume is used to begin scanning the volumes of a computer.</summary>
  413. /// <returns>
  414. /// If the function succeeds, the return value is a search handle used in a subsequent call to the FindNextVolume and FindVolumeClose functions.
  415. /// If the function fails to find any volumes, the return value is the INVALID_HANDLE_VALUE error code. To get extended error information, call GetLastError.
  416. /// </returns>
  417. /// <remarks>Minimum supported client: Windows XP</remarks>
  418. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  419. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  420. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstVolumeW")]
  421. internal extern static SafeFindVolumeHandle FindFirstVolume(StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
  422. /// <summary>Retrieves the name of a mounted folder on the specified volume. FindFirstVolumeMountPoint is used to begin scanning the mounted folders on a volume.</summary>
  423. /// <returns>
  424. /// If the function succeeds, the return value is a search handle used in a subsequent call to the FindNextVolumeMountPoint and FindVolumeMountPointClose functions.
  425. /// If the function fails to find a mounted folder on the volume, the return value is the INVALID_HANDLE_VALUE error code.
  426. /// </returns>
  427. /// <remarks>Minimum supported client: Windows XP</remarks>
  428. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  429. /// <remarks>"lpszRootPathName" must end with a trailing backslash.</remarks>
  430. /// <remarks>
  431. /// Might not enumerate all: http://blogs.msdn.com/b/seealso/archive/2011/07/27/use-this-not-this-apis-to-avoid-all-together.aspx
  432. /// After Windows Vista, the API has changed. The short version of the issue you may encounter with FindFirstVolumeMountPoint is that unless you have a superuser account and permissions to the share, the mount point will not show up in the list.
  433. /// Your enumerated lists of mount points can be incomplete and you will not know when listed folders require permissions.</remarks>
  434. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  435. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstVolumeMountPointW")]
  436. internal extern static SafeFindVolumeMountPointHandle FindFirstVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszRootPathName, StringBuilder lpszVolumeMountPoint, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
  437. /// <summary>Continues a volume search started by a call to the FindFirstVolume function. FindNextVolume finds one volume per call.</summary>
  438. /// <returns>
  439. /// If the function succeeds, the return value is nonzero.
  440. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function
  441. /// returns the ERROR_NO_MORE_FILES error code. In that case, close the search with the FindVolumeClose function.
  442. /// </returns>
  443. /// <remarks>Minimum supported client: Windows XP</remarks>
  444. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  445. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  446. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextVolumeW")]
  447. [return: MarshalAs(UnmanagedType.Bool)]
  448. internal extern static bool FindNextVolume(SafeHandle hFindVolume, StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
  449. /// <summary>Continues a mounted folder search started by a call to the FindFirstVolumeMountPoint function. FindNextVolumeMountPoint finds one mounted folder per call.</summary>
  450. /// <returns>
  451. /// If the function succeeds, the return value is nonzero.
  452. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. If no more mounted folders can be found, the GetLastError function returns the ERROR_NO_MORE_FILES error code.
  453. /// In that case, close the search with the FindVolumeMountPointClose function.
  454. /// </returns>
  455. /// <remarks>Minimum supported client: Windows XP</remarks>
  456. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  457. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  458. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextVolumeMountPointW")]
  459. [return: MarshalAs(UnmanagedType.Bool)]
  460. internal extern static bool FindNextVolumeMountPoint(SafeHandle hFindVolume, StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
  461. /// <summary>Closes the specified volume search handle.</summary>
  462. /// <returns>
  463. /// If the function succeeds, the return value is nonzero.
  464. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  465. /// </returns>
  466. /// <remarks>Minimum supported client: Windows XP</remarks>
  467. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  468. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  469. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  470. [return: MarshalAs(UnmanagedType.Bool)]
  471. internal extern static bool FindVolumeClose(IntPtr hFindVolume);
  472. /// <summary>Closes the specified mounted folder search handle.</summary>
  473. /// <returns>
  474. /// If the function succeeds, the return value is nonzero.
  475. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  476. /// </returns>
  477. /// <remarks>Minimum supported client: Windows XP</remarks>
  478. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  479. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  480. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  481. [return: MarshalAs(UnmanagedType.Bool)]
  482. internal extern static bool FindVolumeMountPointClose(IntPtr hFindVolume);
  483. /// <summary>Retrieves a volume GUID path for the volume that is associated with the specified volume mount point (drive letter, volume GUID path, or mounted folder).</summary>
  484. /// <returns>
  485. /// If the function succeeds, the return value is nonzero.
  486. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  487. /// To get extended error information call Win32Exception()
  488. /// </returns>
  489. /// <remarks>Minimum supported client: Windows XP</remarks>
  490. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  491. /// <remarks>"lpszVolumeMountPoint" must end with a trailing backslash.</remarks>
  492. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  493. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumeNameForVolumeMountPointW")]
  494. [return: MarshalAs(UnmanagedType.Bool)]
  495. public static extern bool GetVolumeNameForVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint, StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
  496. /// <summary>Retrieves the volume mount point where the specified path is mounted.</summary>
  497. /// <returns>
  498. /// If the function succeeds, the return value is nonzero.
  499. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  500. /// To get extended error information call Win32Exception()
  501. /// </returns>
  502. /// <remarks>Minimum supported client: Windows XP</remarks>
  503. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  504. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  505. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumePathNameW")]
  506. [return: MarshalAs(UnmanagedType.Bool)]
  507. public static extern bool GetVolumePathName([MarshalAs(UnmanagedType.LPWStr)] string lpszFileName, StringBuilder lpszVolumePathName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
  508. /// <summary>Retrieves a list of drive letters and mounted folder paths for the specified volume.</summary>
  509. /// <returns>
  510. /// If the function succeeds, the return value is nonzero.
  511. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  512. /// </returns>
  513. /// <remarks>Minimum supported client: Windows XP</remarks>
  514. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  515. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  516. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumePathNamesForVolumeNameW")]
  517. [return: MarshalAs(UnmanagedType.Bool)]
  518. public static extern bool GetVolumePathNamesForVolumeName([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeName, char[] lpszVolumePathNames, [MarshalAs(UnmanagedType.U4)] uint cchBuferLength, [MarshalAs(UnmanagedType.U4)] out uint lpcchReturnLength);
  519. /// <summary>Retrieves information about the file system and volume associated with the specified root directory.</summary>
  520. /// <returns>
  521. /// If all the requested information is retrieved, the return value is nonzero.
  522. /// If not all the requested information is retrieved, the return value is zero.
  523. /// </returns>
  524. /// <remarks>Minimum supported client: Windows XP</remarks>
  525. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  526. /// <remarks>"lpRootPathName" must end with a trailing backslash.</remarks>
  527. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  528. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumeInformationW")]
  529. [return: MarshalAs(UnmanagedType.Bool)]
  530. internal extern static bool GetVolumeInformation([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName, StringBuilder lpVolumeNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nVolumeNameSize, [MarshalAs(UnmanagedType.U4)] out uint lpVolumeSerialNumber, [MarshalAs(UnmanagedType.U4)] out uint lpMaximumComponentLength, [MarshalAs(UnmanagedType.U4)] out VolumeInfoAttributes lpFileSystemAttributes, StringBuilder lpFileSystemNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nFileSystemNameSize);
  531. /// <summary>Retrieves information about the file system and volume associated with the specified file.</summary>
  532. /// <returns>
  533. /// If all the requested information is retrieved, the return value is nonzero.
  534. /// If not all the requested information is retrieved, the return value is zero.
  535. /// To get extended error information call Win32Exception()
  536. /// </returns>
  537. /// <remarks>Minimum supported client: Windows Vista</remarks>
  538. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  539. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  540. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumeInformationByHandleW")]
  541. [return: MarshalAs(UnmanagedType.Bool)]
  542. internal extern static bool GetVolumeInformationByHandle(SafeHandle hFile, StringBuilder lpVolumeNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nVolumeNameSize, [MarshalAs(UnmanagedType.U4)] out uint lpVolumeSerialNumber, [MarshalAs(UnmanagedType.U4)] out uint lpMaximumComponentLength, out VolumeInfoAttributes lpFileSystemAttributes, StringBuilder lpFileSystemNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nFileSystemNameSize);
  543. /// <summary>Sets the label of a file system volume.</summary>
  544. /// <returns>
  545. /// If the function succeeds, the return value is nonzero.
  546. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  547. /// </returns>
  548. /// <remarks>Minimum supported client: Windows XP</remarks>
  549. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  550. /// <remarks>"lpRootPathName" must end with a trailing backslash.</remarks>
  551. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  552. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetVolumeLabelW")]
  553. [return: MarshalAs(UnmanagedType.Bool)]
  554. internal extern static bool SetVolumeLabel([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName, [MarshalAs(UnmanagedType.LPWStr)] string lpVolumeName);
  555. /// <summary>Associates a volume with a drive letter or a directory on another volume.</summary>
  556. /// <returns>
  557. /// If the function succeeds, the return value is nonzero.
  558. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  559. /// </returns>
  560. /// <remarks>Minimum supported client: Windows XP</remarks>
  561. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  562. /// <remarks>"lpszVolumeMountPoint" must end with a trailing backslash.</remarks>
  563. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  564. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetVolumeMountPointW")]
  565. [return: MarshalAs(UnmanagedType.Bool)]
  566. internal extern static bool SetVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint, [MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeName);
  567. /// <summary>Deletes a drive letter or mounted folder.</summary>
  568. /// If all the requested information is retrieved, the return value is nonzero.
  569. /// If not all the requested information is retrieved, the return value is zero.
  570. /// To get extended error information call Win32Exception()
  571. /// <remarks>Deleting a mounted folder does not cause the underlying directory to be deleted.
  572. /// It's not an error to attempt to unmount a volume from a volume mount point when there is no volume actually mounted at that volume mount point.
  573. /// </remarks>
  574. /// <remarks>Minimum supported client: Windows XP</remarks>
  575. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  576. /// <remarks>"lpszVolumeMountPoint" must end with a trailing backslash.</remarks>
  577. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  578. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DeleteVolumeMountPointW")]
  579. [return: MarshalAs(UnmanagedType.Bool)]
  580. internal extern static bool DeleteVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint);
  581. #endregion // Volume
  582. #endregion // Volume Management
  583. #region Disk Management
  584. /// <summary>Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.</summary>
  585. /// <returns>The return value specifies the type of drive, which can be one of the following <see cref="DriveType"/> values.</returns>
  586. /// <remarks>Minimum supported client: Windows XP</remarks>
  587. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  588. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  589. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetDriveTypeW")]
  590. [return: MarshalAs(UnmanagedType.U4)]
  591. internal extern static DriveType GetDriveType([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName);
  592. /// <summary>Retrieves information about the specified disk, including the amount of free space on the disk.</summary>
  593. /// <returns>
  594. /// If the function succeeds, the return value is nonzero.
  595. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  596. /// </returns>
  597. /// <remarks>
  598. /// Symbolic link behavior; if the path points to a symbolic link, the operation is performed on the target.
  599. /// </remarks>
  600. /// <remarks>Minimum supported client: Windows XP</remarks>
  601. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  602. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  603. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetDiskFreeSpaceW")]
  604. [return: MarshalAs(UnmanagedType.Bool)]
  605. internal static extern bool GetDiskFreeSpace([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName, out ulong lpSectorsPerCluster, out ulong lpBytesPerSector, out ulong lpNumberOfFreeClusters, out ulong lpTotalNumberOfClusters);
  606. /// <summary>Retrieves information about the amount of space that is available on a disk volume, which is the total amount of space,
  607. /// the total amount of free space, and the total amount of free space available to the user that is associated with the calling thread.
  608. /// </summary>
  609. /// <returns>
  610. /// If the function succeeds, the return value is nonzero.
  611. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  612. /// </returns>
  613. /// <remarks>
  614. /// Symbolic link behavior; if the path points to a symbolic link, the operation is performed on the target.
  615. /// </remarks>
  616. /// <remarks>Minimum supported client: Windows XP</remarks>
  617. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  618. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  619. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetDiskFreeSpaceExW")]
  620. [return: MarshalAs(UnmanagedType.Bool)]
  621. internal static extern bool GetDiskFreeSpaceEx([MarshalAs(UnmanagedType.LPWStr)] string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes);
  622. /// <summary>Retrieves a bitmask representing the currently available disk drives.</summary>
  623. /// <returns>
  624. /// If the function succeeds, the return value is a bitmask representing the currently available disk drives.
  625. /// Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.
  626. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  627. /// </returns>
  628. /// <remarks>Minimum supported client: Windows XP</remarks>
  629. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  630. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  631. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  632. [return: MarshalAs(UnmanagedType.U4)]
  633. internal static extern uint GetLogicalDrives();
  634. #endregion // Disk Management
  635. #region Path
  636. #region GetLongPathName
  637. /// <summary>Converts the specified path to its long form.</summary>
  638. /// <returns>
  639. /// If the function succeeds, the return value is nonzero.
  640. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  641. /// </returns>
  642. /// <remarks>Minimum supported client: Windows XP</remarks>
  643. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  644. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  645. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetLongPathNameW")]
  646. [return: MarshalAs(UnmanagedType.U4)]
  647. internal static extern uint GetLongPathName([MarshalAs(UnmanagedType.LPWStr)] string lpszShortPath, StringBuilder lpszLongPath, [MarshalAs(UnmanagedType.U4)] uint cchBuffer);
  648. #endregion // GetLongPathName
  649. #region GetShortPathName
  650. /// <summary>Retrieves the short path form of the specified path.</summary>
  651. /// <returns>
  652. /// If the function succeeds, the return value is nonzero.
  653. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  654. /// </returns>
  655. /// <remarks>Minimum supported client: Windows XP</remarks>
  656. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  657. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  658. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW")]
  659. [return: MarshalAs(UnmanagedType.U4)]
  660. internal static extern uint GetShortPathName([MarshalAs(UnmanagedType.LPWStr)] string lpszLongPath, StringBuilder lpszShortPath, [MarshalAs(UnmanagedType.U4)] uint cchBuffer);
  661. #endregion // GetShortPathName
  662. #region Path/Url Conversion
  663. /// <summary>Converts a file URL to a Microsoft MS-DOS path.</summary>
  664. /// <returns>Type: HRESULT
  665. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
  666. /// </returns>
  667. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  668. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  669. /// <remarks>This function returns a standard COM error value, so set "PreserveSig" to <see langref="false"/> to automatically convert HRESULT or retval values to exceptions.</remarks>
  670. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  671. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathCreateFromUrlW", PreserveSig = false)]
  672. [return: MarshalAs(UnmanagedType.U4)]
  673. internal static extern uint PathCreateFromUrl([MarshalAs(UnmanagedType.LPWStr)] string pszUrl, StringBuilder pszPath, ref uint pcchPath, uint dwFlags);
  674. /// <summary>Creates a path from a file URL.</summary>
  675. /// <returns>Type: HRESULT
  676. /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
  677. /// </returns>
  678. /// <remarks>Minimum supported client: Windows Vista</remarks>
  679. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  680. /// <remarks>This function returns a standard COM error value, so set "PreserveSig" to <see langref="false"/> to automatically convert HRESULT or retval values to exceptions.</remarks>
  681. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  682. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, PreserveSig = false)]
  683. [return: MarshalAs(UnmanagedType.U4)]
  684. internal static extern uint PathCreateFromUrlAlloc([MarshalAs(UnmanagedType.LPWStr)] string pszIn, out StringBuilder pszPath, uint dwFlags);
  685. /// <summary>Converts a Microsoft MS-DOS path to a canonicalized URL.</summary>
  686. /// <returns>Type: HRESULT
  687. /// Returns S_FALSE if pszPath is already in URL format. In this case, pszPath will simply be copied to pszUrl.
  688. /// Otherwise, it returns S_OK if successful or a standard COM error value if not.
  689. /// </returns>
  690. /// <remarks>
  691. /// UrlCreateFromPath does not support extended paths. These are paths that include the extended-length path prefix "\\?\".
  692. /// </remarks>
  693. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  694. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  695. /// <remarks>This function returns a standard COM error value, so set "PreserveSig" to <see langref="false"/> to automatically convert HRESULT or retval values to exceptions.</remarks>
  696. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  697. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "UrlCreateFromPathW", PreserveSig = false)]
  698. [return: MarshalAs(UnmanagedType.U4)]
  699. internal static extern uint UrlCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, StringBuilder pszUrl, ref uint pcchUrl, uint dwFlags);
  700. /// <summary>Tests whether a URL is a specified type.</summary>
  701. /// <returns>
  702. /// Type: BOOL
  703. /// For all but one of the URL types, UrlIs returns true if the URL is the specified type, or false if not.
  704. /// If UrlIs is set to <see cref="Shell32.UrlTypes.IsAppliable"/>, UrlIs will attempt to determine the URL scheme.
  705. /// If the function is able to determine a scheme, it returns true, or false otherwise.
  706. /// </returns>
  707. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  708. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  709. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  710. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "UrlIsW")]
  711. [return: MarshalAs(UnmanagedType.Bool)]
  712. internal static extern bool UrlIs([MarshalAs(UnmanagedType.LPWStr)] string pszUrl, Shell32.UrlTypes urlIs);
  713. #endregion // Path/Url Conversion
  714. #endregion // Path
  715. #region Directory Management
  716. /// <summary>Creates a new directory.
  717. /// If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory.
  718. /// </summary>
  719. /// <returns>
  720. /// If the function succeeds, the return value is nonzero.
  721. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  722. /// </returns>
  723. /// <remarks>Minimum supported client: Windows XP</remarks>
  724. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  725. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  726. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryW")]
  727. [return: MarshalAs(UnmanagedType.Bool)]
  728. internal extern static bool CreateDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, [MarshalAs(UnmanagedType.LPStruct)] SecurityAttributes lpSecurityAttributes);
  729. /// <summary>Creates a new directory with the attributes of a specified template directory.
  730. /// If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory.
  731. /// The new directory retains the other attributes of the specified template directory.
  732. /// </summary>
  733. /// <returns>
  734. /// If the function succeeds, the return value is nonzero.
  735. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  736. /// </returns>
  737. /// <remarks>Minimum supported client: Windows XP</remarks>
  738. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  739. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  740. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryExW")]
  741. [return: MarshalAs(UnmanagedType.Bool)]
  742. internal extern static bool CreateDirectoryEx([MarshalAs(UnmanagedType.LPWStr)] string lpTemplateDirectory, [MarshalAs(UnmanagedType.LPWStr)] string lpPathName, [MarshalAs(UnmanagedType.LPStruct)] SecurityAttributes lpSecurityAttributes);
  743. /// <summary>Creates a new directory as a transacted operation, with the attributes of a specified template directory.
  744. /// If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory.
  745. /// The new directory retains the other attributes of the specified template directory.
  746. /// </summary>
  747. /// <returns>
  748. /// If the function succeeds, the return value is nonzero.
  749. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  750. /// </returns>
  751. /// <remarks>Minimum supported client: Windows Vista</remarks>
  752. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  753. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  754. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateDirectoryTransactedW")]
  755. [return: MarshalAs(UnmanagedType.Bool)]
  756. internal static extern bool CreateDirectoryTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpTemplateDirectory, [MarshalAs(UnmanagedType.LPWStr)] string lpNewDirectory, [MarshalAs(UnmanagedType.LPStruct)] SecurityAttributes lpSecurityAttributes, SafeHandle hTransaction);
  757. /// <summary>Deletes an existing empty directory.</summary>
  758. /// <returns>
  759. /// If the function succeeds, the return value is nonzero.
  760. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  761. /// </returns>
  762. /// <remarks>
  763. /// RemoveDirectory removes a directory junction, even if the contents of the target are not empty; the function removes directory
  764. /// junctions regardless of the state of the target object. For more information on junctions, see Hard Links and Junctions.
  765. /// </remarks>
  766. /// <remarks>Minimum supported client: Windows XP</remarks>
  767. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  768. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  769. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RemoveDirectoryW")]
  770. [return: MarshalAs(UnmanagedType.Bool)]
  771. internal extern static bool RemoveDirectory([MarshalAs(UnmanagedType.LPWStr)] string lpPathName);
  772. /// <summary>Deletes an existing empty directory as a transacted operation.</summary>
  773. /// <returns>
  774. /// If the function succeeds, the return value is nonzero.
  775. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  776. /// </returns>
  777. /// <remarks>
  778. /// RemoveDirectory removes a directory junction, even if the contents of the target are not empty; the function removes directory
  779. /// junctions regardless of the state of the target object. For more information on junctions, see Hard Links and Junctions.
  780. /// </remarks>
  781. /// <remarks>Minimum supported client:Windows Vista</remarks>
  782. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  783. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  784. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RemoveDirectoryTransactedW")]
  785. [return: MarshalAs(UnmanagedType.Bool)]
  786. internal static extern bool RemoveDirectoryTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, SafeHandle hTransaction);
  787. #endregion // Directory Management
  788. #region File Management
  789. #region Types
  790. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\InfoStandard.cs as GetFileExInfoLevels enum.
  791. ///// <summary>
  792. ///// Defines values that are used with the GetFileAttributes function to specify the information level of the returned data.
  793. ///// </summary>
  794. //internal enum GetFileExInfoLevels : uint
  795. //{
  796. // /// <summary>
  797. // /// The GetFileAttributes function retrieves a standard set of attribute information. The data is returned in a WIN32_FILE_ATTRIBUTE_DATA structure.
  798. // /// </summary>
  799. // InfoStandard,
  800. // /// <summary>
  801. // /// One greater than the maximum value. Valid values for this enumeration will be less than this value.
  802. // /// </summary>
  803. // MaxInfoLevel
  804. //}
  805. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\InfoStandard.cs as Win32FindData struct.
  806. ///// <summary>
  807. ///// Note: For some marshalling reason Win32FindData whould be declared as class not a struct.
  808. ///// 2012-03-03: Changing the function's signature: "[In, Out]" to: "out", enables the usage of struct.
  809. ///// </summary>
  810. //[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  811. //internal struct WIN32_FIND_DATA
  812. //{
  813. // public FileAttributes dwFileAttributes;
  814. // public FileTime ftCreationTime;
  815. // public FileTime ftLastAccessTime;
  816. // public FileTime ftLastWriteTime;
  817. // public uint nFileSizeHigh;
  818. // public uint nFileSizeLow;
  819. // public ReparsePointTags dwReserved0;
  820. // public uint dwReserved1;
  821. // [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
  822. // public string cFileName;
  823. // [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  824. // public string cAlternateFileName;
  825. //}
  826. // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\InfoStandard.cs as Win32FileAttributeData struct.
  827. //[StructLayout(LayoutKind.Sequential)]
  828. //internal struct WIN32_FILE_ATTRIBUTE_DATA
  829. //{
  830. // public FileAttributes dwFileAttributes;
  831. // public FileTime ftCreationTime;
  832. // public FileTime ftLastAccessTime;
  833. // public FileTime ftLastWriteTime;
  834. // public uint nFileSizeHigh;
  835. // public uint nFileSizeLow;
  836. //}
  837. //2012-09-28: Yomodo; Not needed anymore.
  838. //[StructLayout(LayoutKind.Sequential)]
  839. //internal struct UNIVERSAL_NAME_INFO
  840. //{
  841. // public string universalName;
  842. //}
  843. #endregion // Types
  844. #region Mapping (View, not Network share)
  845. /// <summary>Creates or opens a named or unnamed file mapping object for a specified file.</summary>
  846. /// <returns>
  847. /// If the function succeeds, the return value is a handle to the newly created file mapping object.
  848. /// If the function fails, the return value is <see langword="null"/>.
  849. /// </returns>
  850. /// <remarks>SetLastError is set to false.</remarks>
  851. /// <remarks>Minimum supported client: Windows XP</remarks>
  852. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  853. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  854. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode, EntryPoint = "CreateFileMappingW")]
  855. internal static extern SafeFileHandle CreateFileMapping(SafeFileHandle hFile, SecurityAttributes lpSecurityAttributes, FileMapProtections flProtect, [MarshalAs(UnmanagedType.U4)] uint dwMaximumSizeHigh, [MarshalAs(UnmanagedType.U4)] uint dwMaximumSizeLow, [MarshalAs(UnmanagedType.LPWStr)] string lpName);
  856. /// <summary>Checks whether the specified address is within a memory-mapped file in the address space of the specified process.
  857. /// If so, the function returns the name of the memory-mapped file.
  858. /// </summary>
  859. /// <returns>
  860. /// If the function succeeds, the return value is nonzero.
  861. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  862. /// </returns>
  863. /// <remarks>SetLastError is set to false.</remarks>
  864. /// <remarks>Minimum supported client: Windows XP</remarks>
  865. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  866. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  867. [DllImport("psapi.dll", SetLastError = false, CharSet = CharSet.Unicode, EntryPoint = "GetMappedFileNameW")]
  868. [return: MarshalAs(UnmanagedType.U4)]
  869. internal static extern uint GetMappedFileName(IntPtr hProcess, SafeLocalMemoryBufferHandle lpv, StringBuilder lpFilename, [MarshalAs(UnmanagedType.U4)] uint nSize);
  870. /// <summary>Maps a view of a file mapping into the address space of a calling process.</summary>
  871. /// <returns>
  872. /// If the function succeeds, the return value is the starting address of the mapped view.
  873. /// If the function fails, the return value is <see langword="null"/>.
  874. /// </returns>
  875. /// <remarks>SetLastError is set to false.</remarks>
  876. /// <remarks>Minimum supported client: Windows XP</remarks>
  877. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  878. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  879. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
  880. internal static extern SafeLocalMemoryBufferHandle MapViewOfFile(SafeFileHandle hFileMappingObject, FileMapAccess dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetHigh, [MarshalAs(UnmanagedType.U4)] uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);
  881. /// <summary>Unmaps a mapped view of a file from the calling process's address space.</summary>
  882. /// <returns>
  883. /// If the function succeeds, the return value is nonzero.
  884. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  885. /// </returns>
  886. /// <remarks>SetLastError is set to false.</remarks>
  887. /// <remarks>Minimum supported client: Windows XP</remarks>
  888. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  889. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  890. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
  891. [return: MarshalAs(UnmanagedType.Bool)]
  892. internal static extern bool UnmapViewOfFile(SafeLocalMemoryBufferHandle lpBaseAddress);
  893. /// <summary>Retrieves the final path for the specified file.</summary>
  894. /// <returns>
  895. /// If the function succeeds, the return value is nonzero.
  896. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  897. /// </returns>
  898. /// <remarks>Minimum supported client: Windows Vista</remarks>
  899. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  900. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  901. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFinalPathNameByHandleW")]
  902. [return: MarshalAs(UnmanagedType.U4)]
  903. internal static extern uint GetFinalPathNameByHandle(SafeFileHandle hFile, StringBuilder lpszFilePath, [MarshalAs(UnmanagedType.U4)] uint cchFilePath, FinalPathFormats dwFlags);
  904. #endregion // Mapping (View, not Network share)
  905. #region Type
  906. /// <summary>Retrieves the file type of the specified file.</summary>
  907. /// <returns>
  908. /// You can distinguish between a "valid" return of FILE_TYPE_UNKNOWN and its return due to a calling error
  909. /// (for example, passing an invalid handle to GetFileType) by calling Win32Exception().
  910. /// If the function worked properly and FILE_TYPE_UNKNOWN was returned, a call to GetLastError will return NO_ERROR.
  911. /// If the function returned FILE_TYPE_UNKNOWN due to an error in calling GetFileType, Win32Exception() will return the error code.
  912. /// </returns>
  913. /// <remarks>
  914. /// "Don't let more than one process try to read from stdin at the same time."
  915. /// http://blogs.msdn.com/b/oldnewthing/archive/2011/12/02/10243553.aspx
  916. /// </remarks>
  917. /// <remarks>Minimum supported client: Windows XP</remarks>
  918. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  919. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  920. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  921. [return: MarshalAs(UnmanagedType.U4)]
  922. internal static extern FileTypes GetFileType(SafeFileHandle hFile);
  923. /// <summary>Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.</summary>
  924. /// <remarks>You should call this function from a background thread. Failure to do so could cause the UI to stop responding.</remarks>
  925. /// <remarks>Minimum supported client: Windows XP</remarks>
  926. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  927. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  928. [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SHGetFileInfoW")]
  929. internal static extern UIntPtr SHGetFileInfo([MarshalAs(UnmanagedType.LPWStr)] string pszPath, FileAttributes dwFileAttributes, out Shell32.FileInfo psfi, [MarshalAs(UnmanagedType.U4)] uint cbFileInfo, Shell32.FileInfoAttributes uFileIconSize);
  930. /// <summary>Retrieves the name of and handle to the executable (.exe) file associated with a specific document file.
  931. /// This is the application that is launched when the document file is directly double-clicked or when Open is chosen from the file's shortcut menu.
  932. /// </summary>
  933. /// <remarks>Minimum supported client: Windows XP</remarks>
  934. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  935. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  936. [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindExecutableW")]
  937. internal static extern SafeFindFileHandle FindExecutable([MarshalAs(UnmanagedType.LPWStr)] string lpFile, [MarshalAs(UnmanagedType.LPWStr)] string lpDirectory, StringBuilder lpResult);
  938. /// <summary>Searches for and retrieves a file or protocol association-related string from the registry.</summary>
  939. /// <returns>Return value Type: HRESULT. Returns a standard COM error value, including the following: S_OK, E_POINTER and S_FALSE.</returns>
  940. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  941. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  942. /// <remarks>This function returns a standard COM error value, so set "PreserveSig" to <see langref="false"/> to automatically convert HRESULT or retval values to exceptions.</remarks>
  943. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  944. [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "AssocQueryStringW", PreserveSig = false)]
  945. [return: MarshalAs(UnmanagedType.U4)]
  946. internal static extern uint AssocQueryString(Shell32.AssociationAttributes flags, Shell32.AssociationString str, [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, [MarshalAs(UnmanagedType.LPWStr)] string pszExtra, StringBuilder pszOut, [MarshalAs(UnmanagedType.U4)] out uint pcchOut);
  947. #endregion // Type
  948. #region Copy/Move
  949. internal delegate CopyProgressResult NativeCopyProgressRoutine(long totalFileSize, long totalBytesTransferred, long streamSize, long streamBytesTransferred, uint dwStreamNumber, CopyProgressCallbackReason dwCallbackReason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData);
  950. /// <summary>Copies an existing file to a new file, notifying the application of its progress through a callback function.</summary>
  951. /// <returns>
  952. /// If the function succeeds, the return value is nonzero.
  953. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  954. /// </returns>
  955. /// <remarks>Minimum supported client: Windows XP</remarks>
  956. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  957. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  958. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CopyFileExW")]
  959. [return: MarshalAs(UnmanagedType.Bool)]
  960. internal static extern bool CopyFileEx([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyProgressRoutine lpProgressRoutine, IntPtr lpData, out int pbCancel, CopyOptions dwCopyFlags);
  961. /// <summary>Copies an existing file to a new file as a transacted operation, notifying the application of its progress through a callback function.</summary>
  962. /// <returns>
  963. /// If the function succeeds, the return value is nonzero.
  964. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  965. /// </returns>
  966. /// <remarks>Minimum supported client: Windows Vista</remarks>
  967. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  968. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  969. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CopyFileTransactedW")]
  970. [return: MarshalAs(UnmanagedType.Bool)]
  971. internal static extern bool CopyFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyProgressRoutine lpProgressRoutine, IntPtr lpData, out int pbCancel, CopyOptions dwCopyFlags, SafeHandle hTransaction);
  972. /// <summary>Moves a file or directory, including its children. You can provide a callback function that receives progress notifications.</summary>
  973. /// <returns>
  974. /// If the function succeeds, the return value is nonzero.
  975. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  976. /// </returns>
  977. /// <remarks>Minimum supported client: Windows XP</remarks>
  978. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  979. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  980. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "MoveFileWithProgressW")]
  981. [return: MarshalAs(UnmanagedType.Bool)]
  982. internal static extern bool MoveFileWithProgress([MarshalAs(UnmanagedType.LPWStr)] string existingFileName, [MarshalAs(UnmanagedType.LPWStr)] string newFileName, NativeCopyProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.U4)] MoveOptions dwFlags);
  983. /// <summary>Moves an existing file or a directory, including its children, as a transacted operation.</summary>
  984. /// <returns>
  985. /// If the function succeeds, the return value is nonzero.
  986. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  987. /// </returns>
  988. /// <remarks>Minimum supported client: Windows Vista</remarks>
  989. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  990. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  991. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "MoveFileTransactedW")]
  992. [return: MarshalAs(UnmanagedType.Bool)]
  993. internal static extern bool MoveFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, NativeCopyProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.U4)] MoveOptions dwCopyFlags, SafeHandle hTransaction);
  994. #endregion // Copy/Move
  995. #region Create
  996. /// <summary>Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, and pipe.</summary>
  997. /// <returns>
  998. /// If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.
  999. /// If the function fails, the return value is <see cref="Win32Errors.ERROR_INVALID_HANDLE"/>. To get extended error information, call GetLastError.
  1000. /// </returns>
  1001. /// <remarks>Minimum supported client: Windows XP</remarks>
  1002. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1003. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1004. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateFileW")]
  1005. internal static extern SafeFileHandle CreateFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileSystemRights dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, SecurityAttributes lpSecurityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition, [MarshalAs(UnmanagedType.U4)] EFileAttributes dwFlagsAndAttributes, SafeHandle hTemplateFile);
  1006. /// <summary>Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, and pipe.</summary>
  1007. /// <returns>
  1008. /// If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.
  1009. /// If the function fails, the return value is <see cref="Win32Errors.ERROR_INVALID_HANDLE"/>. To get extended error information, call GetLastError.
  1010. /// </returns>
  1011. /// <remarks>Minimum supported client: Windows XP</remarks>
  1012. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1013. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1014. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateFileTransactedW")]
  1015. internal static extern SafeFileHandle CreateFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileSystemRights dwDesiredAccess, [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode, SecurityAttributes lpSecurityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition, [MarshalAs(UnmanagedType.U4)] EFileAttributes dwFlagsAndAttributes, SafeHandle hTemplateFile, SafeHandle hTransaction, IntPtr pusMiniVersion, IntPtr pExtendedParameter);
  1016. #endregion // Create
  1017. #region Delete
  1018. /// <summary>Deletes an existing file.</summary>
  1019. /// <returns>
  1020. /// If the function succeeds, the return value is nonzero.
  1021. /// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
  1022. /// </returns>
  1023. /// <remarks>Minimum supported client: Windows XP</remarks>
  1024. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1025. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1026. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DeleteFileW")]
  1027. [return: MarshalAs(UnmanagedType.Bool)]
  1028. internal static extern bool DeleteFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
  1029. /// <summary>Deletes an existing file as a transacted operation.</summary>
  1030. /// <returns>
  1031. /// If the function succeeds, the return value is nonzero.
  1032. /// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
  1033. /// </returns>
  1034. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1035. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1036. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1037. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DeleteFileTransactedW")]
  1038. [return: MarshalAs(UnmanagedType.Bool)]
  1039. internal static extern bool DeleteFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, SafeHandle hTransaction);
  1040. #endregion // Delete
  1041. #region Links
  1042. /// <summary>Establishes a hard link between an existing file and a new file.
  1043. /// This function is only supported on the NTFS file system, and only for files, not directories.
  1044. /// </summary>
  1045. /// <returns>
  1046. /// If the function succeeds, the return value is nonzero.
  1047. /// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
  1048. /// </returns>
  1049. /// <remarks>Minimum supported client: Windows XP</remarks>
  1050. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1051. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1052. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateHardLinkW")]
  1053. [return: MarshalAs(UnmanagedType.Bool)]
  1054. internal static extern bool CreateHardLink([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, IntPtr lpSecurityAttributes);
  1055. /// <summary>Establishes a hard link between an existing file and a new file as a transacted operation.
  1056. /// This function is only supported on the NTFS file system, and only for files, not directories.
  1057. /// </summary>
  1058. /// <returns>
  1059. /// If the function succeeds, the return value is nonzero.
  1060. /// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
  1061. /// </returns>
  1062. /// <remarks>Minimum supported client: Windows XP</remarks>
  1063. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1064. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1065. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateHardLinkTransactedW")]
  1066. [return: MarshalAs(UnmanagedType.Bool)]
  1067. internal static extern bool CreateHardLinkTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, IntPtr lpSecurityAttributes, SafeHandle hTransaction);
  1068. /// <summary>Creates a symbolic link.</summary>
  1069. /// <returns>
  1070. /// If the function succeeds, the return value is nonzero.
  1071. /// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
  1072. /// </returns>
  1073. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1074. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1075. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1076. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateSymbolicLinkW")]
  1077. [return: MarshalAs(UnmanagedType.Bool)]
  1078. internal static extern bool CreateSymbolicLink([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.U4)] SymbolicLinkTarget dwFlags);
  1079. /// <summary>Creates a symbolic link as a transacted operation.</summary>
  1080. /// <returns>
  1081. /// If the function succeeds, the return value is nonzero.
  1082. /// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
  1083. /// </returns>
  1084. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1085. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1086. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1087. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateSymbolicLinkTransactedW")]
  1088. [return: MarshalAs(UnmanagedType.Bool)]
  1089. internal static extern bool CreateSymbolicLinkTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpSymlinkFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpTargetFileName, [MarshalAs(UnmanagedType.U4)] SymbolicLinkTarget dwFlags, SafeHandle hTransaction);
  1090. #endregion // Links
  1091. #region Find
  1092. /// <summary>Searches a directory for a file or subdirectory with a name and attributes that match those specified.</summary>
  1093. /// <returns>
  1094. /// If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose, and the lpFindFileData parameter contains information about the first file or directory found.
  1095. /// If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
  1096. /// </returns>
  1097. /// <remarks>A trailing backslash is not allowed and will be removed.</remarks>
  1098. /// <remarks>Minimum supported client: Windows XP</remarks>
  1099. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1100. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1101. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileExW")]
  1102. internal static extern SafeFindFileHandle FindFirstFileEx([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, FindExInfoLevels fInfoLevelId, ref Win32FindData lpFindFileData, FindExSearchOps fSearchOp, IntPtr lpSearchFilter, FindExAdditionalFlags dwAdditionalFlags);
  1103. /// <summary>Searches a directory for a file or subdirectory with a name that matches a specific name as a transacted operation.</summary>
  1104. /// <returns>
  1105. /// If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose, and the lpFindFileData parameter contains information about the first file or directory found.
  1106. /// If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
  1107. /// </returns>
  1108. /// <remarks>A trailing backslash is not allowed and will be removed.</remarks>
  1109. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1110. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1111. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1112. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileTransactedW")]
  1113. internal static extern SafeFindFileHandle FindFirstFileTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, FindExInfoLevels fInfoLevelId, ref Win32FindData lpFindFileData, FindExSearchOps fSearchOp, IntPtr lpSearchFilter, FindExAdditionalFlags dwAdditionalFlags, SafeHandle hTransaction);
  1114. /// <summary>Continues a file search from a previous call to the FindFirstFile, FindFirstFileEx, or FindFirstFileTransacted functions.</summary>
  1115. /// <returns>
  1116. /// If the function succeeds, the return value is nonzero and the lpFindFileData parameter contains information about the next file or directory found.
  1117. /// If the function fails, the return value is zero and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
  1118. /// If the function fails because no more matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.
  1119. /// </returns>
  1120. /// <remarks>Minimum supported client: Windows XP</remarks>
  1121. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1122. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1123. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextFileW")]
  1124. [return: MarshalAs(UnmanagedType.Bool)]
  1125. internal static extern bool FindNextFile(SafeFindFileHandle hFindFile, ref Win32FindData lpFindFileData);
  1126. /// <summary>Closes a file search handle opened by the FindFirstFile, FindFirstFileEx, FindFirstFileNameW, FindFirstFileNameTransactedW, FindFirstFileTransacted, FindFirstStreamTransactedW, or FindFirstStreamW functions.</summary>
  1127. /// <returns>
  1128. /// If the function succeeds, the return value is nonzero.
  1129. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1130. /// </returns>
  1131. /// <remarks>SetLastError is set to false.</remarks>
  1132. /// <remarks>Minimum supported client: Windows XP</remarks>
  1133. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1134. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1135. [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
  1136. [return: MarshalAs(UnmanagedType.Bool)]
  1137. internal static extern bool FindClose(IntPtr hFindFile);
  1138. /// <summary>Creates an enumeration of all the hard links to the specified file.
  1139. /// The FindFirstFileNameW function returns a handle to the enumeration that can be used on subsequent calls to the FindNextFileNameW function.
  1140. /// </summary>
  1141. /// <returns>
  1142. /// If the function succeeds, the return value is a search handle that can be used with the FindNextFileNameW function or closed with the FindClose function.
  1143. /// If the function fails, the return value is INVALID_HANDLE_VALUE (0xffffffff). To get extended error information, call the GetLastError function.
  1144. /// </returns>
  1145. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1146. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1147. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1148. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileNameW")]
  1149. internal static extern SafeFindFileHandle FindFirstFileName([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, uint dwFlags, [In, Out] ref uint stringLength, StringBuilder linkName);
  1150. /// <summary>Creates an enumeration of all the hard links to the specified file as a transacted operation. The function returns a handle to the enumeration that can be used on subsequent calls to the FindNextFileNameW function.</summary>
  1151. /// <returns>
  1152. /// If the function succeeds, the return value is a search handle that can be used with the FindNextFileNameW function or closed with the FindClose function.
  1153. /// If the function fails, the return value is INVALID_HANDLE_VALUE (0xffffffff). To get extended error information, call the GetLastError function.
  1154. /// </returns>
  1155. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1156. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1157. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1158. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstFileNameTransactedW")]
  1159. internal static extern SafeFindFileHandle FindFirstFileNameTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, int dwFlags, [In, Out] ref uint stringLength, StringBuilder linkName, SafeHandle hTransaction);
  1160. /// <summary>Continues enumerating the hard links to a file using the handle returned by a successful call to the FindFirstFileNameW function.</summary>
  1161. /// <returns>
  1162. /// If the function succeeds, the return value is nonzero.
  1163. /// If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
  1164. /// If no matching files can be found, the GetLastError function returns ERROR_HANDLE_EOF.
  1165. /// </returns>
  1166. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1167. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1168. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1169. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextFileNameW")]
  1170. [return: MarshalAs(UnmanagedType.Bool)]
  1171. internal static extern bool FindNextFileName(SafeHandle hFindStream, [In, Out] ref uint stringLength, [In, Out] StringBuilder linkName);
  1172. /// <summary>Determines whether a path to a file system object such as a file or folder is valid.</summary>
  1173. /// <returns>true if the file exists; otherwise, false. Call GetLastError for extended error information.</returns>
  1174. /// <remarks>
  1175. /// This function tests the validity of the path.
  1176. /// A path specified by Universal Naming Convention (UNC) is limited to a file only; that is, \\server\share\file is permitted.
  1177. /// A network share path to a server or server share is not permitted; that is, \\server or \\server\share.
  1178. /// This function returns FALSE if a mounted remote drive is out of service.
  1179. /// </remarks>
  1180. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  1181. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  1182. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1183. [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathFileExistsW")]
  1184. [return: MarshalAs(UnmanagedType.Bool)]
  1185. internal static extern bool PathFileExists([MarshalAs(UnmanagedType.LPWStr)] string pszPath);
  1186. #endregion // Find
  1187. /// <summary>Retrieves attributes for a specified file or directory.</summary>
  1188. /// <returns>
  1189. /// If the function succeeds, the return value is nonzero.
  1190. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1191. /// </returns>
  1192. /// <remarks>
  1193. /// The GetFileAttributes function retrieves file system attribute information. GetFileAttributesEx can obtain other sets of file or directory attribute information.
  1194. /// Currently, GetFileAttributesEx retrieves a set of standard attributes that is a superset of the file system attribute information.
  1195. /// When the GetFileAttributesEx function is called on a directory that is a mounted folder, it returns the attributes of the directory, not those of
  1196. /// the root directory in the volume that the mounted folder associates with the directory. To obtain the attributes of the associated volume,
  1197. /// call GetVolumeNameForVolumeMountPoint to obtain the name of the associated volume. Then use the resulting name in a call to GetFileAttributesEx.
  1198. /// The results are the attributes of the root directory on the associated volume.
  1199. /// Symbolic link behavior: If the path points to a symbolic link, the function returns attributes for the symbolic link.
  1200. /// </remarks>
  1201. /// <remarks>Minimum supported client: Windows XP</remarks>
  1202. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1203. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1204. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFileAttributesExW")]
  1205. [return: MarshalAs(UnmanagedType.Bool)]
  1206. internal static extern bool GetFileAttributesEx([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] uint fInfoLevelId, ref Win32FileAttributeData lpFileInformation);
  1207. /// <summary>Retrieves file system attributes for a specified file or directory as a transacted operation.</summary>
  1208. /// <returns>
  1209. /// If the function succeeds, the return value is nonzero.
  1210. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1211. /// </returns>
  1212. /// <remarks>
  1213. /// Transacted Operations
  1214. /// If a file is open for modification in a transaction, no other thread can open the file for modification until the transaction is committed.
  1215. /// Conversely, if a file is open for modification outside of a transaction, no transacted thread can open the file for modification until the
  1216. /// non-transacted handle is closed. If a non-transacted thread has a handle opened to modify a file, a call to GetFileAttributesTransacted for
  1217. /// that file will fail with an ERROR_TRANSACTIONAL_CONFLICT error.
  1218. /// </remarks>
  1219. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1220. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1221. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1222. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFileAttributesTransactedW")]
  1223. [return: MarshalAs(UnmanagedType.Bool)]
  1224. internal static extern bool GetFileAttributesTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] uint fInfoLevelId, ref Win32FileAttributeData lpFileInformation, SafeHandle hTransaction);
  1225. /// <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>
  1226. /// <returns>
  1227. /// If the function succeeds, the return value is nonzero.
  1228. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1229. /// </returns>
  1230. /// <remarks>Minimum supported client: Windows XP</remarks>
  1231. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1232. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1233. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "ReplaceFileW")]
  1234. [return: MarshalAs(UnmanagedType.Bool)]
  1235. 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);
  1236. /// <summary>Sets the attributes for a file or directory.</summary>
  1237. /// <returns>
  1238. /// If the function succeeds, the return value is nonzero.
  1239. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1240. /// </returns>
  1241. /// <remarks>Minimum supported client: Windows XP</remarks>
  1242. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1243. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1244. [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api", Justification = "2012-09-26: Yomodo; Managed version can't handle LongPaths.")]
  1245. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetFileAttributesW")]
  1246. [return: MarshalAs(UnmanagedType.Bool)]
  1247. internal static extern bool SetFileAttributes([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileAttributes dwFileAttributes);
  1248. /// <summary>Sets the attributes for a file or directory as a transacted operation.</summary>
  1249. /// <returns>
  1250. /// If the function succeeds, the return value is nonzero.
  1251. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1252. /// </returns>
  1253. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1254. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1255. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1256. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetFileAttributesTransactedW")]
  1257. [return: MarshalAs(UnmanagedType.Bool)]
  1258. internal static extern bool SetFileAttributesTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] FileAttributes dwFileAttributes, SafeHandle hTransaction);
  1259. /// <summary>Sets the date and time that the specified file or directory was created, last accessed, or last modified.</summary>
  1260. /// <returns>
  1261. /// If the function succeeds, the return value is nonzero.
  1262. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1263. /// </returns>
  1264. /// <remarks>Minimum supported client: Windows XP</remarks>
  1265. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1266. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1267. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1268. [return: MarshalAs(UnmanagedType.Bool)]
  1269. internal static extern bool SetFileTime(SafeFileHandle hFile, SafeHandle lpCreationTime, SafeHandle lpLastAccessTime, SafeHandle lpLastWriteTime);
  1270. /// <summary>Retrieves the size of the specified file.</summary>
  1271. /// <returns>
  1272. /// If the function succeeds, the return value is nonzero.
  1273. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1274. /// </returns>
  1275. /// <remarks>Minimum supported client: Windows XP</remarks>
  1276. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1277. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1278. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1279. [return: MarshalAs(UnmanagedType.Bool)]
  1280. internal static extern bool GetFileSizeEx(SafeHandle hFile, out long lpFileSize);
  1281. /// <summary>Retrieves the actual number of bytes of disk storage used to store a specified file.</summary>
  1282. /// <returns>
  1283. /// 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 specified file, and if lpFileSizeHigh is non-NULL, the function puts the high-order DWORD of that actual value into the DWORD pointed to by that parameter. This is the compressed file size for compressed files, the actual file size for noncompressed files.
  1284. /// If the function fails, and lpFileSizeHigh is NULL, the return value is INVALID_FILE_SIZE. To get extended error information, call GetLastError.
  1285. /// If the return value is INVALID_FILE_SIZE and lpFileSizeHigh is non-NULL, an application must call GetLastError to determine whether the function has succeeded (value is NO_ERROR) or failed (value is other than NO_ERROR).
  1286. /// </returns>
  1287. /// <remarks>Minimum supported client: Windows XP</remarks>
  1288. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1289. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1290. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetCompressedFileSizeW")]
  1291. [return: MarshalAs(UnmanagedType.U4)]
  1292. internal static extern uint GetCompressedFileSize([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);
  1293. /// <summary>Retrieves the actual number of bytes of disk storage used to store a specified file as a transacted operation.</summary>
  1294. /// <returns>
  1295. /// 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 specified file, and if lpFileSizeHigh is non-NULL, the function puts the high-order DWORD of that actual value into the DWORD pointed to by that parameter. This is the compressed file size for compressed files, the actual file size for noncompressed files.
  1296. /// If the function fails, and lpFileSizeHigh is NULL, the return value is INVALID_FILE_SIZE. To get extended error information, call GetLastError.
  1297. /// If the return value is INVALID_FILE_SIZE and lpFileSizeHigh is non-NULL, an application must call GetLastError to determine whether the function has succeeded (value is NO_ERROR) or failed (value is other than NO_ERROR).
  1298. /// </returns>
  1299. /// <remarks>Minimum supported client: Windows XP</remarks>
  1300. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1301. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1302. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetCompressedFileSizeTransactedW")]
  1303. [return: MarshalAs(UnmanagedType.U4)]
  1304. internal static extern uint GetCompressedFileSizeTransacted([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh, SafeHandle hTransaction);
  1305. /// <summary>Flushes the buffers of a specified file and causes all buffered data to be written to a file.</summary>
  1306. /// <returns>
  1307. /// If the function succeeds, the return value is nonzero.
  1308. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1309. /// </returns>
  1310. /// <remarks>Minimum supported client: Windows XP</remarks>
  1311. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1312. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1313. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1314. [return: MarshalAs(UnmanagedType.Bool)]
  1315. internal static extern bool FlushFileBuffers(SafeFileHandle hFile);
  1316. /// <summary>Locks the specified file for exclusive access by the calling process.</summary>
  1317. /// <returns>
  1318. /// If the function succeeds, the return value is nonzero (TRUE).
  1319. /// If the function fails, the return value is zero (FALSE). To get extended error information, call GetLastError.
  1320. /// </returns>
  1321. /// <remarks>Minimum supported client: Windows XP</remarks>
  1322. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1323. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1324. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1325. [return: MarshalAs(UnmanagedType.Bool)]
  1326. 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);
  1327. /// <summary>Unlocks a region in an open file. Unlocking a region enables other processes to access the region.</summary>
  1328. /// <returns>
  1329. /// If the function succeeds, the return value is nonzero.
  1330. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1331. /// </returns>
  1332. /// <remarks>Minimum supported client: Windows XP</remarks>
  1333. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1334. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1335. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1336. [return: MarshalAs(UnmanagedType.Bool)]
  1337. 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);
  1338. /// <summary>Retrieves file information for the specified file.</summary>
  1339. /// <returns>
  1340. /// If the function succeeds, the return value is nonzero and file information data is contained in the buffer pointed to by the lpFileInformation parameter.
  1341. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1342. /// </returns>
  1343. /// <remarks>
  1344. /// Depending on the underlying network features of the operating system and the type of server connected to,
  1345. /// the GetFileInformationByHandle function may fail, return partial information, or full information for the given file.
  1346. /// </remarks>
  1347. /// <remarks>Minimum supported client: Windows XP</remarks>
  1348. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1349. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1350. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1351. [return: MarshalAs(UnmanagedType.Bool)]
  1352. internal static extern bool GetFileInformationByHandle(SafeFileHandle hFile, ByHandleFileInfo lpFileInformation);
  1353. /// <summary>Retrieves file information for the specified file.</summary>
  1354. /// <returns>
  1355. /// If the function succeeds, the return value is nonzero and file information data is contained in the buffer pointed to by the lpFileInformation parameter.
  1356. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1357. /// </returns>
  1358. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1359. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1360. /// <remarks>Redistributable: Windows SDK on Windows Server 2003 and Windows XP.</remarks>
  1361. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1362. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1363. [return: MarshalAs(UnmanagedType.Bool)]
  1364. internal static extern bool GetFileInformationByHandleEx(SafeFileHandle hFile, FileInfoByHandleClass fileInformationClass, SafeHandle lpFileInformation, [MarshalAs(UnmanagedType.U4)] uint dwBufferSize);
  1365. #endregion // File Management
  1366. # region Compress/Decompress
  1367. /// <summary>Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.</summary>
  1368. /// <returns>
  1369. /// If the operation completes successfully, the return value is nonzero.
  1370. /// If the operation fails or is pending, the return value is zero. To get extended error information, call GetLastError.
  1371. /// </returns>
  1372. /// <remarks>Minimum supported client: Windows XP</remarks>
  1373. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1374. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1375. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1376. [return: MarshalAs(UnmanagedType.Bool)]
  1377. internal static extern bool DeviceIoControl(SafeFileHandle hDevice, IoControlCode dwIoControlCode, out short compressionFormatDefault, [MarshalAs(UnmanagedType.U4)] uint nInBufferSize, IntPtr lpOutBuffer, [MarshalAs(UnmanagedType.U4)] uint nOutBufferSize, [MarshalAs(UnmanagedType.U4)] out uint lpBytesReturned, IntPtr lpOverlapped);
  1378. #endregion // Compress/Decompress
  1379. #region Encrypt/Decryption
  1380. /// <summary>Encrypts a file or directory. All data streams in a file are encrypted. All new files created in an encrypted directory are encrypted.</summary>
  1381. /// <returns>
  1382. /// If the function succeeds, the return value is nonzero.
  1383. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1384. /// </returns>
  1385. /// <remarks>
  1386. /// The EncryptFile function requires exclusive access to the file being encrypted, and will fail if another process is using the file.
  1387. /// If the file is already encrypted, EncryptFile simply returns a nonzero value, which indicates success. If the file is compressed,
  1388. /// EncryptFile will decompress the file before encrypting it. If lpFileName specifies a read-only file, the function fails and GetLastError
  1389. /// returns ERROR_FILE_READ_ONLY. If lpFileName specifies a directory that contains a read-only file, the functions succeeds but the directory is not encrypted.
  1390. /// </remarks>
  1391. /// <remarks>Minimum supported client: Windows XP</remarks>
  1392. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1393. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1394. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EncryptFileW")]
  1395. [return: MarshalAs(UnmanagedType.Bool)]
  1396. internal static extern bool EncryptFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
  1397. /// <summary>Decrypts an encrypted file or directory.</summary>
  1398. /// <returns>
  1399. /// If the function succeeds, the return value is nonzero.
  1400. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1401. /// </returns>
  1402. /// <remarks>
  1403. /// The DecryptFile function requires exclusive access to the file being decrypted, and will fail if another process is using the file.
  1404. /// If the file is not encrypted, DecryptFile simply returns a nonzero value, which indicates success.
  1405. /// If lpFileName specifies a read-only file, the function fails and GetLastError returns ERROR_FILE_READ_ONLY.
  1406. /// If lpFileName specifies a directory that contains a read-only file, the functions succeeds but the directory is not decrypted.
  1407. /// </remarks>
  1408. /// <remarks>Minimum supported client: Windows XP</remarks>
  1409. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1410. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1411. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DecryptFileW")]
  1412. [return: MarshalAs(UnmanagedType.Bool)]
  1413. internal static extern bool DecryptFile([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, uint dwReserved);
  1414. /// <summary>Disables or enables encryption of the specified directory and the files in it.
  1415. /// It does not affect encryption of subdirectories below the indicated directory.
  1416. /// </summary>
  1417. /// <returns>
  1418. /// If the function succeeds, the return value is nonzero.
  1419. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1420. /// </returns>
  1421. /// <remarks>
  1422. /// EncryptionDisable disables encryption of directories and files.
  1423. /// It does not affect the visibility of files with the FILE_ATTRIBUTE_SYSTEM attribute set.
  1424. /// This method will create/change the file "Desktop.ini" and wil set Encryption value: "Disable=0|1"
  1425. /// </remarks>
  1426. /// <remarks>Minimum supported client: Windows XP</remarks>
  1427. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1428. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1429. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1430. [return: MarshalAs(UnmanagedType.Bool)]
  1431. internal static extern bool EncryptionDisable([MarshalAs(UnmanagedType.LPWStr)] string dirPath, [MarshalAs(UnmanagedType.Bool)] bool disable);
  1432. /// <summary>Retrieves the encryption status of the specified file.</summary>
  1433. /// <returns>
  1434. /// If the function succeeds, the return value is nonzero.
  1435. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1436. /// </returns>
  1437. /// <remarks>Minimum supported client: Windows XP</remarks>
  1438. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1439. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1440. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FileEncryptionStatusW")]
  1441. [return: MarshalAs(UnmanagedType.Bool)]
  1442. internal static extern bool FileEncryptionStatus([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, out FileEncryptionStatus lpStatus);
  1443. #endregion // Encrypt/Decryption
  1444. #region Security
  1445. #region Get/SetFileSecurity
  1446. // 2012-10-15: Yomodo; Moved to Security\NativeMethods.cs since more Security-related stuff is in there.
  1447. ///// <summary>
  1448. ///// The GetFileSecurity function obtains specified information about the security of a file or directory.
  1449. ///// The information obtained is constrained by the caller's access rights and privileges.
  1450. ///// </summary>
  1451. ///// <returns>
  1452. ///// If the function succeeds, the return value is nonzero.
  1453. ///// If the function fails, it returns zero. To get extended error information, call GetLastError.
  1454. ///// </returns>
  1455. ///// <remarks>Minimum supported client: Windows XP</remarks>
  1456. ///// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1457. //[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetFileSecurityW")]
  1458. //[return: MarshalAs(UnmanagedType.Bool)]
  1459. //internal static extern bool GetFileSecurity([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, uint requestedInformation, SafeHandle pSecurityDescriptor, uint nLength, out uint lpnLengthNeeded);
  1460. ///// <summary>
  1461. ///// The SetFileSecurity function sets the security of a file or directory object.
  1462. ///// </summary>
  1463. ///// <returns>
  1464. ///// If the function succeeds, the return value is nonzero.
  1465. ///// If the function fails, it returns zero. To get extended error information, call GetLastError.
  1466. ///// </returns>
  1467. ///// <remarks>Minimum supported client: Windows XP</remarks>
  1468. ///// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1469. ///// <remarks>This function is obsolete. Use the SetNamedSecurityInfo function instead.</remarks>
  1470. //[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetFileSecurityW")]
  1471. //[return: MarshalAs(UnmanagedType.Bool)]
  1472. //internal static extern bool SetFileSecurity([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, Security.NativeMethods.SECURITY_INFORMATION securityInformation, SafeHandle pSecurityDescriptor);
  1473. #endregion // Get/SetFileSecurity
  1474. #region Get/SetNamedSecurityInfo
  1475. ///// <summary>
  1476. ///// The GetNamedSecurityInfo function retrieves a copy of the security descriptor for an object specified by name.
  1477. ///// </summary>
  1478. ///// <returns>
  1479. ///// If the function succeeds, the return value is ERROR_SUCCESS.
  1480. ///// If the function fails, the return value is a nonzero error code defined in WinError.h.
  1481. ///// </returns>
  1482. ///// <remarks>Minimum supported client: Windows XP</remarks>
  1483. ///// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1484. //[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetNamedSecurityInfoW")]
  1485. //[return: MarshalAs(UnmanagedType.Bool)]
  1486. //internal static extern bool GetNamedSecurityInfo([MarshalAs(UnmanagedType.LPWStr)] string pObjectName, Security.NativeMethods.SE_OBJECT_TYPE objectType, Security.NativeMethods.SECURITY_INFORMATION securityInfo, out IntPtr ppsidOwner, out IntPtr ppsidGroup, out IntPtr ppDacl, out IntPtr ppSacl, out SafeLocalMemoryBufferHandle ppSecurityDescriptor);
  1487. ///// <summary>
  1488. ///// The SetNamedSecurityInfo function sets specified security information in the security descriptor of a specified object. The caller identifies the object by name.
  1489. ///// </summary>
  1490. ///// <returns>
  1491. ///// If the function succeeds, the function returns ERROR_SUCCESS.
  1492. ///// If the function fails, it returns a nonzero error code defined in WinError.h.
  1493. ///// </returns>
  1494. ///// <remarks>Minimum supported client: Windows XP</remarks>
  1495. ///// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1496. //[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetNamedSecurityInfoW")]
  1497. //[return: MarshalAs(UnmanagedType.Bool)]
  1498. //internal static extern bool SetNamedSecurityInfo([MarshalAs(UnmanagedType.LPWStr)] string pObjectName, Security.NativeMethods.SE_OBJECT_TYPE objectType, Security.NativeMethods.SECURITY_INFORMATION securityInfo, out IntPtr psidOwner, out IntPtr psidGroup, out IntPtr pDacl, out IntPtr pSacl);
  1499. #endregion Get/SetNamedSecurityInfo
  1500. #endregion // Security
  1501. #region Backup
  1502. /// <summary>The BackupRead function can be used to back up a file or directory, including the security information.
  1503. /// The function reads data associated with a specified file or directory into a buffer, which can then be written to the backup medium using the WriteFile function.</summary>
  1504. /// <returns>
  1505. /// If the function succeeds, the return value is nonzero.
  1506. /// If the function fails, the return value is zero, indicating that an I/O error occurred. To get extended error information, call GetLastError.
  1507. /// </returns>
  1508. /// <remarks>This function is not intended for use in backing up files encrypted under the Encrypted File System. Use ReadEncryptedFileRaw for that purpose.</remarks>
  1509. /// <remarks>Minimum supported client: Windows XP</remarks>
  1510. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1511. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1512. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1513. [return: MarshalAs(UnmanagedType.Bool)]
  1514. internal static extern bool BackupRead(SafeFileHandle hFile, SafeGlobalMemoryBufferHandle lpBuffer, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToRead, [MarshalAs(UnmanagedType.U4)] out uint lpNumberOfBytesRead, [MarshalAs(UnmanagedType.Bool)] bool bAbort, [MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity, out IntPtr lpContext);
  1515. /// <summary>The BackupWrite function can be used to restore a file or directory that was backed up using BackupRead.
  1516. /// Use the ReadFile function to get a stream of data from the backup medium, then use BackupWrite to write the data to the specified file or directory.</summary>
  1517. /// <returns>
  1518. /// If the function succeeds, the return value is nonzero.
  1519. /// If the function fails, the return value is zero, indicating that an I/O error occurred. To get extended error information, call GetLastError.
  1520. /// </returns>
  1521. /// <remarks>This function is not intended for use in restoring files encrypted under the Encrypted File System. Use WriteEncryptedFileRaw for that purpose.</remarks>
  1522. /// <remarks>Minimum supported client: Windows XP</remarks>
  1523. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1524. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1525. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1526. [return: MarshalAs(UnmanagedType.Bool)]
  1527. internal static extern bool BackupWrite(SafeFileHandle hFile, SafeGlobalMemoryBufferHandle lpBuffer, [MarshalAs(UnmanagedType.U4)] uint nNumberOfBytesToWrite, [MarshalAs(UnmanagedType.U4)] out uint lpNumberOfBytesWritten, [MarshalAs(UnmanagedType.Bool)] bool bAbort, [MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity, out IntPtr lpContext);
  1528. /// <summary>The BackupSeek function seeks forward in a data stream initially accessed by using the BackupRead or BackupWrite function.</summary>
  1529. /// <returns>
  1530. /// If the function could seek the requested amount, the function returns a nonzero value.
  1531. /// If the function could not seek the requested amount, the function returns zero. To get extended error information, call GetLastError.
  1532. /// </returns>
  1533. /// <remarks>
  1534. /// Applications use the BackupSeek function to skip portions of a data stream that cause errors.
  1535. /// This function does not seek across stream headers. For example, this function cannot be used to skip the stream name.
  1536. /// If an application attempts to seek past the end of a substream, the function fails, the lpdwLowByteSeeked and lpdwHighByteSeeked parameters
  1537. /// indicate the actual number of bytes the function seeks, and the file position is placed at the start of the next stream header.
  1538. /// </remarks>
  1539. /// <remarks>Minimum supported client: Windows XP</remarks>
  1540. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1541. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1542. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1543. [return: MarshalAs(UnmanagedType.Bool)]
  1544. internal static extern bool BackupSeek(SafeFileHandle hFile, [MarshalAs(UnmanagedType.U4)] uint dwLowBytesToSeek, [MarshalAs(UnmanagedType.U4)] uint dwHighBytesToSeek, [MarshalAs(UnmanagedType.U4)] out uint lpdwLowBytesSeeked, [MarshalAs(UnmanagedType.U4)] out uint lpdwHighBytesSeeked, out IntPtr lpContext);
  1545. #endregion // Backup
  1546. #region Kernel Transaction Manager
  1547. #region CommitTransaction
  1548. /// <summary>Requests that the specified transaction be committed.</summary>
  1549. /// <returns>
  1550. /// If the function succeeds, the return value is nonzero.
  1551. /// If the function fails, the return value is 0 (zero). To get extended error information, call the GetLastError function.
  1552. /// </returns>
  1553. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1554. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1555. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1556. [DllImport("ktmw32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1557. [return: MarshalAs(UnmanagedType.Bool)]
  1558. internal static extern bool CommitTransaction(SafeHandle hTrans);
  1559. #endregion // CommitTransaction
  1560. #region CreateTransaction
  1561. /// <summary>Creates a new transaction object.</summary>
  1562. /// <returns>
  1563. /// If the function succeeds, the return value is a handle to the transaction.
  1564. /// If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call the GetLastError function.
  1565. /// </returns>
  1566. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1567. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1568. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1569. [DllImport("ktmw32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1570. internal static extern SafeKernelTransactionHandle CreateTransaction(SecurityAttributes lpTransactionAttributes, IntPtr uow, uint createOptions, uint isolationLevel, uint isolationFlags, uint timeout, string description);
  1571. #endregion // CreateTransaction
  1572. #region RollbackTransaction
  1573. /// <summary>Requests that the specified transaction be rolled back. This function is synchronous.</summary>
  1574. /// <returns>
  1575. /// If the function succeeds, the return value is nonzero.
  1576. /// If the function fails, the return value is zero. To get extended error information, call the GetLastError function.
  1577. /// </returns>
  1578. /// <remarks>Minimum supported client: Windows Vista</remarks>
  1579. /// <remarks>Minimum supported server: Windows Server 2008</remarks>
  1580. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1581. [DllImport("ktmw32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1582. [return: MarshalAs(UnmanagedType.Bool)]
  1583. internal static extern bool RollbackTransaction(SafeHandle hTrans);
  1584. #endregion // RollbackTransaction
  1585. #endregion // Kernel Transaction Manager
  1586. #region Handles and Objects
  1587. #region CloseHandle
  1588. /// <summary>Closes an open object handle.</summary>
  1589. /// <returns>
  1590. /// If the function succeeds, the return value is nonzero.
  1591. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1592. /// </returns>
  1593. /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
  1594. /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
  1595. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1596. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1597. [return: MarshalAs(UnmanagedType.Bool)]
  1598. internal static extern bool CloseHandle(IntPtr hObject);
  1599. #endregion // CloseHandle
  1600. #endregion // Handles and Objects
  1601. #region Types
  1602. /// <summary>The Win32StreamId structure contains stream data.</summary>
  1603. [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Unicode)]
  1604. internal struct Win32StreamId
  1605. {
  1606. /// <summary>Type of data.
  1607. /// This member can be one of the following values:
  1608. /// BACKUP_DATA 0x00000001 Standard data. This corresponds to the NTFS $DATA stream type on the default (unnamed) data stream.
  1609. /// BACKUP_EA_DATA 0x00000002 Extended attribute data. This corresponds to the NTFS $EA stream type.
  1610. /// BACKUP_SECURITY_DATA 0x00000003 Security descriptor data.
  1611. /// BACKUP_ALTERNATE_DATA 0x00000004 Alternative data streams. This corresponds to the NTFS $DATA stream type on a named data stream.
  1612. /// BACKUP_LINK 0x00000005 Hard link information. This corresponds to the NTFS $FILE_NAME stream type.
  1613. /// BACKUP_PROPERTY_DATA 0x00000006 Property data.
  1614. /// BACKUP_OBJECT_ID 0x00000007 Objects identifiers. This corresponds to the NTFS $OBJECT_ID stream type.
  1615. /// BACKUP_REPARSE_DATA 0x00000008 Reparse points. This corresponds to the NTFS $REPARSE_POINT stream type.
  1616. /// BACKUP_SPARSE_BLOCK 0x00000009 Sparse file. This corresponds to the NTFS $DATA stream type for a sparse file.
  1617. /// BACKUP_TXFS_DATA 0x0000000A Transactional NTFS (TxF) data stream. This corresponds to the NTFS $TXF_DATA stream type.
  1618. /// Windows Server 2003 and Windows XP: This value is not supported.
  1619. /// </summary>
  1620. public readonly uint StreamId;
  1621. /// <summary>Attributes of data to facilitate cross-operating system transfer.
  1622. /// This member can be one or more of the following values:
  1623. /// STREAM_MODIFIED_WHEN_READ 0x00000001 Attribute set if the stream contains data that is modified when read. Allows the backup application to know that verification of data will fail.
  1624. /// STREAM_CONTAINS_SECURITY 0x00000002 Stream contains security data (general attributes). Allows the stream to be ignored on cross-operations restore.
  1625. /// </summary>
  1626. public readonly BackupStreamAttributes StreamAttributes;
  1627. /// <summary>Size of data, in bytes.</summary>
  1628. public readonly ulong Size;
  1629. /// <summary>Length of the name of the alternative data stream, in bytes.</summary>
  1630. public readonly uint StreamNameSize;
  1631. }
  1632. /// <summary>Class used to represent the SECURITY_ATTRIBUES native win32 structure. It provides initialization function from an <see cref="ObjectSecurity"/> object.</summary>
  1633. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  1634. internal sealed class SecurityAttributes
  1635. {
  1636. /// <summary>Initializes the SecurityAttributes structure from an instance of <see cref="ObjectSecurity"/>.</summary>
  1637. /// <param name="memoryHandle">A handle that will refer to the memory allocated by this object for storage of the
  1638. /// security descriptor. As long as this object is used, the memory handle should be kept alive, and afterwards it
  1639. /// should be disposed as soon as possible.</param>
  1640. /// <param name="securityDescriptor">The <see cref="ObjectSecurity"/> security descriptor to assign to this object. This parameter may be <see langword="null"/>.</param>
  1641. public void Initialize(out SafeGlobalMemoryBufferHandle memoryHandle, ObjectSecurity securityDescriptor)
  1642. {
  1643. nLength = (uint)Marshal.SizeOf(this);
  1644. if (securityDescriptor == null)
  1645. memoryHandle = new SafeGlobalMemoryBufferHandle();
  1646. else
  1647. {
  1648. byte[] src = securityDescriptor.GetSecurityDescriptorBinaryForm();
  1649. memoryHandle = new SafeGlobalMemoryBufferHandle(src.Length);
  1650. memoryHandle.CopyFrom(src, 0, src.Length);
  1651. }
  1652. bInheritHandle = 0;
  1653. }
  1654. public uint nLength;
  1655. public IntPtr lpSecurityDescriptor;
  1656. public int bInheritHandle;
  1657. }
  1658. #region FINDEX Constants
  1659. /// <summary>Defines values that are used with the FindFirstFileEx function to specify the information level of the returned data.</summary>
  1660. internal enum FindExInfoLevels
  1661. {
  1662. /// <summary>The FindFirstFileEx function retrieves a standard set of attribute information.</summary>
  1663. /// <remarks>The data is returned in a <see cref="Win32FindData"/> structure.</remarks>
  1664. InfoStandard,
  1665. /// <summary>The FindFirstFileEx function does not query the short file name, improving overall enumeration speed.</summary>
  1666. /// <remarks>The data is returned in a <see cref="Win32FindData"/> structure, and cAlternateFileName member is always a NULL string.</remarks>
  1667. /// <remarks>This value is not supported until Windows Server 2008 R2 and Windows 7.</remarks>
  1668. InfoBasic,
  1669. /// <summary>This value is used for validation. Supported values are less than this value.</summary>
  1670. InfoMaxLevel
  1671. }
  1672. /// <summary>Defines values that are used with the FindFirstFileEx function to specify the type of filtering to perform.</summary>
  1673. internal enum FindExSearchOps
  1674. {
  1675. /// <summary>The search for a file that matches a specified file name.</summary>
  1676. SearchNameMatch,
  1677. /// <summary>This is an advisory flag. If the file system supports directory filtering, the function searches for a file that matches
  1678. /// the specified name and is also a directory. If the file system does not support directory filtering, this flag is silently ignored.
  1679. /// </summary>
  1680. /// <remarks>
  1681. /// The lpSearchFilter parameter of the FindFirstFileEx function must be NULL when this search value is used.
  1682. /// If directory filtering is desired, this flag can be used on all file systems, but because it is an advisory flag and only affects file systems that support it,
  1683. /// the application must examine the file attribute data stored in the lpFindFileData parameter of the FindFirstFileEx function to determine whether the function has returned a handle to a directory.
  1684. /// </remarks>
  1685. SearchLimitToDirectories,
  1686. /// <summary>This filtering type is not available.</summary>
  1687. SearchLimitToDevices
  1688. }
  1689. /// <summary>Additional flags that control the search.</summary>
  1690. [Flags]
  1691. internal enum FindExAdditionalFlags
  1692. {
  1693. /// <summary>No additional flags used.</summary>
  1694. None = 0,
  1695. /// <summary>Searches are case-sensitive.</summary>
  1696. CaseSensitive = 1,
  1697. /// <summary>Uses a larger buffer for directory queries, which can increase performance of the find operation.</summary>
  1698. /// <remarks>This value is not supported until Windows Server 2008 R2 and Windows 7.</remarks>
  1699. LargeFetch = 2
  1700. }
  1701. #endregion // FINDEX Constants
  1702. #region FileIdBothDirInfo
  1703. /// <summary>Contains information about files in the specified directory. Used for directory handles. Use only when calling GetFileInformationByHandleEx.</summary>
  1704. /// <remarks>
  1705. /// The number of files that are returned for each call to GetFileInformationByHandleEx depends on the size of the buffer that is passed to the function.
  1706. /// Any subsequent calls to GetFileInformationByHandleEx on the same handle will resume the enumeration operation after the last file is returned.
  1707. /// </remarks>
  1708. [StructLayout(LayoutKind.Sequential)]
  1709. internal struct FileIdBothDirInfo
  1710. {
  1711. /// <summary>The offset for the next FILE_ID_BOTH_DIR_INFO structure that is returned. Contains zero (0) if no other entries follow this one.</summary>
  1712. public readonly uint NextEntryOffset;
  1713. /// <summary>The byte offset of the file within the parent directory. This member is undefined for file systems, such as NTFS,
  1714. /// in which the position of a file within the parent directory is not fixed and can be changed at any time to maintain sort order.
  1715. /// </summary>
  1716. public readonly uint FileIndex;
  1717. /// <summary>The time that the file was created.</summary>
  1718. public FileTime CreationTime;
  1719. /// <summary>The time that the file was last accessed.</summary>
  1720. public FileTime LastAccessTime;
  1721. /// <summary>The time that the file was last written to.</summary>
  1722. public FileTime LastWriteTime;
  1723. /// <summary>The time that the file was last changed.</summary>
  1724. public FileTime ChangeTime;
  1725. /// <summary>The absolute new end-of-file position as a byte offset from the start of the file to the end of the file.
  1726. /// Because this value is zero-based, it actually refers to the first free byte in the file.
  1727. /// In other words, EndOfFile is the offset to the byte that immediately follows the last valid byte in the file.
  1728. /// </summary>
  1729. public readonly long EndOfFile;
  1730. /// <summary>The number of bytes that are allocated for the file. This value is usually a multiple of the sector or cluster size of the underlying physical device.</summary>
  1731. public readonly long AllocationSize;
  1732. /// <summary>The file attributes.</summary>
  1733. public readonly FileAttributes FileAttributes;
  1734. /// <summary>The length of the file name.</summary>
  1735. public readonly uint FileNameLength;
  1736. /// <summary>The size of the extended attributes for the file.</summary>
  1737. public readonly uint EaSize;
  1738. /// <summary>The length of ShortName.</summary>
  1739. public readonly byte ShortNameLength;
  1740. /// <summary>The short 8.3 file naming convention (for example, "FILENAME.TXT") name of the file.</summary>
  1741. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.U2)]
  1742. public readonly char[] ShortName;
  1743. /// <summary>The file ID.</summary>
  1744. public readonly long FileId;
  1745. /// <summary>The first character of the file name string. This is followed in memory by the remainder of the string.</summary>
  1746. public IntPtr FileName;
  1747. }
  1748. #endregion // FileIdBothDirInfo
  1749. #endregion
  1750. #region Device Management
  1751. /// <summary>Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.</summary>
  1752. /// <returns>
  1753. /// If the function succeeds, the return value is nonzero.
  1754. /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
  1755. /// </returns>
  1756. /// <remarks>Minimum supported client: Windows XP</remarks>
  1757. /// <remarks>Minimum supported server: Windows Server 2003</remarks>
  1758. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1759. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1760. [return: MarshalAs(UnmanagedType.Bool)]
  1761. internal static extern bool DeviceIoControl(SafeHandle hDevice, IoControlCode dwIoControlCode, SafeHandle lpInBuffer, uint nInBufferSize, SafeHandle lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);
  1762. /// <summary>The SetupDiDestroyDeviceInfoList function deletes a device information set and frees all associated memory.</summary>
  1763. /// <returns>
  1764. /// The function returns true if it is successful.
  1765. /// Otherwise, it returns false and the logged error can be retrieved with a call to GetLastError.
  1766. /// </returns>
  1767. /// <remarks>Available in Microsoft Windows 2000 and later versions of Windows.</remarks>
  1768. [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
  1769. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  1770. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  1771. [return: MarshalAs(UnmanagedType.Bool)]
  1772. internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr hDevice);
  1773. #endregion // Device Management
  1774. }
  1775. }