PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/referencesource/mscorlib/system/io/file.cs

https://github.com/pruiz/mono
C# | 1152 lines | 929 code | 136 blank | 87 comment | 212 complexity | 1ccb0cea946f0c00e030df3dc763b728 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // ==++==
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // ==--==
  6. /*============================================================
  7. **
  8. ** Class: File
  9. **
  10. ** <OWNER>Microsoft</OWNER>
  11. **
  12. **
  13. ** Purpose: A collection of methods for manipulating Files.
  14. **
  15. ** April 09,2000 (some design refactorization)
  16. **
  17. ===========================================================*/
  18. using System;
  19. using System.Security.Permissions;
  20. using PermissionSet = System.Security.PermissionSet;
  21. using Win32Native = Microsoft.Win32.Win32Native;
  22. using System.Runtime.InteropServices;
  23. using System.Security;
  24. #if FEATURE_MACL
  25. using System.Security.AccessControl;
  26. #endif
  27. using System.Text;
  28. using Microsoft.Win32.SafeHandles;
  29. using System.Collections.Generic;
  30. using System.Globalization;
  31. using System.Runtime.Versioning;
  32. using System.Diagnostics.Contracts;
  33. namespace System.IO {
  34. // Class for creating FileStream objects, and some basic file management
  35. // routines such as Delete, etc.
  36. [ComVisible(true)]
  37. public static class File
  38. {
  39. private const int GetFileExInfoStandard = 0;
  40. [ResourceExposure(ResourceScope.Machine)]
  41. [ResourceConsumption(ResourceScope.Machine)]
  42. public static StreamReader OpenText(String path)
  43. {
  44. if (path == null)
  45. throw new ArgumentNullException("path");
  46. Contract.EndContractBlock();
  47. return new StreamReader(path);
  48. }
  49. [ResourceExposure(ResourceScope.Machine)]
  50. [ResourceConsumption(ResourceScope.Machine)]
  51. public static StreamWriter CreateText(String path)
  52. {
  53. if (path == null)
  54. throw new ArgumentNullException("path");
  55. Contract.EndContractBlock();
  56. return new StreamWriter(path,false);
  57. }
  58. [ResourceExposure(ResourceScope.Machine)]
  59. [ResourceConsumption(ResourceScope.Machine)]
  60. public static StreamWriter AppendText(String path)
  61. {
  62. if (path == null)
  63. throw new ArgumentNullException("path");
  64. Contract.EndContractBlock();
  65. return new StreamWriter(path,true);
  66. }
  67. // Copies an existing file to a new file. An exception is raised if the
  68. // destination file already exists. Use the
  69. // Copy(String, String, boolean) method to allow
  70. // overwriting an existing file.
  71. //
  72. // The caller must have certain FileIOPermissions. The caller must have
  73. // Read permission to sourceFileName and Create
  74. // and Write permissions to destFileName.
  75. //
  76. [ResourceExposure(ResourceScope.Machine)]
  77. [ResourceConsumption(ResourceScope.Machine)]
  78. public static void Copy(String sourceFileName, String destFileName) {
  79. if (sourceFileName == null)
  80. throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
  81. if (destFileName == null)
  82. throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
  83. if (sourceFileName.Length == 0)
  84. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
  85. if (destFileName.Length == 0)
  86. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
  87. Contract.EndContractBlock();
  88. InternalCopy(sourceFileName, destFileName, false, true);
  89. }
  90. // Copies an existing file to a new file. If overwrite is
  91. // false, then an IOException is thrown if the destination file
  92. // already exists. If overwrite is true, the file is
  93. // overwritten.
  94. //
  95. // The caller must have certain FileIOPermissions. The caller must have
  96. // Read permission to sourceFileName
  97. // and Write permissions to destFileName.
  98. //
  99. [ResourceExposure(ResourceScope.Machine)]
  100. [ResourceConsumption(ResourceScope.Machine)]
  101. public static void Copy(String sourceFileName, String destFileName, bool overwrite) {
  102. if (sourceFileName == null)
  103. throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
  104. if (destFileName == null)
  105. throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
  106. if (sourceFileName.Length == 0)
  107. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
  108. if (destFileName.Length == 0)
  109. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
  110. Contract.EndContractBlock();
  111. InternalCopy(sourceFileName, destFileName, overwrite, true);
  112. }
  113. [System.Security.SecurityCritical]
  114. [ResourceExposure(ResourceScope.Machine)]
  115. [ResourceConsumption(ResourceScope.Machine)]
  116. internal static void UnsafeCopy(String sourceFileName, String destFileName, bool overwrite) {
  117. if (sourceFileName == null)
  118. throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
  119. if (destFileName == null)
  120. throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
  121. if (sourceFileName.Length == 0)
  122. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
  123. if (destFileName.Length == 0)
  124. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
  125. Contract.EndContractBlock();
  126. InternalCopy(sourceFileName, destFileName, overwrite, false);
  127. }
  128. /// <devdoc>
  129. /// Note: This returns the fully qualified name of the destination file.
  130. /// </devdoc>
  131. [System.Security.SecuritySafeCritical]
  132. [ResourceExposure(ResourceScope.Machine)]
  133. [ResourceConsumption(ResourceScope.Machine)]
  134. internal static String InternalCopy(String sourceFileName, String destFileName, bool overwrite, bool checkHost) {
  135. Contract.Requires(sourceFileName != null);
  136. Contract.Requires(destFileName != null);
  137. Contract.Requires(sourceFileName.Length > 0);
  138. Contract.Requires(destFileName.Length > 0);
  139. String fullSourceFileName = Path.GetFullPathInternal(sourceFileName);
  140. String fullDestFileName = Path.GetFullPathInternal(destFileName);
  141. #if FEATURE_CORECLR
  142. if (checkHost) {
  143. FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.Read, sourceFileName, fullSourceFileName);
  144. FileSecurityState destState = new FileSecurityState(FileSecurityStateAccess.Write, destFileName, fullDestFileName);
  145. sourceState.EnsureState();
  146. destState.EnsureState();
  147. }
  148. #else
  149. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullSourceFileName, false, false);
  150. FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullDestFileName, false, false);
  151. #endif
  152. bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite);
  153. if (!r) {
  154. // Save Win32 error because subsequent checks will overwrite this HRESULT.
  155. int errorCode = Marshal.GetLastWin32Error();
  156. String fileName = destFileName;
  157. if (errorCode != Win32Native.ERROR_FILE_EXISTS) {
  158. // For a number of error codes (sharing violation, path
  159. // not found, etc) we don't know if the problem was with
  160. // the source or dest file. Try reading the source file.
  161. using(SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero)) {
  162. if (handle.IsInvalid)
  163. fileName = sourceFileName;
  164. }
  165. if (errorCode == Win32Native.ERROR_ACCESS_DENIED) {
  166. if (Directory.InternalExists(fullDestFileName))
  167. throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", destFileName), Win32Native.ERROR_ACCESS_DENIED, fullDestFileName);
  168. }
  169. }
  170. __Error.WinIOError(errorCode, fileName);
  171. }
  172. return fullDestFileName;
  173. }
  174. // Creates a file in a particular path. If the file exists, it is replaced.
  175. // The file is opened with ReadWrite accessand cannot be opened by another
  176. // application until it has been closed. An IOException is thrown if the
  177. // directory specified doesn't exist.
  178. //
  179. // Your application must have Create, Read, and Write permissions to
  180. // the file.
  181. //
  182. [ResourceExposure(ResourceScope.Machine)]
  183. [ResourceConsumption(ResourceScope.Machine)]
  184. public static FileStream Create(String path) {
  185. return Create(path, FileStream.DefaultBufferSize);
  186. }
  187. // Creates a file in a particular path. If the file exists, it is replaced.
  188. // The file is opened with ReadWrite access and cannot be opened by another
  189. // application until it has been closed. An IOException is thrown if the
  190. // directory specified doesn't exist.
  191. //
  192. // Your application must have Create, Read, and Write permissions to
  193. // the file.
  194. //
  195. [ResourceExposure(ResourceScope.Machine)]
  196. [ResourceConsumption(ResourceScope.Machine)]
  197. public static FileStream Create(String path, int bufferSize) {
  198. return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize);
  199. }
  200. [ResourceExposure(ResourceScope.Machine)]
  201. [ResourceConsumption(ResourceScope.Machine)]
  202. public static FileStream Create(String path, int bufferSize, FileOptions options) {
  203. return new FileStream(path, FileMode.Create, FileAccess.ReadWrite,
  204. FileShare.None, bufferSize, options);
  205. }
  206. #if FEATURE_MACL
  207. public static FileStream Create(String path, int bufferSize, FileOptions options, FileSecurity fileSecurity) {
  208. return new FileStream(path, FileMode.Create, FileSystemRights.Read | FileSystemRights.Write,
  209. FileShare.None, bufferSize, options, fileSecurity);
  210. }
  211. #endif
  212. // Deletes a file. The file specified by the designated path is deleted.
  213. // If the file does not exist, Delete succeeds without throwing
  214. // an exception.
  215. //
  216. // On NT, Delete will fail for a file that is open for normal I/O
  217. // or a file that is memory mapped.
  218. //
  219. // Your application must have Delete permission to the target file.
  220. //
  221. [System.Security.SecuritySafeCritical]
  222. [ResourceExposure(ResourceScope.Machine)]
  223. [ResourceConsumption(ResourceScope.Machine)]
  224. public static void Delete(String path) {
  225. if (path == null)
  226. throw new ArgumentNullException("path");
  227. Contract.EndContractBlock();
  228. #if FEATURE_LEGACYNETCF
  229. if(CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) {
  230. System.Reflection.Assembly callingAssembly = System.Reflection.Assembly.GetCallingAssembly();
  231. if(callingAssembly != null && !callingAssembly.IsProfileAssembly) {
  232. string caller = new System.Diagnostics.StackFrame(1).GetMethod().FullName;
  233. string callee = System.Reflection.MethodBase.GetCurrentMethod().FullName;
  234. throw new MethodAccessException(String.Format(
  235. CultureInfo.CurrentCulture,
  236. Environment.GetResourceString("Arg_MethodAccessException_WithCaller"),
  237. caller,
  238. callee));
  239. }
  240. }
  241. #endif // FEATURE_LEGACYNETCF
  242. InternalDelete(path, true);
  243. }
  244. [System.Security.SecurityCritical]
  245. [ResourceExposure(ResourceScope.Machine)]
  246. [ResourceConsumption(ResourceScope.Machine)]
  247. internal static void UnsafeDelete(String path)
  248. {
  249. if (path == null)
  250. throw new ArgumentNullException("path");
  251. Contract.EndContractBlock();
  252. InternalDelete(path, false);
  253. }
  254. [System.Security.SecurityCritical]
  255. [ResourceExposure(ResourceScope.Machine)]
  256. [ResourceConsumption(ResourceScope.Machine)]
  257. internal static void InternalDelete(String path, bool checkHost)
  258. {
  259. String fullPath = Path.GetFullPathInternal(path);
  260. #if FEATURE_CORECLR
  261. if (checkHost)
  262. {
  263. FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, path, fullPath);
  264. state.EnsureState();
  265. }
  266. #else
  267. // For security check, path should be resolved to an absolute path.
  268. FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPath, false, false);
  269. #endif
  270. bool r = Win32Native.DeleteFile(fullPath);
  271. if (!r) {
  272. int hr = Marshal.GetLastWin32Error();
  273. if (hr==Win32Native.ERROR_FILE_NOT_FOUND)
  274. return;
  275. else
  276. __Error.WinIOError(hr, fullPath);
  277. }
  278. }
  279. [System.Security.SecuritySafeCritical] // auto-generated
  280. [ResourceExposure(ResourceScope.Machine)]
  281. [ResourceConsumption(ResourceScope.Machine)]
  282. public static void Decrypt(String path)
  283. {
  284. if (path == null)
  285. throw new ArgumentNullException("path");
  286. Contract.EndContractBlock();
  287. #if FEATURE_PAL
  288. throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_RequiresNT"));
  289. #else
  290. String fullPath = Path.GetFullPathInternal(path);
  291. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, fullPath, false, false);
  292. bool r = Win32Native.DecryptFile(fullPath, 0);
  293. if (!r) {
  294. int errorCode = Marshal.GetLastWin32Error();
  295. if (errorCode == Win32Native.ERROR_ACCESS_DENIED) {
  296. // Check to see if the file system is not NTFS. If so,
  297. // throw a different exception.
  298. DriveInfo di = new DriveInfo(Path.GetPathRoot(fullPath));
  299. if (!String.Equals("NTFS", di.DriveFormat))
  300. throw new NotSupportedException(Environment.GetResourceString("NotSupported_EncryptionNeedsNTFS"));
  301. }
  302. __Error.WinIOError(errorCode, fullPath);
  303. }
  304. #endif
  305. }
  306. [System.Security.SecuritySafeCritical] // auto-generated
  307. [ResourceExposure(ResourceScope.Machine)]
  308. [ResourceConsumption(ResourceScope.Machine)]
  309. public static void Encrypt(String path)
  310. {
  311. if (path == null)
  312. throw new ArgumentNullException("path");
  313. Contract.EndContractBlock();
  314. #if FEATURE_PAL
  315. throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_RequiresNT"));
  316. #else
  317. String fullPath = Path.GetFullPathInternal(path);
  318. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, fullPath, false, false);
  319. bool r = Win32Native.EncryptFile(fullPath);
  320. if (!r) {
  321. int errorCode = Marshal.GetLastWin32Error();
  322. if (errorCode == Win32Native.ERROR_ACCESS_DENIED) {
  323. // Check to see if the file system is not NTFS. If so,
  324. // throw a different exception.
  325. DriveInfo di = new DriveInfo(Path.GetPathRoot(fullPath));
  326. if (!String.Equals("NTFS", di.DriveFormat))
  327. throw new NotSupportedException(Environment.GetResourceString("NotSupported_EncryptionNeedsNTFS"));
  328. }
  329. __Error.WinIOError(errorCode, fullPath);
  330. }
  331. #endif
  332. }
  333. // Tests if a file exists. The result is true if the file
  334. // given by the specified path exists; otherwise, the result is
  335. // false. Note that if path describes a directory,
  336. // Exists will return true.
  337. //
  338. // Your application must have Read permission for the target directory.
  339. //
  340. [System.Security.SecuritySafeCritical]
  341. [ResourceExposure(ResourceScope.Machine)]
  342. [ResourceConsumption(ResourceScope.Machine)]
  343. public static bool Exists(String path)
  344. {
  345. #if FEATURE_LEGACYNETCF
  346. if(CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) {
  347. System.Reflection.Assembly callingAssembly = System.Reflection.Assembly.GetCallingAssembly();
  348. if(callingAssembly != null && !callingAssembly.IsProfileAssembly) {
  349. string caller = new System.Diagnostics.StackFrame(1).GetMethod().FullName;
  350. string callee = System.Reflection.MethodBase.GetCurrentMethod().FullName;
  351. throw new MethodAccessException(String.Format(
  352. CultureInfo.CurrentCulture,
  353. Environment.GetResourceString("Arg_MethodAccessException_WithCaller"),
  354. caller,
  355. callee));
  356. }
  357. }
  358. #endif // FEATURE_LEGACYNETCF
  359. return InternalExistsHelper(path, true);
  360. }
  361. [System.Security.SecurityCritical]
  362. [ResourceExposure(ResourceScope.Machine)]
  363. [ResourceConsumption(ResourceScope.Machine)]
  364. internal static bool UnsafeExists(String path)
  365. {
  366. return InternalExistsHelper(path, false);
  367. }
  368. [System.Security.SecurityCritical]
  369. [ResourceExposure(ResourceScope.Machine)]
  370. [ResourceConsumption(ResourceScope.Machine)]
  371. private static bool InternalExistsHelper(String path, bool checkHost)
  372. {
  373. try
  374. {
  375. if (path == null)
  376. return false;
  377. if (path.Length == 0)
  378. return false;
  379. path = Path.GetFullPathInternal(path);
  380. // After normalizing, check whether path ends in directory separator.
  381. // Otherwise, FillAttributeInfo removes it and we may return a false positive.
  382. // GetFullPathInternal should never return null
  383. Contract.Assert(path != null, "File.Exists: GetFullPathInternal returned null");
  384. if (path.Length > 0 && Path.IsDirectorySeparator(path[path.Length - 1]))
  385. {
  386. return false;
  387. }
  388. #if FEATURE_CORECLR
  389. if (checkHost)
  390. {
  391. FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, String.Empty, path);
  392. state.EnsureState();
  393. }
  394. #else
  395. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, path, false, false);
  396. #endif
  397. return InternalExists(path);
  398. }
  399. catch (ArgumentException) { }
  400. catch (NotSupportedException) { } // Security can throw this on ":"
  401. catch (SecurityException) { }
  402. catch (IOException) { }
  403. catch (UnauthorizedAccessException) { }
  404. return false;
  405. }
  406. [System.Security.SecurityCritical] // auto-generated
  407. internal static bool InternalExists(String path) {
  408. Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
  409. int dataInitialised = FillAttributeInfo(path, ref data, false, true);
  410. return (dataInitialised == 0) && (data.fileAttributes != -1)
  411. && ((data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0);
  412. }
  413. [ResourceExposure(ResourceScope.Machine)]
  414. [ResourceConsumption(ResourceScope.Machine)]
  415. public static FileStream Open(String path, FileMode mode) {
  416. return Open(path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None);
  417. }
  418. [ResourceExposure(ResourceScope.Machine)]
  419. [ResourceConsumption(ResourceScope.Machine)]
  420. public static FileStream Open(String path, FileMode mode, FileAccess access) {
  421. return Open(path,mode, access, FileShare.None);
  422. }
  423. [ResourceExposure(ResourceScope.Machine)]
  424. [ResourceConsumption(ResourceScope.Machine)]
  425. public static FileStream Open(String path, FileMode mode, FileAccess access, FileShare share) {
  426. return new FileStream(path, mode, access, share);
  427. }
  428. [ResourceExposure(ResourceScope.Machine)]
  429. [ResourceConsumption(ResourceScope.Machine)]
  430. public static void SetCreationTime(String path, DateTime creationTime)
  431. {
  432. SetCreationTimeUtc(path, creationTime.ToUniversalTime());
  433. }
  434. [System.Security.SecuritySafeCritical] // auto-generated
  435. [ResourceExposure(ResourceScope.Machine)]
  436. [ResourceConsumption(ResourceScope.Machine)]
  437. public unsafe static void SetCreationTimeUtc(String path, DateTime creationTimeUtc)
  438. {
  439. SafeFileHandle handle;
  440. using(OpenFile(path, FileAccess.Write, out handle)) {
  441. Win32Native.FILE_TIME fileTime = new Win32Native.FILE_TIME(creationTimeUtc.ToFileTimeUtc());
  442. bool r = Win32Native.SetFileTime(handle, &fileTime, null, null);
  443. if (!r)
  444. {
  445. int errorCode = Marshal.GetLastWin32Error();
  446. __Error.WinIOError(errorCode, path);
  447. }
  448. }
  449. }
  450. [System.Security.SecuritySafeCritical]
  451. [ResourceExposure(ResourceScope.Machine)]
  452. [ResourceConsumption(ResourceScope.Machine)]
  453. public static DateTime GetCreationTime(String path)
  454. {
  455. return InternalGetCreationTimeUtc(path, true).ToLocalTime();
  456. }
  457. [System.Security.SecuritySafeCritical] // auto-generated
  458. [ResourceExposure(ResourceScope.Machine)]
  459. [ResourceConsumption(ResourceScope.Machine)]
  460. public static DateTime GetCreationTimeUtc(String path)
  461. {
  462. return InternalGetCreationTimeUtc(path, false); // this API isn't exposed in Silverlight
  463. }
  464. [System.Security.SecurityCritical]
  465. [ResourceExposure(ResourceScope.Machine)]
  466. [ResourceConsumption(ResourceScope.Machine)]
  467. private static DateTime InternalGetCreationTimeUtc(String path, bool checkHost)
  468. {
  469. String fullPath = Path.GetFullPathInternal(path);
  470. #if FEATURE_CORECLR
  471. if (checkHost)
  472. {
  473. FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, path, fullPath);
  474. state.EnsureState();
  475. }
  476. #else
  477. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPath, false, false);
  478. #endif
  479. Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
  480. int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);
  481. if (dataInitialised != 0)
  482. __Error.WinIOError(dataInitialised, fullPath);
  483. long dt = ((long)(data.ftCreationTimeHigh) << 32) | ((long)data.ftCreationTimeLow);
  484. return DateTime.FromFileTimeUtc(dt);
  485. }
  486. [ResourceExposure(ResourceScope.Machine)]
  487. [ResourceConsumption(ResourceScope.Machine)]
  488. public static void SetLastAccessTime(String path, DateTime lastAccessTime)
  489. {
  490. SetLastAccessTimeUtc(path, lastAccessTime.ToUniversalTime());
  491. }
  492. [System.Security.SecuritySafeCritical] // auto-generated
  493. [ResourceExposure(ResourceScope.Machine)]
  494. [ResourceConsumption(ResourceScope.Machine)]
  495. public unsafe static void SetLastAccessTimeUtc(String path, DateTime lastAccessTimeUtc)
  496. {
  497. SafeFileHandle handle;
  498. using(OpenFile(path, FileAccess.Write, out handle)) {
  499. Win32Native.FILE_TIME fileTime = new Win32Native.FILE_TIME(lastAccessTimeUtc.ToFileTimeUtc());
  500. bool r = Win32Native.SetFileTime(handle, null, &fileTime, null);
  501. if (!r)
  502. {
  503. int errorCode = Marshal.GetLastWin32Error();
  504. __Error.WinIOError(errorCode, path);
  505. }
  506. }
  507. }
  508. [System.Security.SecuritySafeCritical]
  509. [ResourceExposure(ResourceScope.Machine)]
  510. [ResourceConsumption(ResourceScope.Machine)]
  511. public static DateTime GetLastAccessTime(String path)
  512. {
  513. return InternalGetLastAccessTimeUtc(path, true).ToLocalTime();
  514. }
  515. [System.Security.SecuritySafeCritical] // auto-generated
  516. [ResourceExposure(ResourceScope.Machine)]
  517. [ResourceConsumption(ResourceScope.Machine)]
  518. public static DateTime GetLastAccessTimeUtc(String path)
  519. {
  520. return InternalGetLastAccessTimeUtc(path, false); // this API isn't exposed in Silverlight
  521. }
  522. [System.Security.SecurityCritical]
  523. [ResourceExposure(ResourceScope.Machine)]
  524. [ResourceConsumption(ResourceScope.Machine)]
  525. private static DateTime InternalGetLastAccessTimeUtc(String path, bool checkHost)
  526. {
  527. String fullPath = Path.GetFullPathInternal(path);
  528. #if FEATURE_CORECLR
  529. if (checkHost)
  530. {
  531. FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, path, fullPath);
  532. state.EnsureState();
  533. }
  534. #else
  535. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPath, false, false);
  536. #endif
  537. Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
  538. int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);
  539. if (dataInitialised != 0)
  540. __Error.WinIOError(dataInitialised, fullPath);
  541. long dt = ((long)(data.ftLastAccessTimeHigh) << 32) | ((long)data.ftLastAccessTimeLow);
  542. return DateTime.FromFileTimeUtc(dt);
  543. }
  544. [ResourceExposure(ResourceScope.Machine)]
  545. [ResourceConsumption(ResourceScope.Machine)]
  546. public static void SetLastWriteTime(String path, DateTime lastWriteTime)
  547. {
  548. SetLastWriteTimeUtc(path, lastWriteTime.ToUniversalTime());
  549. }
  550. [System.Security.SecuritySafeCritical] // auto-generated
  551. [ResourceExposure(ResourceScope.Machine)]
  552. [ResourceConsumption(ResourceScope.Machine)]
  553. public unsafe static void SetLastWriteTimeUtc(String path, DateTime lastWriteTimeUtc)
  554. {
  555. SafeFileHandle handle;
  556. using(OpenFile(path, FileAccess.Write, out handle)) {
  557. Win32Native.FILE_TIME fileTime = new Win32Native.FILE_TIME(lastWriteTimeUtc.ToFileTimeUtc());
  558. bool r = Win32Native.SetFileTime(handle, null, null, &fileTime);
  559. if (!r)
  560. {
  561. int errorCode = Marshal.GetLastWin32Error();
  562. __Error.WinIOError(errorCode, path);
  563. }
  564. }
  565. }
  566. [System.Security.SecuritySafeCritical]
  567. [ResourceExposure(ResourceScope.Machine)]
  568. [ResourceConsumption(ResourceScope.Machine)]
  569. public static DateTime GetLastWriteTime(String path)
  570. {
  571. return InternalGetLastWriteTimeUtc(path, true).ToLocalTime();
  572. }
  573. [System.Security.SecuritySafeCritical] // auto-generated
  574. [ResourceExposure(ResourceScope.Machine)]
  575. [ResourceConsumption(ResourceScope.Machine)]
  576. public static DateTime GetLastWriteTimeUtc(String path)
  577. {
  578. return InternalGetLastWriteTimeUtc(path, false); // this API isn't exposed in Silverlight
  579. }
  580. [System.Security.SecurityCritical]
  581. [ResourceExposure(ResourceScope.Machine)]
  582. [ResourceConsumption(ResourceScope.Machine)]
  583. private static DateTime InternalGetLastWriteTimeUtc(String path, bool checkHost)
  584. {
  585. String fullPath = Path.GetFullPathInternal(path);
  586. #if FEATURE_CORECLR
  587. if (checkHost)
  588. {
  589. FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, path, fullPath);
  590. state.EnsureState();
  591. }
  592. #else
  593. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPath, false, false);
  594. #endif
  595. Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
  596. int dataInitialised = FillAttributeInfo(fullPath, ref data, false, false);
  597. if (dataInitialised != 0)
  598. __Error.WinIOError(dataInitialised, fullPath);
  599. long dt = ((long)data.ftLastWriteTimeHigh << 32) | ((long)data.ftLastWriteTimeLow);
  600. return DateTime.FromFileTimeUtc(dt);
  601. }
  602. [System.Security.SecuritySafeCritical]
  603. [ResourceExposure(ResourceScope.Machine)]
  604. [ResourceConsumption(ResourceScope.Machine)]
  605. public static FileAttributes GetAttributes(String path)
  606. {
  607. String fullPath = Path.GetFullPathInternal(path);
  608. #if FEATURE_CORECLR
  609. FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, path, fullPath);
  610. state.EnsureState();
  611. #else
  612. FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPath, false, false);
  613. #endif
  614. Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
  615. int dataInitialised = FillAttributeInfo(fullPath, ref data, false, true);
  616. if (dataInitialised != 0)
  617. __Error.WinIOError(dataInitialised, fullPath);
  618. return (FileAttributes) data.fileAttributes;
  619. }
  620. #if FEATURE_CORECLR
  621. [System.Security.SecurityCritical]
  622. #else
  623. [System.Security.SecuritySafeCritical]
  624. #endif
  625. [ResourceExposure(ResourceScope.Machine)]
  626. [ResourceConsumption(ResourceScope.Machine)]
  627. public static void SetAttributes(String path, FileAttributes fileAttributes)
  628. {
  629. String fullPath = Path.GetFullPathInternal(path);
  630. #if !FEATURE_CORECLR
  631. FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPath, false, false);
  632. #endif
  633. bool r = Win32Native.SetFileAttributes(fullPath, (int) fileAttributes);
  634. if (!r) {
  635. int hr = Marshal.GetLastWin32Error();
  636. if (hr==ERROR_INVALID_PARAMETER)
  637. throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileAttrs"));
  638. __Error.WinIOError(hr, fullPath);
  639. }
  640. }
  641. #if FEATURE_MACL
  642. [ResourceExposure(ResourceScope.Machine)]
  643. [ResourceConsumption(ResourceScope.Machine)]
  644. public static FileSecurity GetAccessControl(String path)
  645. {
  646. return GetAccessControl(path, AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group);
  647. }
  648. [ResourceExposure(ResourceScope.Machine)]
  649. [ResourceConsumption(ResourceScope.Machine)]
  650. public static FileSecurity GetAccessControl(String path, AccessControlSections includeSections)
  651. {
  652. // Appropriate security check should be done for us by FileSecurity.
  653. return new FileSecurity(path, includeSections);
  654. }
  655. [System.Security.SecuritySafeCritical] // auto-generated
  656. [ResourceExposure(ResourceScope.Machine)]
  657. [ResourceConsumption(ResourceScope.Machine)]
  658. public static void SetAccessControl(String path, FileSecurity fileSecurity)
  659. {
  660. if (fileSecurity == null)
  661. throw new ArgumentNullException("fileSecurity");
  662. Contract.EndContractBlock();
  663. String fullPath = Path.GetFullPathInternal(path);
  664. // Appropriate security check should be done for us by FileSecurity.
  665. fileSecurity.Persist(fullPath);
  666. }
  667. #endif
  668. #if FEATURE_LEGACYNETCF
  669. [System.Security.SecuritySafeCritical]
  670. #endif // FEATURE_LEGACYNETCF
  671. [ResourceExposure(ResourceScope.Machine)]
  672. [ResourceConsumption(ResourceScope.Machine)]
  673. public static FileStream OpenRead(String path) {
  674. #if FEATURE_LEGACYNETCF
  675. if(CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) {
  676. System.Reflection.Assembly callingAssembly = System.Reflection.Assembly.GetCallingAssembly();
  677. if(callingAssembly != null && !callingAssembly.IsProfileAssembly) {
  678. string caller = new System.Diagnostics.StackFrame(1).GetMethod().FullName;
  679. string callee = System.Reflection.MethodBase.GetCurrentMethod().FullName;
  680. throw new MethodAccessException(String.Format(
  681. CultureInfo.CurrentCulture,
  682. Environment.GetResourceString("Arg_MethodAccessException_WithCaller"),
  683. caller,
  684. callee));
  685. }
  686. }
  687. #endif // FEATURE_LEGACYNETCF
  688. return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
  689. }
  690. [ResourceExposure(ResourceScope.Machine)]
  691. [ResourceConsumption(ResourceScope.Machine)]
  692. public static FileStream OpenWrite(String path) {
  693. return new FileStream(path, FileMode.OpenOrCreate,
  694. FileAccess.Write, FileShare.None);
  695. }
  696. [System.Security.SecuritySafeCritical] // auto-generated
  697. [ResourceExposure(ResourceScope.Machine)]
  698. [ResourceConsumption(ResourceScope.Machine)]
  699. public static String ReadAllText(String path)
  700. {
  701. if (path == null)
  702. throw new ArgumentNullException("path");
  703. if (path.Length == 0)
  704. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  705. Contract.EndContractBlock();
  706. return InternalReadAllText(path, Encoding.UTF8, true);
  707. }
  708. [System.Security.SecuritySafeCritical] // auto-generated
  709. [ResourceExposure(ResourceScope.Machine)]
  710. [ResourceConsumption(ResourceScope.Machine)]
  711. public static String ReadAllText(String path, Encoding encoding)
  712. {
  713. if (path == null)
  714. throw new ArgumentNullException("path");
  715. if (encoding == null)
  716. throw new ArgumentNullException("encoding");
  717. if (path.Length == 0)
  718. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  719. Contract.EndContractBlock();
  720. return InternalReadAllText(path, encoding, true);
  721. }
  722. [System.Security.SecurityCritical]
  723. [ResourceExposure(ResourceScope.Machine)]
  724. [ResourceConsumption(ResourceScope.Machine)]
  725. internal static String UnsafeReadAllText(String path)
  726. {
  727. if (path == null)
  728. throw new ArgumentNullException("path");
  729. if (path.Length == 0)
  730. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  731. Contract.EndContractBlock();
  732. return InternalReadAllText(path, Encoding.UTF8, false);
  733. }
  734. [System.Security.SecurityCritical]
  735. [ResourceExposure(ResourceScope.Machine)]
  736. [ResourceConsumption(ResourceScope.Machine)]
  737. private static String InternalReadAllText(String path, Encoding encoding, bool checkHost)
  738. {
  739. Contract.Requires(path != null);
  740. Contract.Requires(encoding != null);
  741. Contract.Requires(path.Length > 0);
  742. using (StreamReader sr = new StreamReader(path, encoding, true, StreamReader.DefaultBufferSize, checkHost))
  743. return sr.ReadToEnd();
  744. }
  745. [System.Security.SecuritySafeCritical] // auto-generated
  746. [ResourceExposure(ResourceScope.Machine)]
  747. [ResourceConsumption(ResourceScope.Machine)]
  748. public static void WriteAllText(String path, String contents)
  749. {
  750. if (path == null)
  751. throw new ArgumentNullException("path");
  752. if (path.Length == 0)
  753. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  754. Contract.EndContractBlock();
  755. InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM, true);
  756. }
  757. [System.Security.SecuritySafeCritical] // auto-generated
  758. [ResourceExposure(ResourceScope.Machine)]
  759. [ResourceConsumption(ResourceScope.Machine)]
  760. public static void WriteAllText(String path, String contents, Encoding encoding)
  761. {
  762. if (path == null)
  763. throw new ArgumentNullException("path");
  764. if (encoding == null)
  765. throw new ArgumentNullException("encoding");
  766. if (path.Length == 0)
  767. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  768. Contract.EndContractBlock();
  769. InternalWriteAllText(path, contents, encoding, true);
  770. }
  771. [System.Security.SecurityCritical]
  772. [ResourceExposure(ResourceScope.Machine)]
  773. [ResourceConsumption(ResourceScope.Machine)]
  774. internal static void UnsafeWriteAllText(String path, String contents)
  775. {
  776. if (path == null)
  777. throw new ArgumentNullException("path");
  778. if (path.Length == 0)
  779. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  780. Contract.EndContractBlock();
  781. InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM, false);
  782. }
  783. [System.Security.SecurityCritical]
  784. [ResourceExposure(ResourceScope.Machine)]
  785. [ResourceConsumption(ResourceScope.Machine)]
  786. private static void InternalWriteAllText(String path, String contents, Encoding encoding, bool checkHost)
  787. {
  788. Contract.Requires(path != null);
  789. Contract.Requires(encoding != null);
  790. Contract.Requires(path.Length > 0);
  791. using (StreamWriter sw = new StreamWriter(path, false, encoding, StreamWriter.DefaultBufferSize, checkHost))
  792. sw.Write(contents);
  793. }
  794. [System.Security.SecuritySafeCritical] // auto-generated
  795. [ResourceExposure(ResourceScope.Machine)]
  796. [ResourceConsumption(ResourceScope.Machine)]
  797. public static byte[] ReadAllBytes(String path)
  798. {
  799. return InternalReadAllBytes(path, true);
  800. }
  801. [System.Security.SecurityCritical]
  802. [ResourceExposure(ResourceScope.Machine)]
  803. [ResourceConsumption(ResourceScope.Machine)]
  804. internal static byte[] UnsafeReadAllBytes(String path)
  805. {
  806. return InternalReadAllBytes(path, false);
  807. }
  808. [System.Security.SecurityCritical]
  809. [ResourceExposure(ResourceScope.Machine)]
  810. [ResourceConsumption(ResourceScope.Machine)]
  811. private static byte[] InternalReadAllBytes(String path, bool checkHost)
  812. {
  813. byte[] bytes;
  814. using(FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read,
  815. FileStream.DefaultBufferSize, FileOptions.None, Path.GetFileName(path), false, false, checkHost)) {
  816. // Do a blocking read
  817. int index = 0;
  818. long fileLength = fs.Length;
  819. if (fileLength > Int32.MaxValue)
  820. throw new IOException(Environment.GetResourceString("IO.IO_FileTooLong2GB"));
  821. int count = (int) fileLength;
  822. bytes = new byte[count];
  823. while(count > 0) {
  824. int n = fs.Read(bytes, index, count);
  825. if (n == 0)
  826. __Error.EndOfFile();
  827. index += n;
  828. count -= n;
  829. }
  830. }
  831. return bytes;
  832. }
  833. [System.Security.SecuritySafeCritical] // auto-generated
  834. [ResourceExposure(ResourceScope.Machine)]
  835. [ResourceConsumption(ResourceScope.Machine)]
  836. public static void WriteAllBytes(String path, byte[] bytes)
  837. {
  838. if (path == null)
  839. throw new ArgumentNullException("path", Environment.GetResourceString("ArgumentNull_Path"));
  840. if (path.Length == 0)
  841. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  842. if (bytes == null)
  843. throw new ArgumentNullException("bytes");
  844. Contract.EndContractBlock();
  845. InternalWriteAllBytes(path, bytes, true);
  846. }
  847. [System.Security.SecurityCritical]
  848. [ResourceExposure(ResourceScope.Machine)]
  849. [ResourceConsumption(ResourceScope.Machine)]
  850. internal static void UnsafeWriteAllBytes(String path, byte[] bytes)
  851. {
  852. if (path == null)
  853. throw new ArgumentNullException("path", Environment.GetResourceString("ArgumentNull_Path"));
  854. if (path.Length == 0)
  855. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  856. if (bytes == null)
  857. throw new ArgumentNullException("bytes");
  858. Contract.EndContractBlock();
  859. InternalWriteAllBytes(path, bytes, false);
  860. }
  861. [System.Security.SecurityCritical]
  862. [ResourceExposure(ResourceScope.Machine)]
  863. [ResourceConsumption(ResourceScope.Machine)]
  864. private static void InternalWriteAllBytes(String path, byte[] bytes, bool checkHost)
  865. {
  866. Contract.Requires(path != null);
  867. Contract.Requires(path.Length != 0);
  868. Contract.Requires(bytes != null);
  869. using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read,
  870. FileStream.DefaultBufferSize, FileOptions.None, Path.GetFileName(path), false, false, checkHost))
  871. {
  872. fs.Write(bytes, 0, bytes.Length);
  873. }
  874. }
  875. [ResourceExposure(ResourceScope.Machine)]
  876. [ResourceConsumption(ResourceScope.Machine)]
  877. public static String[] ReadAllLines(String path)
  878. {
  879. if (path == null)
  880. throw new ArgumentNullException("path");
  881. if (path.Length == 0)
  882. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  883. Contract.EndContractBlock();
  884. return InternalReadAllLines(path, Encoding.UTF8);
  885. }
  886. [ResourceExposure(ResourceScope.Machine)]
  887. [ResourceConsumption(ResourceScope.Machine)]
  888. public static String[] ReadAllLines(String path, Encoding encoding)
  889. {
  890. if (path == null)
  891. throw new ArgumentNullException("path");
  892. if (encoding == null)
  893. throw new ArgumentNullException("encoding");
  894. if (path.Length == 0)
  895. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  896. Contract.EndContractBlock();
  897. return InternalReadAllLines(path, encoding);
  898. }
  899. [ResourceExposure(ResourceScope.Machine)]
  900. [ResourceConsumption(ResourceScope.Machine)]
  901. private static String[] InternalReadAllLines(String path, Encoding encoding)
  902. {
  903. Contract.Requires(path != null);
  904. Contract.Requires(encoding != null);
  905. Contract.Requires(path.Length != 0);
  906. String line;
  907. List<String> lines = new List<String>();
  908. using (StreamReader sr = new StreamReader(path, encoding))
  909. while ((line = sr.ReadLine()) != null)
  910. lines.Add(line);
  911. return lines.ToArray();
  912. }
  913. [ResourceExposure(ResourceScope.Machine)]
  914. [ResourceConsumption(ResourceScope.Machine)]
  915. public static IEnumerable<String> ReadLines(String path)
  916. {
  917. if (path == null)
  918. throw new ArgumentNullException("path");
  919. if (path.Length == 0)
  920. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"), "path");
  921. Contract.EndContractBlock();
  922. return ReadLinesIterator.CreateIterator(path, Encoding.UTF8);
  923. }
  924. [ResourceExposure(ResourceScope.Machine)]
  925. [ResourceConsumption(ResourceScope.Machine)]
  926. public static IEnumerable<String> ReadLines(String path, Encoding encoding)
  927. {
  928. if (path == null)
  929. throw new ArgumentNullException("path");
  930. if (encoding == null)
  931. throw new ArgumentNullException("encoding");
  932. if (path.Length == 0)
  933. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"), "path");
  934. Contract.EndContractBlock();
  935. return ReadLinesIterator.CreateIterator(path, encoding);
  936. }
  937. [ResourceExposure(ResourceScope.Machine)]
  938. [ResourceConsumption(ResourceScope.Machine)]
  939. public static void WriteAllLines(String path, String[] contents)
  940. {
  941. if (path == null)
  942. throw new ArgumentNullException("path");
  943. if (contents == null)
  944. throw new ArgumentNullException("contents");
  945. if (path.Length == 0)
  946. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  947. Contract.EndContractBlock();
  948. InternalWriteAllLines(new StreamWriter(path, false, StreamWriter.UTF8NoBOM), contents);
  949. }
  950. [ResourceExposure(ResourceScope.Machine)]
  951. [ResourceConsumption(ResourceScope.Machine)]
  952. public static void WriteAllLines(String path, String[] contents, Encoding encoding)
  953. {
  954. if (path == null)
  955. throw new ArgumentNullException("path");
  956. if (contents == null)
  957. throw new ArgumentNullException("contents");
  958. if (encoding == null)
  959. throw new ArgumentNullException("encoding");
  960. if (path.Length == 0)
  961. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  962. Contract.EndContractBlock();
  963. InternalWriteAllLines(new StreamWriter(path, false, encoding), contents);
  964. }
  965. [ResourceExposure(ResourceScope.Machine)]
  966. [ResourceConsumption(ResourceScope.Machine)]
  967. public static void WriteAllLines(String path, IEnumerable<String> contents)
  968. {
  969. if (path == null)
  970. throw new ArgumentNullException("path");
  971. if (contents == null)
  972. throw new ArgumentNullException("contents");
  973. if (path.Length == 0)
  974. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  975. Contract.EndContractBlock();
  976. InternalWriteAllLines(new StreamWriter(path, false, StreamWriter.UTF8NoBOM), contents);
  977. }
  978. [ResourceExposure(ResourceScope.Machine)]
  979. [ResourceConsumption(ResourceScope.Machine)]
  980. public static void WriteAllLines(String path, IEnumerable<String> contents, Encoding encoding)
  981. {
  982. if (path == null)
  983. throw new ArgumentNullException("path");
  984. if (contents == null)
  985. throw new ArgumentNullException("contents");
  986. if (encoding == null)
  987. throw new ArgumentNullException("encoding");
  988. if (path.Length == 0)
  989. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  990. Contract.EndContractBlock();
  991. InternalWriteAllLines(new StreamWriter(path, false, encoding), contents);
  992. }
  993. [ResourceExposure(ResourceScope.Machine)]
  994. [ResourceConsumption(ResourceScope.Machine)]
  995. private static void InternalWriteAllLines(TextWriter writer, IEnumerable<String> contents)
  996. {
  997. Contract.Requires(writer != null);
  998. Contract.Requires(contents != null);
  999. using (writer)
  1000. {
  1001. foreach (String line in contents)
  1002. {
  1003. writer.WriteLine(line);
  1004. }
  1005. }
  1006. }
  1007. [ResourceExposure(ResourceScope.Machine)]
  1008. [ResourceConsumption(ResourceScope.Machine)]
  1009. public static void AppendAllText(String path, String contents)
  1010. {
  1011. if (path == null)
  1012. throw new ArgumentNullException("path");
  1013. if (path.Length == 0)
  1014. throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
  1015. Contract.EndContractBlock();
  1016. InternalAppendAllText(p