File.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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, 2010 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.Collections.Generic;
  36. using System.Diagnostics;
  37. using System.Security;
  38. using System.Text;
  39. using System.Runtime.InteropServices;
  40. #if !NET_2_1
  41. using System.Security.AccessControl;
  42. #endif
  43. namespace System.IO
  44. {
  45. [ComVisible (true)]
  46. public static class File
  47. {
  48. public static void AppendAllText (string path, string contents)
  49. {
  50. using (TextWriter w = new StreamWriter (path, true)) {
  51. w.Write (contents);
  52. }
  53. }
  54. public static void AppendAllText (string path, string contents, Encoding encoding)
  55. {
  56. using (TextWriter w = new StreamWriter (path, true, encoding)) {
  57. w.Write (contents);
  58. }
  59. }
  60. public static StreamWriter AppendText (string path)
  61. {
  62. return new StreamWriter (path, true);
  63. }
  64. public static void Copy (string sourceFileName, string destFileName)
  65. {
  66. Copy (sourceFileName, destFileName, false);
  67. }
  68. public static void Copy (string sourceFileName, string destFileName, bool overwrite)
  69. {
  70. MonoIOError error;
  71. if (sourceFileName == null)
  72. throw new ArgumentNullException ("sourceFileName");
  73. if (destFileName == null)
  74. throw new ArgumentNullException ("destFileName");
  75. if (sourceFileName.Length == 0)
  76. throw new ArgumentException ("An empty file name is not valid.", "sourceFileName");
  77. if (sourceFileName.Trim ().Length == 0 || sourceFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  78. throw new ArgumentException ("The file name is not valid.");
  79. if (destFileName.Length == 0)
  80. throw new ArgumentException ("An empty file name is not valid.", "destFileName");
  81. if (destFileName.Trim ().Length == 0 || destFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  82. throw new ArgumentException ("The file name is not valid.");
  83. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  84. if (!MonoIO.Exists (sourceFileName, out error))
  85. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", sourceFileName), sourceFileName);
  86. if ((GetAttributes (sourceFileName) & FileAttributes.Directory) == FileAttributes.Directory)
  87. throw new ArgumentException (Locale.GetText ("{0} is a directory", sourceFileName));
  88. if (MonoIO.Exists (destFileName, out error)) {
  89. if ((GetAttributes (destFileName) & FileAttributes.Directory) == FileAttributes.Directory)
  90. throw new ArgumentException (Locale.GetText ("{0} is a directory", destFileName));
  91. if (!overwrite)
  92. throw new IOException (Locale.GetText ("{0} already exists", destFileName));
  93. }
  94. string DirName = Path.GetDirectoryName (destFileName);
  95. if (DirName != String.Empty && !Directory.Exists (DirName))
  96. throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}",DirName));
  97. if (!MonoIO.CopyFile (sourceFileName, destFileName, overwrite, out error)) {
  98. string p = Locale.GetText ("{0}\" or \"{1}", sourceFileName, destFileName);
  99. throw MonoIO.GetException (p, error);
  100. }
  101. }
  102. public static FileStream Create (string path)
  103. {
  104. return Create (path, 8192);
  105. }
  106. public static FileStream Create (string path, int bufferSize)
  107. {
  108. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  109. FileShare.None, bufferSize);
  110. }
  111. #if !NET_2_1
  112. [MonoLimitation ("FileOptions are ignored")]
  113. public static FileStream Create (string path, int bufferSize,
  114. FileOptions options)
  115. {
  116. return Create (path, bufferSize, options, null);
  117. }
  118. [MonoLimitation ("FileOptions and FileSecurity are ignored")]
  119. public static FileStream Create (string path, int bufferSize,
  120. FileOptions options,
  121. FileSecurity fileSecurity)
  122. {
  123. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  124. FileShare.None, bufferSize, options);
  125. }
  126. #endif
  127. public static StreamWriter CreateText (string path)
  128. {
  129. return new StreamWriter (path, false);
  130. }
  131. public static void Delete (string path)
  132. {
  133. Path.Validate (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. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  140. MonoIOError error;
  141. if (!MonoIO.DeleteFile (path, out error)){
  142. if (error != MonoIOError.ERROR_FILE_NOT_FOUND)
  143. throw MonoIO.GetException (path, error);
  144. }
  145. }
  146. public static bool Exists (string path)
  147. {
  148. // For security reasons no exceptions are
  149. // thrown, only false is returned if there is
  150. // any problem with the path or permissions.
  151. // Minimizes what information can be
  152. // discovered by using this method.
  153. if (String.IsNullOrWhiteSpace (path) || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  154. return false;
  155. // on Moonlight this does not throw but returns false
  156. if (!SecurityManager.CheckElevatedPermissions ())
  157. return false;
  158. MonoIOError error;
  159. return MonoIO.ExistsFile (path, out error);
  160. }
  161. #if !NET_2_1
  162. public static FileSecurity GetAccessControl (string path)
  163. {
  164. // AccessControlSections.Audit requires special permissions.
  165. return GetAccessControl (path,
  166. AccessControlSections.Owner |
  167. AccessControlSections.Group |
  168. AccessControlSections.Access);
  169. }
  170. public static FileSecurity GetAccessControl (string path, AccessControlSections includeSections)
  171. {
  172. return new FileSecurity (path, includeSections);
  173. }
  174. #endif
  175. public static FileAttributes GetAttributes (string path)
  176. {
  177. Path.Validate (path);
  178. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  179. MonoIOError error;
  180. FileAttributes attrs;
  181. attrs = MonoIO.GetFileAttributes (path, out error);
  182. if (error != MonoIOError.ERROR_SUCCESS)
  183. throw MonoIO.GetException (path, error);
  184. return attrs;
  185. }
  186. public static DateTime GetCreationTime (string path)
  187. {
  188. MonoIOStat stat;
  189. MonoIOError error;
  190. Path.Validate (path);
  191. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  192. if (!MonoIO.GetFileStat (path, out stat, out error)) {
  193. if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
  194. return DefaultLocalFileTime;
  195. else
  196. throw new IOException (path);
  197. }
  198. return DateTime.FromFileTime (stat.CreationTime);
  199. }
  200. public static DateTime GetCreationTimeUtc (string path)
  201. {
  202. return GetCreationTime (path).ToUniversalTime ();
  203. }
  204. public static DateTime GetLastAccessTime (string path)
  205. {
  206. MonoIOStat stat;
  207. MonoIOError error;
  208. Path.Validate (path);
  209. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  210. if (!MonoIO.GetFileStat (path, out stat, out error)) {
  211. if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
  212. return DefaultLocalFileTime;
  213. else
  214. throw new IOException (path);
  215. }
  216. return DateTime.FromFileTime (stat.LastAccessTime);
  217. }
  218. public static DateTime GetLastAccessTimeUtc (string path)
  219. {
  220. return GetLastAccessTime (path).ToUniversalTime ();
  221. }
  222. public static DateTime GetLastWriteTime (string path)
  223. {
  224. MonoIOStat stat;
  225. MonoIOError error;
  226. Path.Validate (path);
  227. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  228. if (!MonoIO.GetFileStat (path, out stat, out error)) {
  229. if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
  230. return DefaultLocalFileTime;
  231. else
  232. throw new IOException (path);
  233. }
  234. return DateTime.FromFileTime (stat.LastWriteTime);
  235. }
  236. public static DateTime GetLastWriteTimeUtc (string path)
  237. {
  238. return GetLastWriteTime (path).ToUniversalTime ();
  239. }
  240. public static void Move (string sourceFileName, string destFileName)
  241. {
  242. if (sourceFileName == null)
  243. throw new ArgumentNullException ("sourceFileName");
  244. if (destFileName == null)
  245. throw new ArgumentNullException ("destFileName");
  246. if (sourceFileName.Length == 0)
  247. throw new ArgumentException ("An empty file name is not valid.", "sourceFileName");
  248. if (sourceFileName.Trim ().Length == 0 || sourceFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  249. throw new ArgumentException ("The file name is not valid.");
  250. if (destFileName.Length == 0)
  251. throw new ArgumentException ("An empty file name is not valid.", "destFileName");
  252. if (destFileName.Trim ().Length == 0 || destFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  253. throw new ArgumentException ("The file name is not valid.");
  254. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  255. MonoIOError error;
  256. if (!MonoIO.Exists (sourceFileName, out error))
  257. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", sourceFileName), sourceFileName);
  258. // Don't check for this error here to allow the runtime
  259. // to check if sourceFileName and destFileName are equal.
  260. // Comparing sourceFileName and destFileName is not enough.
  261. //if (MonoIO.Exists (destFileName, out error))
  262. // throw new IOException (Locale.GetText ("{0} already exists", destFileName));
  263. string DirName;
  264. DirName = Path.GetDirectoryName (destFileName);
  265. if (DirName != String.Empty && !Directory.Exists (DirName))
  266. throw new DirectoryNotFoundException (Locale.GetText ("Could not find a part of the path."));
  267. if (!MonoIO.MoveFile (sourceFileName, destFileName, out error)) {
  268. if (error == MonoIOError.ERROR_ALREADY_EXISTS)
  269. throw MonoIO.GetException (error);
  270. else if (error == MonoIOError.ERROR_SHARING_VIOLATION)
  271. throw MonoIO.GetException (sourceFileName, error);
  272. throw MonoIO.GetException (error);
  273. }
  274. }
  275. public static FileStream Open (string path, FileMode mode)
  276. {
  277. return new FileStream (path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None);
  278. }
  279. public static FileStream Open (string path, FileMode mode, FileAccess access)
  280. {
  281. return new FileStream (path, mode, access, FileShare.None);
  282. }
  283. public static FileStream Open (string path, FileMode mode, FileAccess access,
  284. FileShare share)
  285. {
  286. return new FileStream (path, mode, access, share);
  287. }
  288. public static FileStream OpenRead (string path)
  289. {
  290. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  291. }
  292. public static StreamReader OpenText (string path)
  293. {
  294. return new StreamReader (path);
  295. }
  296. public static FileStream OpenWrite (string path)
  297. {
  298. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  299. }
  300. public static void Replace (string sourceFileName,
  301. string destinationFileName,
  302. string destinationBackupFileName)
  303. {
  304. Replace (sourceFileName, destinationFileName, destinationBackupFileName, false);
  305. }
  306. public static void Replace (string sourceFileName,
  307. string destinationFileName,
  308. string destinationBackupFileName,
  309. bool ignoreMetadataErrors)
  310. {
  311. MonoIOError error;
  312. if (sourceFileName == null)
  313. throw new ArgumentNullException ("sourceFileName");
  314. if (destinationFileName == null)
  315. throw new ArgumentNullException ("destinationFileName");
  316. if (sourceFileName.Trim ().Length == 0 || sourceFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  317. throw new ArgumentException ("sourceFileName");
  318. if (destinationFileName.Trim ().Length == 0 || destinationFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  319. throw new ArgumentException ("destinationFileName");
  320. string fullSource = Path.GetFullPath (sourceFileName);
  321. string fullDest = Path.GetFullPath (destinationFileName);
  322. if (MonoIO.ExistsDirectory (fullSource, out error))
  323. throw new IOException (Locale.GetText ("{0} is a directory", sourceFileName));
  324. if (MonoIO.ExistsDirectory (fullDest, out error))
  325. throw new IOException (Locale.GetText ("{0} is a directory", destinationFileName));
  326. if (!Exists (fullSource))
  327. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", sourceFileName),
  328. sourceFileName);
  329. if (!Exists (fullDest))
  330. throw new FileNotFoundException (Locale.GetText ("{0} does not exist", destinationFileName),
  331. destinationFileName);
  332. if (fullSource == fullDest)
  333. throw new IOException (Locale.GetText ("Source and destination arguments are the same file."));
  334. string fullBackup = null;
  335. if (destinationBackupFileName != null) {
  336. if (destinationBackupFileName.Trim ().Length == 0 ||
  337. destinationBackupFileName.IndexOfAny (Path.InvalidPathChars) != -1)
  338. throw new ArgumentException ("destinationBackupFileName");
  339. fullBackup = Path.GetFullPath (destinationBackupFileName);
  340. if (MonoIO.ExistsDirectory (fullBackup, out error))
  341. throw new IOException (Locale.GetText ("{0} is a directory", destinationBackupFileName));
  342. if (fullSource == fullBackup)
  343. throw new IOException (Locale.GetText ("Source and backup arguments are the same file."));
  344. if (fullDest == fullBackup)
  345. throw new IOException (Locale.GetText (
  346. "Destination and backup arguments are the same file."));
  347. }
  348. if (!MonoIO.ReplaceFile (fullSource, fullDest, fullBackup,
  349. ignoreMetadataErrors, out error)) {
  350. throw MonoIO.GetException (error);
  351. }
  352. }
  353. #if !NET_2_1
  354. public static void SetAccessControl (string path,
  355. FileSecurity fileSecurity)
  356. {
  357. if (null == fileSecurity)
  358. throw new ArgumentNullException ("fileSecurity");
  359. fileSecurity.PersistModifications (path);
  360. }
  361. #endif
  362. public static void SetAttributes (string path,
  363. FileAttributes fileAttributes)
  364. {
  365. MonoIOError error;
  366. Path.Validate (path);
  367. if (!MonoIO.SetFileAttributes (path, fileAttributes, out error))
  368. throw MonoIO.GetException (path, error);
  369. }
  370. public static void SetCreationTime (string path, DateTime creationTime)
  371. {
  372. MonoIOError error;
  373. Path.Validate (path);
  374. if (!MonoIO.Exists (path, out error))
  375. throw MonoIO.GetException (path, error);
  376. if (!MonoIO.SetCreationTime (path, creationTime, out error))
  377. throw MonoIO.GetException (path, error);
  378. }
  379. public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc)
  380. {
  381. SetCreationTime (path, creationTimeUtc.ToLocalTime ());
  382. }
  383. public static void SetLastAccessTime (string path, DateTime lastAccessTime)
  384. {
  385. MonoIOError error;
  386. Path.Validate (path);
  387. if (!MonoIO.Exists (path, out error))
  388. throw MonoIO.GetException (path, error);
  389. if (!MonoIO.SetLastAccessTime (path, lastAccessTime, out error))
  390. throw MonoIO.GetException (path, error);
  391. }
  392. public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc)
  393. {
  394. SetLastAccessTime (path, lastAccessTimeUtc.ToLocalTime ());
  395. }
  396. public static void SetLastWriteTime (string path,
  397. DateTime lastWriteTime)
  398. {
  399. MonoIOError error;
  400. Path.Validate (path);
  401. if (!MonoIO.Exists (path, out error))
  402. throw MonoIO.GetException (path, error);
  403. if (!MonoIO.SetLastWriteTime (path, lastWriteTime, out error))
  404. throw MonoIO.GetException (path, error);
  405. }
  406. public static void SetLastWriteTimeUtc (string path,
  407. DateTime lastWriteTimeUtc)
  408. {
  409. SetLastWriteTime (path, lastWriteTimeUtc.ToLocalTime ());
  410. }
  411. //
  412. // The documentation for this method is most likely wrong, it
  413. // talks about doing a "binary read", but the remarks say
  414. // that this "detects the encoding".
  415. //
  416. // This can not detect and do anything useful with the encoding
  417. // since the result is a byte [] not a char [].
  418. //
  419. public static byte [] ReadAllBytes (string path)
  420. {
  421. using (FileStream s = OpenRead (path)) {
  422. long size = s.Length;
  423. // limited to 2GB according to http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx
  424. if (size > Int32.MaxValue)
  425. throw new IOException ("Reading more than 2GB with this call is not supported");
  426. int pos = 0;
  427. int count = (int) size;
  428. byte [] result = new byte [size];
  429. while (count > 0) {
  430. int n = s.Read (result, pos, count);
  431. if (n == 0)
  432. throw new IOException ("Unexpected end of stream");
  433. pos += n;
  434. count -= n;
  435. }
  436. return result;
  437. }
  438. }
  439. public static string [] ReadAllLines (string path)
  440. {
  441. using (StreamReader reader = File.OpenText (path)) {
  442. return ReadAllLines (reader);
  443. }
  444. }
  445. public static string [] ReadAllLines (string path, Encoding encoding)
  446. {
  447. using (StreamReader reader = new StreamReader (path, encoding)) {
  448. return ReadAllLines (reader);
  449. }
  450. }
  451. static string [] ReadAllLines (StreamReader reader)
  452. {
  453. List<string> list = new List<string> ();
  454. while (!reader.EndOfStream)
  455. list.Add (reader.ReadLine ());
  456. return list.ToArray ();
  457. }
  458. public static string ReadAllText (string path)
  459. {
  460. using (StreamReader sr = new StreamReader (path)) {
  461. return sr.ReadToEnd ();
  462. }
  463. }
  464. public static string ReadAllText (string path, Encoding encoding)
  465. {
  466. using (StreamReader sr = new StreamReader (path, encoding)) {
  467. return sr.ReadToEnd ();
  468. }
  469. }
  470. public static void WriteAllBytes (string path, byte [] bytes)
  471. {
  472. using (Stream stream = File.Create (path)) {
  473. stream.Write (bytes, 0, bytes.Length);
  474. }
  475. }
  476. public static void WriteAllLines (string path, string [] contents)
  477. {
  478. using (StreamWriter writer = new StreamWriter (path)) {
  479. WriteAllLines (writer, contents);
  480. }
  481. }
  482. public static void WriteAllLines (string path, string [] contents, Encoding encoding)
  483. {
  484. using (StreamWriter writer = new StreamWriter (path, false, encoding)) {
  485. WriteAllLines (writer, contents);
  486. }
  487. }
  488. static void WriteAllLines (StreamWriter writer, string [] contents)
  489. {
  490. foreach (string line in contents)
  491. writer.WriteLine (line);
  492. }
  493. public static void WriteAllText (string path, string contents)
  494. {
  495. WriteAllText (path, contents, Encoding.UTF8Unmarked);
  496. }
  497. public static void WriteAllText (string path, string contents, Encoding encoding)
  498. {
  499. using (StreamWriter sw = new StreamWriter (path, false, encoding)) {
  500. sw.Write (contents);
  501. }
  502. }
  503. static DateTime? defaultLocalFileTime;
  504. static DateTime DefaultLocalFileTime {
  505. get {
  506. if (defaultLocalFileTime == null)
  507. defaultLocalFileTime = new DateTime (1601, 1, 1).ToLocalTime ();
  508. return defaultLocalFileTime.Value;
  509. }
  510. }
  511. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  512. public static void Encrypt (string path)
  513. {
  514. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  515. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  516. // we throw the same (instead of a NotImplementedException) because most code should already be
  517. // handling this exception to work properly.
  518. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  519. }
  520. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  521. public static void Decrypt (string path)
  522. {
  523. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  524. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  525. // we throw the same (instead of a NotImplementedException) because most code should already be
  526. // handling this exception to work properly.
  527. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  528. }
  529. #if NET_4_0
  530. public static IEnumerable<string> ReadLines (string path)
  531. {
  532. return ReadLines (File.OpenText (path));
  533. }
  534. public static IEnumerable<string> ReadLines (string path, Encoding encoding)
  535. {
  536. return ReadLines (new StreamReader (path, encoding));
  537. }
  538. // refactored in order to avoid compiler-generated names for Moonlight tools
  539. static IEnumerable<string> ReadLines (StreamReader reader)
  540. {
  541. using (reader) {
  542. string s;
  543. while ((s = reader.ReadLine ()) != null) {
  544. yield return s;
  545. }
  546. }
  547. }
  548. public static void AppendAllLines (string path, IEnumerable<string> contents)
  549. {
  550. Path.Validate (path);
  551. if (contents == null)
  552. return;
  553. using (TextWriter w = new StreamWriter (path, true)) {
  554. foreach (var line in contents)
  555. w.WriteLine (line);
  556. }
  557. }
  558. public static void AppendAllLines (string path, IEnumerable<string> contents, Encoding encoding)
  559. {
  560. Path.Validate (path);
  561. if (contents == null)
  562. return;
  563. using (TextWriter w = new StreamWriter (path, true, encoding)) {
  564. foreach (var line in contents)
  565. w.WriteLine (line);
  566. }
  567. }
  568. public static void WriteAllLines (string path, IEnumerable<string> contents)
  569. {
  570. Path.Validate (path);
  571. if (contents == null)
  572. return;
  573. using (TextWriter w = new StreamWriter (path, false)) {
  574. foreach (var line in contents)
  575. w.WriteLine (line);
  576. }
  577. }
  578. public static void WriteAllLines (string path, IEnumerable<string> contents, Encoding encoding)
  579. {
  580. Path.Validate (path);
  581. if (contents == null)
  582. return;
  583. using (TextWriter w = new StreamWriter (path, false, encoding)) {
  584. foreach (var line in contents)
  585. w.WriteLine (line);
  586. }
  587. }
  588. #endif
  589. }
  590. }