File.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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
  20. {
  21. private File () {}
  22. public static StreamWriter AppendText (string path)
  23. {
  24. return new StreamWriter (path, true);
  25. }
  26. [MonoTODO("Security Permision Checks")]
  27. public static void Copy (string sourceFilename, string destFilename)
  28. {
  29. Copy (sourceFilename, destFilename, false);
  30. }
  31. public static void Copy (string src, string dest, bool overwrite)
  32. {
  33. if (src == null)
  34. throw new ArgumentNullException ("src");
  35. if (dest == null)
  36. throw new ArgumentNullException ("dest");
  37. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  38. throw new ArgumentException ("src");
  39. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  40. throw new ArgumentException ("dest");
  41. if (!Exists (src))
  42. throw new FileNotFoundException (src + " does not exist");
  43. if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
  44. throw new ArgumentException(src + " is a directory");
  45. }
  46. if (Exists (dest)) {
  47. if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
  48. throw new ArgumentException(dest + " is a directory");
  49. }
  50. if (!overwrite)
  51. throw new IOException (dest + " already exists");
  52. }
  53. string DirName = Path.GetDirectoryName(dest);
  54. if (DirName != String.Empty && !Directory.Exists (DirName))
  55. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  56. if (!MonoIO.CopyFile (src, dest, overwrite))
  57. throw MonoIO.GetException ();
  58. }
  59. public static FileStream Create (string path)
  60. {
  61. return Create (path, 8192);
  62. }
  63. public static FileStream Create (string path, int buffersize)
  64. {
  65. if (null == path)
  66. throw new ArgumentNullException("path");
  67. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  68. throw new ArgumentException("path");
  69. string DirName = Path.GetDirectoryName(path);
  70. if (DirName != String.Empty && !Directory.Exists (DirName))
  71. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  72. if (Exists(path)){
  73. if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
  74. throw new UnauthorizedAccessException(path + " is a read-only");
  75. }
  76. }
  77. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  78. FileShare.None, buffersize);
  79. }
  80. public static StreamWriter CreateText(string path)
  81. {
  82. return new StreamWriter (path, false);
  83. }
  84. public static void Delete (string path)
  85. {
  86. if (null == path)
  87. throw new ArgumentNullException("path");
  88. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  89. throw new ArgumentException("path");
  90. if (Directory.Exists (path))
  91. throw new UnauthorizedAccessException("path is a directory");
  92. string DirName = Path.GetDirectoryName(path);
  93. if (DirName != String.Empty && !Directory.Exists (DirName))
  94. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  95. if (!MonoIO.DeleteFile (path)){
  96. Exception e = MonoIO.GetException ();
  97. if (! (e is FileNotFoundException))
  98. throw e;
  99. }
  100. }
  101. public static bool Exists (string path)
  102. {
  103. // For security reasons no exceptions are thrown, only false is returned if there
  104. // is any problem with the path or permissions. Minimizes what information can be
  105. // discovered by using this method.
  106. if (null == path || String.Empty == path.Trim()
  107. || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  108. return false;
  109. return MonoIO.ExistsFile (path);
  110. }
  111. public static FileAttributes GetAttributes (string path)
  112. {
  113. if (null == path)
  114. throw new ArgumentNullException("path");
  115. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  116. throw new ArgumentException("path");
  117. return MonoIO.GetFileAttributes (path);
  118. }
  119. public static DateTime GetCreationTime (string path)
  120. {
  121. MonoIOStat stat;
  122. MonoIO.GetFileStat (path, out stat);
  123. return DateTime.FromFileTime (stat.CreationTime);
  124. }
  125. public static DateTime GetLastAccessTime (string path)
  126. {
  127. MonoIOStat stat;
  128. MonoIO.GetFileStat (path, out stat);
  129. return DateTime.FromFileTime (stat.LastAccessTime);
  130. }
  131. public static DateTime GetLastWriteTime (string path)
  132. {
  133. MonoIOStat stat;
  134. MonoIO.GetFileStat (path, out stat);
  135. return DateTime.FromFileTime (stat.LastWriteTime);
  136. }
  137. public static void Move (string src, string dest)
  138. {
  139. if (src == null)
  140. throw new ArgumentNullException ("src");
  141. if (dest == null)
  142. throw new ArgumentNullException ("dest");
  143. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  144. throw new ArgumentException ("src");
  145. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  146. throw new ArgumentException ("dest");
  147. if (!Exists (src))
  148. throw new FileNotFoundException (src + " does not exist");
  149. if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))
  150. throw new ArgumentException(dest + " is a directory");
  151. string DirName;
  152. DirName = Path.GetDirectoryName(src);
  153. if (DirName != String.Empty && !Directory.Exists (DirName))
  154. throw new DirectoryNotFoundException("Source directory not found: " + DirName);
  155. DirName = Path.GetDirectoryName(dest);
  156. if (DirName != String.Empty && !Directory.Exists (DirName))
  157. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  158. if (!MonoIO.MoveFile (src, dest))
  159. throw MonoIO.GetException ();
  160. }
  161. public static FileStream Open (string path, FileMode mode)
  162. {
  163. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  164. }
  165. public static FileStream Open (string path, FileMode mode, FileAccess access)
  166. {
  167. return new FileStream (path, mode, access, FileShare.None);
  168. }
  169. public static FileStream Open (string path, FileMode mode, FileAccess access,
  170. FileShare share)
  171. {
  172. return new FileStream (path, mode, access, share);
  173. }
  174. public static FileStream OpenRead (string path)
  175. {
  176. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  177. }
  178. public static StreamReader OpenText (string path)
  179. {
  180. return new StreamReader (path);
  181. }
  182. public static FileStream OpenWrite (string path)
  183. {
  184. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  185. }
  186. public static void SetAttributes (string path, FileAttributes attributes)
  187. {
  188. if (!MonoIO.SetFileAttributes (path, attributes))
  189. throw MonoIO.GetException (path);
  190. }
  191. public static void SetCreationTime (string path, DateTime creation_time)
  192. {
  193. if (!MonoIO.SetFileTime (path, creation_time.Ticks, -1, -1))
  194. throw MonoIO.GetException (path);
  195. }
  196. public static void SetLastAccessTime (string path, DateTime last_access_time)
  197. {
  198. if (!MonoIO.SetFileTime (path, -1, last_access_time.Ticks, -1))
  199. throw MonoIO.GetException (path);
  200. }
  201. public static void SetLastWriteTime (string path, DateTime last_write_time)
  202. {
  203. if (!MonoIO.SetFileTime (path, -1, -1, last_write_time.Ticks))
  204. throw MonoIO.GetException (path);
  205. }
  206. }
  207. }