File.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. //
  10. // Copyright 2002 Ximian, Inc. http://www.ximian.com
  11. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  12. //
  13. using System;
  14. namespace System.IO
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public sealed class File : Object
  20. {
  21. private File () {}
  22. public static StreamWriter AppendText (string path)
  23. {
  24. return new StreamWriter (path, true);
  25. }
  26. public static void Copy (string sourceFilename, string destFilename)
  27. {
  28. Copy (sourceFilename, destFilename, false);
  29. }
  30. public static void Copy (string src, string dest, bool overwrite)
  31. {
  32. if (src == null || dest == null)
  33. throw new ArgumentNullException ();
  34. if (src == "" || dest == "" ||
  35. src.IndexOfAny (Path.InvalidPathChars) != -1 ||
  36. dest.IndexOfAny (Path.InvalidPathChars) != -1)
  37. throw new ArgumentException ();
  38. if (!MonoIO.CopyFile (src, dest, overwrite))
  39. throw MonoIO.GetException ();
  40. }
  41. public static FileStream Create (string path)
  42. {
  43. return Create (path, 8192);
  44. }
  45. public static FileStream Create (string path, int buffersize)
  46. {
  47. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  48. FileShare.None, buffersize);
  49. }
  50. public static StreamWriter CreateText(string path)
  51. {
  52. return new StreamWriter (path, false);
  53. }
  54. public static void Delete (string path)
  55. {
  56. if (path == null)
  57. throw new ArgumentNullException ();
  58. if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)
  59. throw new ArgumentException ();
  60. if (!MonoIO.DeleteFile (path)){
  61. Exception e = MonoIO.GetException ();
  62. if (e != null && !(e is FileNotFoundException))
  63. throw e;
  64. }
  65. }
  66. public static bool Exists (string path)
  67. {
  68. return MonoIO.ExistsFile (path);
  69. }
  70. public static FileAttributes GetAttributes (string path)
  71. {
  72. return MonoIO.GetFileAttributes (path);
  73. }
  74. public static DateTime GetCreationTime (string path)
  75. {
  76. MonoIOStat stat;
  77. MonoIO.GetFileStat (path, out stat);
  78. return DateTime.FromFileTime (stat.CreationTime);
  79. }
  80. public static DateTime GetLastAccessTime (string path)
  81. {
  82. MonoIOStat stat;
  83. MonoIO.GetFileStat (path, out stat);
  84. return DateTime.FromFileTime (stat.LastAccessTime);
  85. }
  86. public static DateTime GetLastWriteTime (string path)
  87. {
  88. MonoIOStat stat;
  89. MonoIO.GetFileStat (path, out stat);
  90. return DateTime.FromFileTime (stat.LastWriteTime);
  91. }
  92. public static void Move (string src, string dest)
  93. {
  94. if (src == null || dest == null)
  95. throw new ArgumentNullException ();
  96. if (src == "" || dest == "" ||
  97. src.IndexOfAny (Path.InvalidPathChars) != -1 ||
  98. dest.IndexOfAny (Path.InvalidPathChars) != -1)
  99. throw new ArgumentException ();
  100. if (!MonoIO.MoveFile (src, dest))
  101. throw MonoIO.GetException ();
  102. }
  103. public static FileStream Open (string path, FileMode mode)
  104. {
  105. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  106. }
  107. public static FileStream Open (string path, FileMode mode, FileAccess access)
  108. {
  109. return new FileStream (path, mode, access, FileShare.None);
  110. }
  111. public static FileStream Open (string path, FileMode mode, FileAccess access,
  112. FileShare share)
  113. {
  114. return new FileStream (path, mode, access, share);
  115. }
  116. public static FileStream OpenRead (string path)
  117. {
  118. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  119. }
  120. public static StreamReader OpenText (string path)
  121. {
  122. return new StreamReader (path);
  123. }
  124. public static FileStream OpenWrite (string path)
  125. {
  126. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  127. }
  128. public static void SetAttributes (string path, FileAttributes attributes)
  129. {
  130. if (!MonoIO.SetFileAttributes (path, attributes))
  131. throw MonoIO.GetException (path);
  132. }
  133. public static void SetCreationTime (string path, DateTime creation_time)
  134. {
  135. if (!MonoIO.SetFileTime (path, creation_time.Ticks, -1, -1))
  136. throw MonoIO.GetException (path);
  137. }
  138. public static void SetLastAccessTime (string path, DateTime last_access_time)
  139. {
  140. if (!MonoIO.SetFileTime (path, -1, last_access_time.Ticks, -1))
  141. throw MonoIO.GetException (path);
  142. }
  143. public static void SetLastWriteTime (string path, DateTime last_write_time)
  144. {
  145. if (!MonoIO.SetFileTime (path, -1, -1, last_write_time.Ticks))
  146. throw MonoIO.GetException (path);
  147. }
  148. }
  149. }