File.cs 4.5 KB

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