File.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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, true);
  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. throw MonoIO.GetException ();
  62. }
  63. public static bool Exists (string path)
  64. {
  65. return MonoIO.ExistsFile (path);
  66. }
  67. public static FileAttributes GetAttributes (string path)
  68. {
  69. return MonoIO.GetFileAttributes (path);
  70. }
  71. public static DateTime GetCreationTime (string path)
  72. {
  73. MonoIOStat stat;
  74. MonoIO.GetFileStat (path, out stat);
  75. return DateTime.FromFileTime (stat.CreationTime);
  76. }
  77. public static DateTime GetLastAccessTime (string path)
  78. {
  79. MonoIOStat stat;
  80. MonoIO.GetFileStat (path, out stat);
  81. return DateTime.FromFileTime (stat.LastAccessTime);
  82. }
  83. public static DateTime GetLastWriteTime (string path)
  84. {
  85. MonoIOStat stat;
  86. MonoIO.GetFileStat (path, out stat);
  87. return DateTime.FromFileTime (stat.LastWriteTime);
  88. }
  89. public static void Move (string src, string dest)
  90. {
  91. if (src == null || dest == null)
  92. throw new ArgumentNullException ();
  93. if (src == "" || dest == "" ||
  94. src.IndexOfAny (Path.InvalidPathChars) != -1 ||
  95. dest.IndexOfAny (Path.InvalidPathChars) != -1)
  96. throw new ArgumentException ();
  97. if (!MonoIO.MoveFile (src, dest))
  98. throw MonoIO.GetException ();
  99. }
  100. public static FileStream Open (string path, FileMode mode)
  101. {
  102. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  103. }
  104. public static FileStream Open (string path, FileMode mode, FileAccess access)
  105. {
  106. return new FileStream (path, mode, access, FileShare.None);
  107. }
  108. public static FileStream Open (string path, FileMode mode, FileAccess access,
  109. FileShare share)
  110. {
  111. return new FileStream (path, mode, access, share);
  112. }
  113. public static FileStream OpenRead (string path)
  114. {
  115. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  116. }
  117. public static StreamReader OpenText (string path)
  118. {
  119. return new StreamReader (path);
  120. }
  121. public static FileStream OpenWrite (string path)
  122. {
  123. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  124. }
  125. public static void SetAttributes (string path, FileAttributes attributes)
  126. {
  127. if (!MonoIO.SetFileAttributes (path, attributes))
  128. throw MonoIO.GetException (path);
  129. }
  130. public static void SetCreationTime (string path, DateTime creation_time)
  131. {
  132. if (!MonoIO.SetFileTime (path, creation_time.Ticks, -1, -1))
  133. throw MonoIO.GetException (path);
  134. }
  135. public static void SetLastAccessTime (string path, DateTime last_access_time)
  136. {
  137. if (!MonoIO.SetFileTime (path, -1, last_access_time.Ticks, -1))
  138. throw MonoIO.GetException (path);
  139. }
  140. public static void SetLastWriteTime (string path, DateTime last_write_time)
  141. {
  142. if (!MonoIO.SetFileTime (path, -1, -1, last_write_time.Ticks))
  143. throw MonoIO.GetException (path);
  144. }
  145. }
  146. }