File.cs 4.4 KB

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