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.
 
 

1010 rivejä
55 KiB

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