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

1017 lines
74 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using Microsoft.Win32.SafeHandles;
  22. using System.IO;
  23. using System.Security;
  24. using System.Security.AccessControl;
  25. using FileStream = System.IO.FileStream;
  26. using System.Diagnostics.CodeAnalysis;
  27. namespace Alphaleonis.Win32.Filesystem
  28. {
  29. partial class File
  30. {
  31. #region Using FileAccess
  32. /// <summary>Opens a <see cref="FileStream"/> on the specified path with read/write access.</summary>
  33. /// <param name="path">The file to open.</param>
  34. /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
  35. /// <returns>A <see cref="FileStream"/> opened in the specified mode and path, with read/write access and not shared.</returns>
  36. [SecurityCritical]
  37. public static FileStream Open(string path, FileMode mode)
  38. {
  39. return OpenCore(null, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath);
  40. }
  41. /// <summary>Opens a <see cref="FileStream"/> on the specified path, with the specified mode and access.</summary>
  42. /// <param name="path">The file to open.</param>
  43. /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
  44. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  45. /// <returns>An unshared <see cref="FileStream"/> that provides access to the specified file, with the specified mode and access.</returns>
  46. [SecurityCritical]
  47. public static FileStream Open(string path, FileMode mode, FileAccess access)
  48. {
  49. return OpenCore(null, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath);
  50. }
  51. /// <summary>Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</summary>
  52. /// <param name="path">The file to open.</param>
  53. /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
  54. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  55. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  56. /// <returns>A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</returns>
  57. [SecurityCritical]
  58. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
  59. {
  60. return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath);
  61. }
  62. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path with read/write access.</summary>
  63. /// <param name="path">The file to open.</param>
  64. /// <param name="mode">
  65. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  66. /// of existing files are retained or overwritten.
  67. /// </param>
  68. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  69. /// <returns>A <see cref="FileStream"/> opened in the specified mode and path, with read/write access and not shared.</returns>
  70. [SecurityCritical]
  71. public static FileStream Open(string path, FileMode mode, PathFormat pathFormat)
  72. {
  73. return OpenCore(null, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat);
  74. }
  75. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path, with the specified mode and access.</summary>
  76. /// <param name="path">The file to open.</param>
  77. /// <param name="mode">
  78. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  79. /// of existing files are retained or overwritten.
  80. /// </param>
  81. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  82. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  83. /// <returns>
  84. /// An unshared <see cref="FileStream"/> that provides access to the specified file, with the specified mode and access.
  85. /// </returns>
  86. [SecurityCritical]
  87. public static FileStream Open(string path, FileMode mode, FileAccess access, PathFormat pathFormat)
  88. {
  89. return OpenCore(null, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat);
  90. }
  91. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</summary>
  92. /// <param name="path">The file to open.</param>
  93. /// <param name="mode">
  94. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  95. /// of existing files are retained or overwritten.
  96. /// </param>
  97. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  98. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  99. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  100. /// <returns>
  101. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the
  102. /// specified sharing option.
  103. /// </returns>
  104. [SecurityCritical]
  105. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, PathFormat pathFormat)
  106. {
  107. return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, pathFormat);
  108. }
  109. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</summary>
  110. /// <param name="path">The file to open.</param>
  111. /// <param name="mode">
  112. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  113. /// of existing files are retained or overwritten.
  114. /// </param>
  115. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  116. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  117. /// <param name="extendedAttributes">The extended attributes.</param>
  118. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  119. /// <returns>
  120. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the
  121. /// specified sharing option.
  122. /// </returns>
  123. [SecurityCritical]
  124. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
  125. {
  126. return OpenCore(null, path, mode, access, share, extendedAttributes, null, null, pathFormat);
  127. }
  128. // New below
  129. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  130. /// <param name="path">The file to open.</param>
  131. /// <param name="mode">A constant that determines how to open or create the file.</param>
  132. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  133. /// file.</param>
  134. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  135. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The default buffer size is 4096. </param>
  136. /// <returns>
  137. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  138. /// access and the specified sharing option.
  139. /// </returns>
  140. [SecurityCritical]
  141. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
  142. {
  143. return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, PathFormat.RelativePath);
  144. }
  145. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  146. /// <param name="path">The file to open.</param>
  147. /// <param name="mode">A constant that determines how to open or create the file.</param>
  148. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  149. /// file.</param>
  150. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  151. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  152. /// default buffer size is 4096.</param>
  153. /// <param name="useAsync">Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the
  154. /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be
  155. /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods
  156. /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the
  157. /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using
  158. /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without
  159. /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10.</param>
  160. /// <returns>
  161. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  162. /// access and the specified sharing option.
  163. /// </returns>
  164. [SecurityCritical]
  165. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync)
  166. {
  167. return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, PathFormat.RelativePath);
  168. }
  169. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  170. /// <param name="path">The file to open.</param>
  171. /// <param name="mode">A constant that determines how to open or create the file.</param>
  172. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  173. /// file.</param>
  174. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  175. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  176. /// default buffer size is 4096.</param>
  177. /// <param name="options">A value that specifies additional file options.</param>
  178. /// <returns>
  179. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  180. /// access and the specified sharing option.
  181. /// </returns>
  182. [SecurityCritical]
  183. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
  184. {
  185. return OpenCore(null, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, PathFormat.RelativePath);
  186. }
  187. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  188. /// <param name="path">The file to open.</param>
  189. /// <param name="mode">A constant that determines how to open or create the file.</param>
  190. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  191. /// file.</param>
  192. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  193. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  194. /// default buffer size is 4096.</param>
  195. /// <param name="extendedAttributes">The extended attributes specifying additional options.</param>
  196. /// <returns>
  197. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  198. /// access and the specified sharing option.
  199. /// </returns>
  200. [SecurityCritical]
  201. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes)
  202. {
  203. return OpenCore(null, path, mode, access, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath);
  204. }
  205. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  206. /// <param name="path">The file to open.</param>
  207. /// <param name="mode">A constant that determines how to open or create the file.</param>
  208. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  209. /// file.</param>
  210. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  211. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  212. /// default buffer size is 4096.</param>
  213. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  214. /// <returns>
  215. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  216. /// access and the specified sharing option.
  217. /// </returns>
  218. [SecurityCritical]
  219. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, PathFormat pathFormat)
  220. {
  221. return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, pathFormat);
  222. }
  223. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  224. /// <param name="path">The file to open.</param>
  225. /// <param name="mode">A constant that determines how to open or create the file.</param>
  226. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  227. /// file.</param>
  228. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  229. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  230. /// default buffer size is 4096.</param>
  231. /// <param name="useAsync">Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the
  232. /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be
  233. /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods
  234. /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the
  235. /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using
  236. /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without
  237. /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10.</param>
  238. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  239. /// <returns>
  240. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  241. /// access and the specified sharing option.
  242. /// </returns>
  243. [SecurityCritical]
  244. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync, PathFormat pathFormat)
  245. {
  246. return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, pathFormat);
  247. }
  248. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  249. /// <param name="path">The file to open.</param>
  250. /// <param name="mode">A constant that determines how to open or create the file.</param>
  251. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  252. /// file.</param>
  253. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  254. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  255. /// default buffer size is 4096.</param>
  256. /// <param name="options">A value that specifies additional file options.</param>
  257. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  258. /// <returns>
  259. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  260. /// access and the specified sharing option.
  261. /// </returns>
  262. [SecurityCritical]
  263. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat)
  264. {
  265. return OpenCore(null, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat);
  266. }
  267. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  268. /// <param name="path">The file to open.</param>
  269. /// <param name="mode">A constant that determines how to open or create the file.</param>
  270. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  271. /// file.</param>
  272. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  273. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  274. /// default buffer size is 4096.</param>
  275. /// <param name="extendedAttributes">The extended attributes specifying additional options.</param>
  276. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  277. /// <returns>
  278. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  279. /// access and the specified sharing option.
  280. /// </returns>
  281. [SecurityCritical]
  282. public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
  283. {
  284. return OpenCore(null, path, mode, access, share, extendedAttributes, bufferSize, null, pathFormat);
  285. }
  286. #endregion // Using FileAccess
  287. #region Using FileSystemRights
  288. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  289. /// <param name="path">The file to open.</param>
  290. /// <param name="mode">A constant that determines how to open or create the file.</param>
  291. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  292. /// file.</param>
  293. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  294. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  295. /// default buffer size is 4096.</param>
  296. /// <param name="options">A value that specifies additional file options.</param>
  297. /// <returns>
  298. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  299. /// access and the specified sharing option.
  300. /// </returns>
  301. [SecurityCritical]
  302. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options)
  303. {
  304. return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes)options, bufferSize, null, PathFormat.RelativePath);
  305. }
  306. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  307. /// <param name="path">The file to open.</param>
  308. /// <param name="mode">A constant that determines how to open or create the file.</param>
  309. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  310. /// file.</param>
  311. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  312. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  313. /// default buffer size is 4096.</param>
  314. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  315. /// <returns>
  316. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  317. /// access and the specified sharing option.
  318. /// </returns>
  319. [SecurityCritical]
  320. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes)
  321. {
  322. return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath);
  323. }
  324. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  325. /// <param name="path">The file to open.</param>
  326. /// <param name="mode">A constant that determines how to open or create the file.</param>
  327. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  328. /// file.</param>
  329. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  330. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  331. /// default buffer size is 4096.</param>
  332. /// <param name="options">A value that specifies additional file options.</param>
  333. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  334. /// <returns>
  335. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  336. /// access and the specified sharing option.
  337. /// </returns>
  338. [SecurityCritical]
  339. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security)
  340. {
  341. return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes)options, bufferSize, security, PathFormat.RelativePath);
  342. }
  343. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  344. /// <param name="path">The file to open.</param>
  345. /// <param name="mode">A constant that determines how to open or create the file.</param>
  346. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  347. /// file.</param>
  348. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  349. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  350. /// default buffer size is 4096.</param>
  351. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  352. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  353. /// <returns>
  354. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  355. /// access and the specified sharing option.
  356. /// </returns>
  357. [SecurityCritical]
  358. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security)
  359. {
  360. return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, security, PathFormat.RelativePath);
  361. }
  362. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  363. /// <param name="path">The file to open.</param>
  364. /// <param name="mode">A constant that determines how to open or create the file.</param>
  365. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  366. /// file.</param>
  367. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  368. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  369. /// default buffer size is 4096.</param>
  370. /// <param name="options">A value that specifies additional file options.</param>
  371. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  372. /// <returns>
  373. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  374. /// access and the specified sharing option.
  375. /// </returns>
  376. [SecurityCritical]
  377. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat)
  378. {
  379. return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat);
  380. }
  381. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  382. /// <param name="path">The file to open.</param>
  383. /// <param name="mode">A constant that determines how to open or create the file.</param>
  384. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  385. /// file.</param>
  386. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  387. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  388. /// default buffer size is 4096.</param>
  389. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  390. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  391. /// <returns>
  392. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  393. /// access and the specified sharing option.
  394. /// </returns>
  395. [SecurityCritical]
  396. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
  397. {
  398. return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, null, pathFormat);
  399. }
  400. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.</summary>
  401. /// <param name="path">The file to open.</param>
  402. /// <param name="mode">A constant that determines how to open or create the file.</param>
  403. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  404. /// file.</param>
  405. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  406. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  407. /// default buffer size is 4096.</param>
  408. /// <param name="options">A value that specifies additional file options.</param>
  409. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  410. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  411. /// <returns>
  412. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  413. /// access and the specified sharing option.
  414. /// </returns>
  415. [SecurityCritical]
  416. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security, PathFormat pathFormat)
  417. {
  418. return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, security, pathFormat);
  419. }
  420. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.</summary>
  421. /// <param name="path">The file to open.</param>
  422. /// <param name="mode">A constant that determines how to open or create the file.</param>
  423. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  424. /// file.</param>
  425. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  426. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  427. /// default buffer size is 4096.</param>
  428. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  429. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  430. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  431. /// <returns>
  432. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  433. /// access and the specified sharing option.
  434. /// </returns>
  435. [SecurityCritical]
  436. public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security, PathFormat pathFormat)
  437. {
  438. return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, security, pathFormat);
  439. }
  440. #endregion // Using FileSystemRights
  441. #region Transactional
  442. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path with read/write access.</summary>
  443. /// <param name="transaction">The transaction.</param>
  444. /// <param name="path">The file to open.</param>
  445. /// <param name="mode">
  446. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  447. /// of existing files are retained or overwritten.
  448. /// </param>
  449. /// <returns>A <see cref="FileStream"/> opened in the specified mode and path, with read/write access and not shared.</returns>
  450. [SecurityCritical]
  451. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode)
  452. {
  453. return OpenCore(transaction, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath);
  454. }
  455. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path with read/write access.</summary>
  456. /// <param name="transaction">The transaction.</param>
  457. /// <param name="path">The file to open.</param>
  458. /// <param name="mode">
  459. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  460. /// of existing files are retained or overwritten.
  461. /// </param>
  462. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  463. /// <returns>A <see cref="FileStream"/> opened in the specified mode and path, with read/write access and not shared.</returns>
  464. [SecurityCritical]
  465. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, PathFormat pathFormat)
  466. {
  467. return OpenCore(transaction, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat);
  468. }
  469. #region Using FileAccess
  470. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path, with the specified mode and access.</summary>
  471. /// <param name="transaction">The transaction.</param>
  472. /// <param name="path">The file to open.</param>
  473. /// <param name="mode">
  474. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  475. /// of existing files are retained or overwritten.
  476. /// </param>
  477. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  478. /// <returns>
  479. /// An unshared <see cref="FileStream"/> that provides access to the specified file, with the specified mode and access.
  480. /// </returns>
  481. [SecurityCritical]
  482. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access)
  483. {
  484. return OpenCore(transaction, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath);
  485. }
  486. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</summary>
  487. /// <param name="transaction">The transaction.</param>
  488. /// <param name="path">The file to open.</param>
  489. /// <param name="mode">
  490. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  491. /// of existing files are retained or overwritten.
  492. /// </param>
  493. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  494. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  495. /// <returns>
  496. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the
  497. /// specified sharing option.
  498. /// </returns>
  499. [SecurityCritical]
  500. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share)
  501. {
  502. return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath);
  503. }
  504. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path, with the specified mode and access.</summary>
  505. /// <param name="transaction">The transaction.</param>
  506. /// <param name="path">The file to open.</param>
  507. /// <param name="mode">
  508. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  509. /// of existing files are retained or overwritten.
  510. /// </param>
  511. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  512. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  513. /// <returns>
  514. /// An unshared <see cref="FileStream"/> that provides access to the specified file, with the specified mode and access.
  515. /// </returns>
  516. [SecurityCritical]
  517. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, PathFormat pathFormat)
  518. {
  519. return OpenCore(transaction, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat);
  520. }
  521. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</summary>
  522. /// <param name="transaction">The transaction.</param>
  523. /// <param name="path">The file to open.</param>
  524. /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
  525. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  526. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  527. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  528. /// <returns>A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</returns>
  529. [SecurityCritical]
  530. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, PathFormat pathFormat)
  531. {
  532. return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, pathFormat);
  533. }
  534. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.</summary>
  535. /// <param name="transaction">The transaction.</param>
  536. /// <param name="path">The file to open.</param>
  537. /// <param name="mode">
  538. /// A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents
  539. /// of existing files are retained or overwritten.
  540. /// </param>
  541. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  542. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  543. /// <param name="extendedAttributes">The extended attributes.</param>
  544. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  545. /// <returns>
  546. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access and the
  547. /// specified sharing option.
  548. /// </returns>
  549. [SecurityCritical]
  550. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
  551. {
  552. return OpenCore(transaction, path, mode, access, share, extendedAttributes, null, null, pathFormat);
  553. }
  554. // New below
  555. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  556. /// <param name="transaction">The transaction.</param>
  557. /// <param name="path">The file to open.</param>
  558. /// <param name="mode">A constant that determines how to open or create the file.</param>
  559. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  560. /// file.</param>
  561. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  562. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  563. /// default buffer size is 4096.</param>
  564. /// <returns>
  565. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  566. /// access and the specified sharing option.
  567. /// </returns>
  568. [SecurityCritical]
  569. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)
  570. {
  571. return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, PathFormat.RelativePath);
  572. }
  573. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  574. /// <param name="transaction">The transaction.</param>
  575. /// <param name="path">The file to open.</param>
  576. /// <param name="mode">A constant that determines how to open or create the file.</param>
  577. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  578. /// file.</param>
  579. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  580. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  581. /// default buffer size is 4096.</param>
  582. /// <param name="useAsync">Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the
  583. /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be
  584. /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods
  585. /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the
  586. /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using
  587. /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without
  588. /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10.</param>
  589. /// <returns>
  590. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  591. /// access and the specified sharing option.
  592. /// </returns>
  593. [SecurityCritical]
  594. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync)
  595. {
  596. return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, PathFormat.RelativePath);
  597. }
  598. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  599. /// <param name="transaction">The transaction.</param>
  600. /// <param name="path">The file to open.</param>
  601. /// <param name="mode">A constant that determines how to open or create the file.</param>
  602. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  603. /// file.</param>
  604. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  605. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  606. /// default buffer size is 4096.</param>
  607. /// <param name="options">A value that specifies additional file options.</param>
  608. /// <returns>
  609. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  610. /// access and the specified sharing option.
  611. /// </returns>
  612. [SecurityCritical]
  613. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
  614. {
  615. return OpenCore(transaction, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, PathFormat.RelativePath);
  616. }
  617. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  618. /// <param name="transaction">The transaction.</param>
  619. /// <param name="path">The file to open.</param>
  620. /// <param name="mode">A constant that determines how to open or create the file.</param>
  621. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  622. /// file.</param>
  623. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  624. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  625. /// default buffer size is 4096.</param>
  626. /// <param name="extendedAttributes">The extended attributes specifying additional options.</param>
  627. /// <returns>
  628. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  629. /// access and the specified sharing option.
  630. /// </returns>
  631. [SecurityCritical]
  632. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes)
  633. {
  634. return OpenCore(transaction, path, mode, access, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath);
  635. }
  636. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  637. /// <param name="transaction">The transaction.</param>
  638. /// <param name="path">The file to open.</param>
  639. /// <param name="mode">A constant that determines how to open or create the file.</param>
  640. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  641. /// file.</param>
  642. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  643. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  644. /// default buffer size is 4096.</param>
  645. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  646. /// <returns>
  647. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  648. /// access and the specified sharing option.
  649. /// </returns>
  650. [SecurityCritical]
  651. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, PathFormat pathFormat)
  652. {
  653. return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, pathFormat);
  654. }
  655. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  656. /// <param name="transaction">The transaction.</param>
  657. /// <param name="path">The file to open.</param>
  658. /// <param name="mode">A constant that determines how to open or create the file.</param>
  659. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  660. /// file.</param>
  661. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  662. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  663. /// default buffer size is 4096.</param>
  664. /// <param name="useAsync">Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the
  665. /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be
  666. /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods
  667. /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the
  668. /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using
  669. /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without
  670. /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10.</param>
  671. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  672. /// <returns>
  673. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  674. /// access and the specified sharing option.
  675. /// </returns>
  676. [SecurityCritical]
  677. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync, PathFormat pathFormat)
  678. {
  679. return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, pathFormat);
  680. }
  681. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  682. /// <param name="transaction">The transaction.</param>
  683. /// <param name="path">The file to open.</param>
  684. /// <param name="mode">A constant that determines how to open or create the file.</param>
  685. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  686. /// file.</param>
  687. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  688. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  689. /// default buffer size is 4096.</param>
  690. /// <param name="options">A value that specifies additional file options.</param>
  691. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  692. /// <returns>
  693. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  694. /// access and the specified sharing option.
  695. /// </returns>
  696. [SecurityCritical]
  697. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat)
  698. {
  699. return OpenCore(transaction, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat);
  700. }
  701. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  702. /// <param name="transaction">The transaction.</param>
  703. /// <param name="path">The file to open.</param>
  704. /// <param name="mode">A constant that determines how to open or create the file.</param>
  705. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  706. /// file.</param>
  707. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  708. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  709. /// default buffer size is 4096.</param>
  710. /// <param name="extendedAttributes">The extended attributes specifying additional options.</param>
  711. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  712. /// <returns>
  713. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  714. /// access and the specified sharing option.
  715. /// </returns>
  716. [SecurityCritical]
  717. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
  718. {
  719. return OpenCore(transaction, path, mode, access, share, extendedAttributes, bufferSize, null, pathFormat);
  720. }
  721. #endregion // Using FileAccess
  722. #region Using FileSystemRights
  723. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  724. /// <param name="transaction">The transaction.</param>
  725. /// <param name="path">The file to open.</param>
  726. /// <param name="mode">A constant that determines how to open or create the file.</param>
  727. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  728. /// file.</param>
  729. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  730. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  731. /// default buffer size is 4096.</param>
  732. /// <param name="options">A value that specifies additional file options.</param>
  733. /// <returns>
  734. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  735. /// access and the specified sharing option.
  736. /// </returns>
  737. [SecurityCritical]
  738. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options)
  739. {
  740. return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, null, PathFormat.RelativePath);
  741. }
  742. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  743. /// <param name="transaction">The transaction.</param>
  744. /// <param name="path">The file to open.</param>
  745. /// <param name="mode">A constant that determines how to open or create the file.</param>
  746. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  747. /// file.</param>
  748. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  749. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  750. /// default buffer size is 4096.</param>
  751. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  752. /// <returns>
  753. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  754. /// access and the specified sharing option.
  755. /// </returns>
  756. [SecurityCritical]
  757. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes)
  758. {
  759. return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath);
  760. }
  761. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  762. /// <param name="transaction">The transaction.</param>
  763. /// <param name="path">The file to open.</param>
  764. /// <param name="mode">A constant that determines how to open or create the file.</param>
  765. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  766. /// file.</param>
  767. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  768. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  769. /// default buffer size is 4096.</param>
  770. /// <param name="options">A value that specifies additional file options.</param>
  771. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  772. /// <returns>
  773. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  774. /// access and the specified sharing option.
  775. /// </returns>
  776. [SecurityCritical]
  777. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security)
  778. {
  779. return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, security, PathFormat.RelativePath);
  780. }
  781. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  782. /// <param name="transaction">The transaction.</param>
  783. /// <param name="path">The file to open.</param>
  784. /// <param name="mode">A constant that determines how to open or create the file.</param>
  785. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  786. /// file.</param>
  787. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  788. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  789. /// default buffer size is 4096.</param>
  790. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  791. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  792. /// <returns>
  793. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  794. /// access and the specified sharing option.
  795. /// </returns>
  796. [SecurityCritical]
  797. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security)
  798. {
  799. return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, security, PathFormat.RelativePath);
  800. }
  801. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  802. /// <param name="transaction">The transaction.</param>
  803. /// <param name="path">The file to open.</param>
  804. /// <param name="mode">A constant that determines how to open or create the file.</param>
  805. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  806. /// file.</param>
  807. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  808. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  809. /// default buffer size is 4096.</param>
  810. /// <param name="options">A value that specifies additional file options.</param>
  811. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  812. /// <returns>
  813. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  814. /// access and the specified sharing option.
  815. /// </returns>
  816. [SecurityCritical]
  817. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat)
  818. {
  819. return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat);
  820. }
  821. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, read/write and sharing permission, and buffer size.</summary>
  822. /// <param name="transaction">The transaction.</param>
  823. /// <param name="path">The file to open.</param>
  824. /// <param name="mode">A constant that determines how to open or create the file.</param>
  825. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  826. /// file.</param>
  827. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  828. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  829. /// default buffer size is 4096.</param>
  830. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  831. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  832. /// <returns>
  833. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  834. /// access and the specified sharing option.
  835. /// </returns>
  836. [SecurityCritical]
  837. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat)
  838. {
  839. return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, null, pathFormat);
  840. }
  841. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.</summary>
  842. /// <param name="transaction">The transaction.</param>
  843. /// <param name="path">The file to open.</param>
  844. /// <param name="mode">A constant that determines how to open or create the file.</param>
  845. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  846. /// file.</param>
  847. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  848. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  849. /// default buffer size is 4096.</param>
  850. /// <param name="options">A value that specifies additional file options.</param>
  851. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  852. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  853. /// <returns>
  854. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  855. /// access and the specified sharing option.
  856. /// </returns>
  857. [SecurityCritical]
  858. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security, PathFormat pathFormat)
  859. {
  860. return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, security, pathFormat);
  861. }
  862. /// <summary>[AlphaFS] (Transacted) Opens a <see cref="FileStream"/> on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.</summary>
  863. /// <param name="transaction">The transaction.</param>
  864. /// <param name="path">The file to open.</param>
  865. /// <param name="mode">A constant that determines how to open or create the file.</param>
  866. /// <param name="rights">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the
  867. /// file.</param>
  868. /// <param name="share">A constant that determines how the file will be shared by processes.</param>
  869. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The
  870. /// default buffer size is 4096.</param>
  871. /// <param name="extendedAttributes">Extended attributes specifying additional options.</param>
  872. /// <param name="security">A value that determines the access control and audit security for the file.</param>
  873. /// <param name="pathFormat">Indicates the format of the path parameter.</param>
  874. /// <returns>
  875. /// A <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write
  876. /// access and the specified sharing option.
  877. /// </returns>
  878. [SecurityCritical]
  879. public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security, PathFormat pathFormat)
  880. {
  881. return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, security, pathFormat);
  882. }
  883. #endregion // Using FileSystemRights
  884. #endregion // Transacted
  885. #region Internal Methods
  886. /// <summary>[AlphaFS] Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access, the specified sharing option and additional options specified.</summary>
  887. /// <param name="transaction">The transaction.</param>
  888. /// <param name="path">The file to open.</param>
  889. /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
  890. /// <param name="access">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
  891. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  892. /// <param name="attributes">Advanced <see cref="ExtendedFileAttributes"/> options for this file.</param>
  893. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The default buffer size is 4096.</param>
  894. /// <param name="security">The security.</param>
  895. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  896. /// <returns>
  897. /// <para>A <see cref="FileStream"/> instance on the specified path, having the specified mode with</para>
  898. /// <para>read, write, or read/write access and the specified sharing option.</para>
  899. /// </returns>
  900. internal static FileStream OpenCore(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes attributes, int? bufferSize, FileSecurity security, PathFormat pathFormat)
  901. {
  902. var rights = access == FileAccess.Read
  903. ? FileSystemRights.Read
  904. : (access == FileAccess.Write ? FileSystemRights.Write : FileSystemRights.Read | FileSystemRights.Write);
  905. return OpenCore(transaction, path, mode, rights, share, attributes, bufferSize, security, pathFormat);
  906. }
  907. /// <summary>Opens a <see cref="FileStream"/> on the specified path, having the specified mode with read, write, or read/write access, the specified sharing option and additional options specified.</summary>
  908. /// <param name="transaction">The transaction.</param>
  909. /// <param name="path">The file to open.</param>
  910. /// <param name="mode">A <see cref="FileMode"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.</param>
  911. /// <param name="rights">A <see cref="FileSystemRights"/> value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten along with additional options.</param>
  912. /// <param name="share">A <see cref="FileShare"/> value specifying the type of access other threads have to the file.</param>
  913. /// <param name="attributes">Advanced <see cref="ExtendedFileAttributes"/> options for this file.</param>
  914. /// <param name="bufferSize">A positive <see cref="System.Int32"/> value greater than 0 indicating the buffer size. The default buffer size is 4096.</param>
  915. /// <param name="security">The security.</param>
  916. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  917. /// <returns>
  918. /// <para>A <see cref="FileStream"/> instance on the specified path, having the specified mode with</para>
  919. /// <para>read, write, or read/write access and the specified sharing option.</para>
  920. /// </returns>
  921. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  922. internal static FileStream OpenCore(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, ExtendedFileAttributes attributes, int? bufferSize, FileSecurity security, PathFormat pathFormat)
  923. {
  924. var access = ((rights & FileSystemRights.ReadData) != 0 ? FileAccess.Read : 0) |
  925. ((rights & FileSystemRights.WriteData) != 0 || (rights & FileSystemRights.AppendData) != 0
  926. ? FileAccess.Write
  927. : 0);
  928. SafeFileHandle safeHandle = null;
  929. try
  930. {
  931. safeHandle = CreateFileCore(transaction, path, attributes, security, mode, rights, share, true, pathFormat);
  932. return new FileStream(safeHandle, access, bufferSize ?? NativeMethods.DefaultFileBufferSize, (attributes & ExtendedFileAttributes.Overlapped) != 0);
  933. }
  934. catch
  935. {
  936. if (safeHandle != null)
  937. safeHandle.Dispose();
  938. throw;
  939. }
  940. }
  941. #endregion // Internal Methods
  942. }
  943. }