File.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. //
  2. // System.IO.File.cs
  3. //
  4. //
  5. // Authors:
  6. // Miguel de Icaza ([email protected])
  7. // Jim Richardson ([email protected])
  8. // Dan Lewis ([email protected])
  9. // Ville Palo ([email protected])
  10. //
  11. // Copyright 2002 Ximian, Inc. http://www.ximian.com
  12. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  13. // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Text;
  36. using System.Collections.Generic;
  37. using System.Runtime.InteropServices;
  38. #if !NET_2_1
  39. using System.Security.AccessControl;
  40. #endif
  41. namespace System.IO
  42. {
  43. [ComVisible (true)]
  44. public static class File
  45. {
  46. public static void AppendAllText (string path, string contents)
  47. {
  48. using (TextWriter w = new StreamWriter (path, true)) {
  49. w.Write (contents);
  50. }
  51. }
  52. public static void AppendAllText (string path, string contents, Encoding encoding)
  53. {
  54. using (TextWriter w = new StreamWriter (path, true, encoding)) {
  55. w.Write (contents);
  56. }
  57. }
  58. public static StreamWriter AppendText (string path)
  59. {
  60. return new StreamWriter (path, true);
  61. }
  62. public static void Copy (string sourceFileName, string destFileName)
  63. {
  64. Copy (sourceFileName, destFileName, false);
  65. }
  66. public static void Copy (string sourceFileName, string destFileName, bool overwrite)
  67. {
  68. MonoIOError error;
  69. if (sourceFileName == null)
  70. throw new ArgumentNullException ("sourceFileName");
  71. if (destFileName == null)
  72. throw new ArgumentNullException ("destFileName");
  73. if (sourceFileName.Length == 0)
  74. throw new ArgumentException ("An empty file name is not valid.", "sourceFileName");
  75. if (sourceFileName.Trim ().Length == 0 || sourceFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  76. throw new ArgumentException ("The file name is not valid.");
  77. if (destFileName.Length == 0)
  78. throw new ArgumentException ("An empty file name is not valid.", "destFileName");
  79. if (destFileName.Trim ().Length == 0 || destFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  80. throw new ArgumentException ("The file name is not valid.");
  81. if (!MonoIO.Exists (sourceFileName, out error))
  82. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", sourceFileName), sourceFileName);
  83. if ((GetAttributes (sourceFileName) & FileAttributes.Directory) == FileAttributes.Directory)
  84. throw new ArgumentException (Locale.GetText ("{0} is a directory", sourceFileName));
  85. if (MonoIO.Exists (destFileName, out error)) {
  86. if ((GetAttributes (destFileName) & FileAttributes.Directory) == FileAttributes.Directory)
  87. throw new ArgumentException (Locale.GetText ("{0} is a directory", destFileName));
  88. if (!overwrite)
  89. throw new IOException (Locale.GetText ("{0} already exists", destFileName));
  90. }
  91. string DirName = Path.GetDirectoryName (destFileName);
  92. if (DirName != String.Empty && !Directory.Exists (DirName))
  93. throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}",DirName));
  94. if (!MonoIO.CopyFile (sourceFileName, destFileName, overwrite, out error)) {
  95. string p = Locale.GetText ("{0}\" or \"{1}", sourceFileName, destFileName);
  96. throw MonoIO.GetException (p, error);
  97. }
  98. }
  99. public static FileStream Create (string path)
  100. {
  101. return Create (path, 8192);
  102. }
  103. public static FileStream Create (string path, int bufferSize)
  104. {
  105. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  106. FileShare.None, bufferSize);
  107. }
  108. #if !NET_2_1
  109. [MonoLimitation ("FileOptions are ignored")]
  110. public static FileStream Create (string path, int bufferSize,
  111. FileOptions options)
  112. {
  113. return Create (path, bufferSize, options, null);
  114. }
  115. [MonoLimitation ("FileOptions and FileSecurity are ignored")]
  116. public static FileStream Create (string path, int bufferSize,
  117. FileOptions options,
  118. FileSecurity fileSecurity)
  119. {
  120. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  121. FileShare.None, bufferSize, options);
  122. }
  123. #endif
  124. public static StreamWriter CreateText (string path)
  125. {
  126. return new StreamWriter (path, false);
  127. }
  128. public static void Delete (string path)
  129. {
  130. if (path == null)
  131. throw new ArgumentNullException("path");
  132. if (path.Trim().Length == 0 || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  133. throw new ArgumentException("path");
  134. if (Directory.Exists (path))
  135. throw new UnauthorizedAccessException(Locale.GetText ("{0} is a directory", path));
  136. string DirName = Path.GetDirectoryName(path);
  137. if (DirName != String.Empty && !Directory.Exists (DirName))
  138. throw new DirectoryNotFoundException (Locale.GetText ("Could not find a part of the path \"{0}\".", path));
  139. MonoIOError error;
  140. if (!MonoIO.DeleteFile (path, out error)){
  141. if (error != MonoIOError.ERROR_FILE_NOT_FOUND)
  142. throw MonoIO.GetException (path, error);
  143. }
  144. }
  145. public static bool Exists (string path)
  146. {
  147. // For security reasons no exceptions are
  148. // thrown, only false is returned if there is
  149. // any problem with the path or permissions.
  150. // Minimizes what information can be
  151. // discovered by using this method.
  152. if (path == null || path.Trim().Length == 0
  153. || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
  154. return false;
  155. }
  156. MonoIOError error;
  157. return MonoIO.ExistsFile (path, out error);
  158. }
  159. #if !NET_2_1
  160. public static FileSecurity GetAccessControl (string path)
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. public static FileSecurity GetAccessControl (string path, AccessControlSections includeSections)
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. #endif
  169. public static FileAttributes GetAttributes (string path)
  170. {
  171. if (path == null)
  172. throw new ArgumentNullException("path");
  173. if (path.Trim ().Length == 0)
  174. throw new ArgumentException (Locale.GetText ("Path is empty"));
  175. if (path.IndexOfAny (Path.InvalidPathChars) >= 0)
  176. throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
  177. MonoIOError error;
  178. FileAttributes attrs;
  179. attrs = MonoIO.GetFileAttributes (path, out error);
  180. if (error != MonoIOError.ERROR_SUCCESS)
  181. throw MonoIO.GetException (path, error);
  182. return attrs;
  183. }
  184. public static DateTime GetCreationTime (string path)
  185. {
  186. MonoIOStat stat;
  187. MonoIOError error;
  188. CheckPathExceptions (path);
  189. if (!MonoIO.GetFileStat (path, out stat, out error)) {
  190. if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
  191. return DefaultLocalFileTime;
  192. else
  193. throw new IOException (path);
  194. }
  195. return DateTime.FromFileTime (stat.CreationTime);
  196. }
  197. public static DateTime GetCreationTimeUtc (string path)
  198. {
  199. return GetCreationTime (path).ToUniversalTime ();
  200. }
  201. public static DateTime GetLastAccessTime (string path)
  202. {
  203. MonoIOStat stat;
  204. MonoIOError error;
  205. CheckPathExceptions (path);
  206. if (!MonoIO.GetFileStat (path, out stat, out error)) {
  207. if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
  208. return DefaultLocalFileTime;
  209. else
  210. throw new IOException (path);
  211. }
  212. return DateTime.FromFileTime (stat.LastAccessTime);
  213. }
  214. public static DateTime GetLastAccessTimeUtc (string path)
  215. {
  216. return GetLastAccessTime (path).ToUniversalTime ();
  217. }
  218. public static DateTime GetLastWriteTime (string path)
  219. {
  220. MonoIOStat stat;
  221. MonoIOError error;
  222. CheckPathExceptions (path);
  223. if (!MonoIO.GetFileStat (path, out stat, out error)) {
  224. if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
  225. return DefaultLocalFileTime;
  226. else
  227. throw new IOException (path);
  228. }
  229. return DateTime.FromFileTime (stat.LastWriteTime);
  230. }
  231. public static DateTime GetLastWriteTimeUtc (string path)
  232. {
  233. return GetLastWriteTime (path).ToUniversalTime ();
  234. }
  235. public static void Move (string sourceFileName, string destFileName)
  236. {
  237. MonoIOError error;
  238. if (sourceFileName == null)
  239. throw new ArgumentNullException ("sourceFileName");
  240. if (destFileName == null)
  241. throw new ArgumentNullException ("destFileName");
  242. if (sourceFileName.Length == 0)
  243. throw new ArgumentException ("An empty file name is not valid.", "sourceFileName");
  244. if (sourceFileName.Trim ().Length == 0 || sourceFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  245. throw new ArgumentException ("The file name is not valid.");
  246. if (destFileName.Length == 0)
  247. throw new ArgumentException ("An empty file name is not valid.", "destFileName");
  248. if (destFileName.Trim ().Length == 0 || destFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  249. throw new ArgumentException ("The file name is not valid.");
  250. if (!MonoIO.Exists (sourceFileName, out error))
  251. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", sourceFileName), sourceFileName);
  252. // Don't check for this error here to allow the runtime
  253. // to check if sourceFileName and destFileName are equal.
  254. // Comparing sourceFileName and destFileName is not enough.
  255. //if (MonoIO.Exists (destFileName, out error))
  256. // throw new IOException (Locale.GetText ("{0} already exists", destFileName));
  257. string DirName;
  258. DirName = Path.GetDirectoryName (destFileName);
  259. if (DirName != String.Empty && !Directory.Exists (DirName))
  260. throw new DirectoryNotFoundException (Locale.GetText ("Could not find a part of the path."));
  261. if (!MonoIO.MoveFile (sourceFileName, destFileName, out error)) {
  262. if (error == MonoIOError.ERROR_ALREADY_EXISTS)
  263. throw MonoIO.GetException (error);
  264. else if (error == MonoIOError.ERROR_SHARING_VIOLATION)
  265. throw MonoIO.GetException (sourceFileName, error);
  266. throw MonoIO.GetException (error);
  267. }
  268. }
  269. public static FileStream Open (string path, FileMode mode)
  270. {
  271. return new FileStream (path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None);
  272. }
  273. public static FileStream Open (string path, FileMode mode, FileAccess access)
  274. {
  275. return new FileStream (path, mode, access, FileShare.None);
  276. }
  277. public static FileStream Open (string path, FileMode mode, FileAccess access,
  278. FileShare share)
  279. {
  280. return new FileStream (path, mode, access, share);
  281. }
  282. public static FileStream OpenRead (string path)
  283. {
  284. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  285. }
  286. public static StreamReader OpenText (string path)
  287. {
  288. return new StreamReader (path);
  289. }
  290. public static FileStream OpenWrite (string path)
  291. {
  292. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  293. }
  294. public static void Replace (string sourceFileName,
  295. string destinationFileName,
  296. string destinationBackupFileName)
  297. {
  298. Replace (sourceFileName, destinationFileName, destinationBackupFileName, false);
  299. }
  300. public static void Replace (string sourceFileName,
  301. string destinationFileName,
  302. string destinationBackupFileName,
  303. bool ignoreMetadataErrors)
  304. {
  305. MonoIOError error;
  306. if (sourceFileName == null)
  307. throw new ArgumentNullException ("sourceFileName");
  308. if (destinationFileName == null)
  309. throw new ArgumentNullException ("destinationFileName");
  310. if (sourceFileName.Trim ().Length == 0 || sourceFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  311. throw new ArgumentException ("sourceFileName");
  312. if (destinationFileName.Trim ().Length == 0 || destinationFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  313. throw new ArgumentException ("destinationFileName");
  314. string fullSource = Path.GetFullPath (sourceFileName);
  315. string fullDest = Path.GetFullPath (destinationFileName);
  316. if (MonoIO.ExistsDirectory (fullSource, out error))
  317. throw new IOException (Locale.GetText ("{0} is a directory", sourceFileName));
  318. if (MonoIO.ExistsDirectory (fullDest, out error))
  319. throw new IOException (Locale.GetText ("{0} is a directory", destinationFileName));
  320. if (!Exists (fullSource))
  321. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", sourceFileName),
  322. sourceFileName);
  323. if (!Exists (fullDest))
  324. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", destinationFileName),
  325. destinationFileName);
  326. if (fullSource == fullDest)
  327. throw new IOException (Locale.GetText ("Source and destination arguments are the same file."));
  328. string fullBackup = null;
  329. if (destinationBackupFileName != null) {
  330. if (destinationBackupFileName.Trim ().Length == 0 ||
  331. destinationBackupFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  332. throw new ArgumentException ("destinationBackupFileName");
  333. fullBackup = Path.GetFullPath (destinationBackupFileName);
  334. if (MonoIO.ExistsDirectory (fullBackup, out error))
  335. throw new IOException (Locale.GetText ("{0} is a directory", destinationBackupFileName));
  336. if (fullSource == fullBackup)
  337. throw new IOException (Locale.GetText ("Source and backup arguments are the same file."));
  338. if (fullDest == fullBackup)
  339. throw new IOException (Locale.GetText (
  340. "Destination and backup arguments are the same file."));
  341. }
  342. if (!MonoIO.ReplaceFile (fullSource, fullDest, fullBackup,
  343. ignoreMetadataErrors, out error)) {
  344. throw MonoIO.GetException (error);
  345. }
  346. }
  347. #if !NET_2_1
  348. public static void SetAccessControl (string path,
  349. FileSecurity fileSecurity)
  350. {
  351. throw new NotImplementedException ();
  352. }
  353. #endif
  354. public static void SetAttributes (string path,
  355. FileAttributes fileAttributes)
  356. {
  357. MonoIOError error;
  358. CheckPathExceptions (path);
  359. if (!MonoIO.SetFileAttributes (path, fileAttributes, out error))
  360. throw MonoIO.GetException (path, error);
  361. }
  362. public static void SetCreationTime (string path, DateTime creationTime)
  363. {
  364. MonoIOError error;
  365. CheckPathExceptions (path);
  366. if (!MonoIO.Exists (path, out error))
  367. throw MonoIO.GetException (path, error);
  368. if (!MonoIO.SetCreationTime (path, creationTime, out error))
  369. throw MonoIO.GetException (path, error);
  370. }
  371. public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc)
  372. {
  373. SetCreationTime (path, creationTimeUtc.ToLocalTime ());
  374. }
  375. public static void SetLastAccessTime (string path, DateTime lastAccessTime)
  376. {
  377. MonoIOError error;
  378. CheckPathExceptions (path);
  379. if (!MonoIO.Exists (path, out error))
  380. throw MonoIO.GetException (path, error);
  381. if (!MonoIO.SetLastAccessTime (path, lastAccessTime, out error))
  382. throw MonoIO.GetException (path, error);
  383. }
  384. public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc)
  385. {
  386. SetLastAccessTime (path, lastAccessTimeUtc.ToLocalTime ());
  387. }
  388. public static void SetLastWriteTime (string path,
  389. DateTime lastWriteTime)
  390. {
  391. MonoIOError error;
  392. CheckPathExceptions (path);
  393. if (!MonoIO.Exists (path, out error))
  394. throw MonoIO.GetException (path, error);
  395. if (!MonoIO.SetLastWriteTime (path, lastWriteTime, out error))
  396. throw MonoIO.GetException (path, error);
  397. }
  398. public static void SetLastWriteTimeUtc (string path,
  399. DateTime lastWriteTimeUtc)
  400. {
  401. SetLastWriteTime (path, lastWriteTimeUtc.ToLocalTime ());
  402. }
  403. #region Private
  404. private static void CheckPathExceptions (string path)
  405. {
  406. if (path == null)
  407. throw new System.ArgumentNullException("path");
  408. if (path.Length == 0)
  409. throw new System.ArgumentException(Locale.GetText ("Path is empty"));
  410. if (path.Trim().Length == 0)
  411. throw new ArgumentException (Locale.GetText ("Path is empty"));
  412. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  413. throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
  414. }
  415. #endregion
  416. //
  417. // The documentation for this method is most likely wrong, it
  418. // talks about doing a "binary read", but the remarks say
  419. // that this "detects the encoding".
  420. //
  421. // This can not detect and do anything useful with the encoding
  422. // since the result is a byte [] not a char [].
  423. //
  424. public static byte [] ReadAllBytes (string path)
  425. {
  426. using (FileStream s = Open (path, FileMode.Open, FileAccess.Read, FileShare.Read)){
  427. long size = s.Length;
  428. //
  429. // Is this worth supporting?
  430. //
  431. if (size > Int32.MaxValue)
  432. throw new ArgumentException ("Reading more than 4gigs with this call is not supported");
  433. byte [] result = new byte [s.Length];
  434. s.Read (result, 0, (int) size);
  435. return result;
  436. }
  437. }
  438. public static string [] ReadAllLines (string path)
  439. {
  440. using (StreamReader reader = File.OpenText (path)) {
  441. return ReadAllLines (reader);
  442. }
  443. }
  444. public static string [] ReadAllLines (string path, Encoding encoding)
  445. {
  446. using (StreamReader reader = new StreamReader (path, encoding)) {
  447. return ReadAllLines (reader);
  448. }
  449. }
  450. static string [] ReadAllLines (StreamReader reader)
  451. {
  452. List<string> list = new List<string> ();
  453. while (!reader.EndOfStream)
  454. list.Add (reader.ReadLine ());
  455. return list.ToArray ();
  456. }
  457. public static string ReadAllText (string path)
  458. {
  459. return ReadAllText (path, Encoding.UTF8Unmarked);
  460. }
  461. public static string ReadAllText (string path, Encoding encoding)
  462. {
  463. using (StreamReader sr = new StreamReader (path, encoding)) {
  464. return sr.ReadToEnd ();
  465. }
  466. }
  467. public static void WriteAllBytes (string path, byte [] bytes)
  468. {
  469. using (Stream stream = File.Create (path)) {
  470. stream.Write (bytes, 0, bytes.Length);
  471. }
  472. }
  473. public static void WriteAllLines (string path, string [] contents)
  474. {
  475. using (StreamWriter writer = new StreamWriter (path)) {
  476. WriteAllLines (writer, contents);
  477. }
  478. }
  479. public static void WriteAllLines (string path, string [] contents, Encoding encoding)
  480. {
  481. using (StreamWriter writer = new StreamWriter (path, false, encoding)) {
  482. WriteAllLines (writer, contents);
  483. }
  484. }
  485. static void WriteAllLines (StreamWriter writer, string [] contents)
  486. {
  487. foreach (string line in contents)
  488. writer.WriteLine (line);
  489. }
  490. public static void WriteAllText (string path, string contents)
  491. {
  492. WriteAllText (path, contents, Encoding.UTF8Unmarked);
  493. }
  494. public static void WriteAllText (string path, string contents, Encoding encoding)
  495. {
  496. using (StreamWriter sw = new StreamWriter (path, false, encoding)) {
  497. sw.Write (contents);
  498. }
  499. }
  500. static DateTime? defaultLocalFileTime;
  501. static DateTime DefaultLocalFileTime {
  502. get {
  503. if (defaultLocalFileTime == null)
  504. defaultLocalFileTime = new DateTime (1601, 1, 1).ToLocalTime ();
  505. return defaultLocalFileTime.Value;
  506. }
  507. }
  508. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  509. public static void Encrypt (string path)
  510. {
  511. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  512. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  513. // we throw the same (instead of a NotImplementedException) because most code should already be
  514. // handling this exception to work properly.
  515. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  516. }
  517. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  518. public static void Decrypt (string path)
  519. {
  520. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  521. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  522. // we throw the same (instead of a NotImplementedException) because most code should already be
  523. // handling this exception to work properly.
  524. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  525. }
  526. #if NET_4_0
  527. public static IEnumerable<string> ReadLines (string path)
  528. {
  529. if (path == null)
  530. throw new ArgumentNullException ("path");
  531. if (path.Length == 0)
  532. throw new ArgumentException ("path");
  533. using (StreamReader reader = File.OpenText (path)) {
  534. string s;
  535. while ((s = reader.ReadLine ()) != null)
  536. yield return s;
  537. }
  538. }
  539. public static IEnumerable<string> ReadLines (string path, Encoding encoding)
  540. {
  541. if (path == null)
  542. throw new ArgumentNullException ("path");
  543. if (path.Length == 0)
  544. throw new ArgumentException ("path");
  545. using (StreamReader reader = new StreamReader (path, encoding)) {
  546. string s;
  547. while ((s = reader.ReadLine ()) != null)
  548. yield return s;
  549. }
  550. }
  551. public static void AppendAllLines (string path, IEnumerable<string> contents)
  552. {
  553. if (path == null)
  554. throw new ArgumentNullException ("path");
  555. if (path.Length == 0)
  556. throw new ArgumentException ("path");
  557. if (contents == null)
  558. return;
  559. using (TextWriter w = new StreamWriter (path, true)) {
  560. foreach (var line in contents)
  561. w.Write (line);
  562. }
  563. }
  564. public static void AppendAllLines (string path, IEnumerable<string> contents, Encoding encoding)
  565. {
  566. if (path == null)
  567. throw new ArgumentNullException ("path");
  568. if (path.Length == 0)
  569. throw new ArgumentException ("path");
  570. if (contents == null)
  571. return;
  572. using (TextWriter w = new StreamWriter (path, true, encoding)) {
  573. foreach (var line in contents)
  574. w.Write (line);
  575. }
  576. }
  577. public static void WriteAllLines (string path, IEnumerable<string> contents)
  578. {
  579. if (path == null)
  580. throw new ArgumentNullException ("path");
  581. if (path.Length == 0)
  582. throw new ArgumentException ("path");
  583. if (contents == null)
  584. return;
  585. using (TextWriter w = new StreamWriter (path, false)) {
  586. foreach (var line in contents)
  587. w.Write (line);
  588. }
  589. }
  590. public static void WriteAllLines (string path, IEnumerable<string> contents, Encoding encoding)
  591. {
  592. if (path == null)
  593. throw new ArgumentNullException ("path");
  594. if (path.Length == 0)
  595. throw new ArgumentException ("path");
  596. if (contents == null)
  597. return;
  598. using (TextWriter w = new StreamWriter (path, false, encoding)) {
  599. foreach (var line in contents)
  600. w.Write (line);
  601. }
  602. }
  603. #endif
  604. }
  605. }