File.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // System.IO.File.cs
  3. //
  4. //
  5. // Authors:
  6. // Miguel de Icaza ([email protected])
  7. // Jim Richardson ([email protected])
  8. //
  9. // Copyright 2002 Ximian, Inc. http://www.ximian.com
  10. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  11. //
  12. using System;
  13. using System.Private;
  14. namespace System.IO
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public sealed class File : Object
  20. {
  21. const int COPY_BLOCK_SIZE = 32 * 1024;
  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 overrite)
  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. using (FileStream src_stream = new FileStream (
  39. src, FileMode.Open, FileAccess.Read, FileShare.Read, COPY_BLOCK_SIZE),
  40. dst_stream = new FileStream (
  41. dest, FileMode.CreateNew, FileAccess.Write, FileShare.None,
  42. COPY_BLOCK_SIZE)){
  43. byte [] buffer = new byte [COPY_BLOCK_SIZE];
  44. int count;
  45. while ((count = src_stream.Read (buffer, 0, COPY_BLOCK_SIZE)) != 0)
  46. dst_stream.Write (buffer, 0, count);
  47. }
  48. }
  49. public static FileStream Create (string path)
  50. {
  51. return Create (path, 8192);
  52. }
  53. public static FileStream Create (string path, int buffersize)
  54. {
  55. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  56. FileShare.None, buffersize);
  57. }
  58. public static void Delete (string path)
  59. {
  60. int code;
  61. if (path == null)
  62. throw new ArgumentNullException ();
  63. if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)
  64. throw new ArgumentException ();
  65. code = Wrapper.unlink (path);
  66. if (code == Wrapper.EISDIR)
  67. throw new UnauthorizedAccessException ();
  68. else
  69. throw new IOException (Errno.Message (code));
  70. }
  71. public static bool Exists (string path)
  72. {
  73. unsafe {
  74. stat s;
  75. if (Wrapper.stat (path, &s) == 0)
  76. return true;
  77. return false;
  78. }
  79. }
  80. [MonoTODO]
  81. public static FileAttributes GetAttributes (string path)
  82. {
  83. throw new Exception ("Unimplemented");
  84. }
  85. [MonoTODO]
  86. public static DateTime GetCreationTime (string path)
  87. {
  88. throw new Exception ("Unimplemented");
  89. }
  90. [MonoTODO]
  91. public static DateTime GetLastAccessTime (string path)
  92. {
  93. throw new Exception ("Unimplemented");
  94. }
  95. [MonoTODO]
  96. public static DateTime GetLastWriteTime (string path)
  97. {
  98. throw new Exception ("Unimplemented");
  99. }
  100. public static void Move (string src, string dest)
  101. {
  102. if (src == null || dest == null)
  103. throw new ArgumentNullException ();
  104. if (src == "" || dest == null ||
  105. src.IndexOfAny (Path.InvalidPathChars) != -1 ||
  106. dest.IndexOfAny (Path.InvalidPathChars) != -1)
  107. throw new ArgumentException ();
  108. int code = Wrapper.rename (src, dest);
  109. if (code == 0)
  110. return;
  111. throw new Exception (Errno.Message (code));
  112. }
  113. public static FileStream Open (string path, FileMode mode)
  114. {
  115. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  116. }
  117. public static FileStream Open (string path, FileMode mode, FileAccess access)
  118. {
  119. return new FileStream (path, mode, access, FileShare.None);
  120. }
  121. public static FileStream Open (string path, FileMode mode, FileAccess access,
  122. FileShare share)
  123. {
  124. return new FileStream (path, mode, access, share);
  125. }
  126. public static FileStream OpenRead (string path)
  127. {
  128. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  129. }
  130. public static StreamReader OpenText (string path)
  131. {
  132. return new StreamReader (path);
  133. }
  134. public FileStream OpenWrite (string path)
  135. {
  136. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  137. }
  138. [MonoTODO]
  139. public static void SetAttributes (string path, FileAttributes attributes)
  140. {
  141. throw new Exception ("Unimplemented");
  142. }
  143. [MonoTODO]
  144. public static void SetCreationTime (string path, DateTime creationTime)
  145. {
  146. throw new Exception ("Unimplemented");
  147. }
  148. [MonoTODO]
  149. public static void SetLastAccessTime (string path, DateTime accessTime)
  150. {
  151. throw new Exception ("Unimplemented");
  152. }
  153. [MonoTODO]
  154. public static void SetLastWriteTime (string path, DateTime modifiedTime)
  155. {
  156. throw new Exception ("Unimplemented");
  157. }
  158. }
  159. }