File.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. MonoIOError error;
  57. if (!MonoIO.CopyFile (src, dest, overwrite, out error))
  58. throw MonoIO.GetException (error);
  59. }
  60. public static FileStream Create (string path)
  61. {
  62. return Create (path, 8192);
  63. }
  64. public static FileStream Create (string path, int buffersize)
  65. {
  66. if (null == path)
  67. throw new ArgumentNullException("path");
  68. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  69. throw new ArgumentException("path");
  70. string DirName = Path.GetDirectoryName(path);
  71. if (DirName != String.Empty && !Directory.Exists (DirName))
  72. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  73. if (Exists(path)){
  74. if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
  75. throw new UnauthorizedAccessException(path + " is a read-only");
  76. }
  77. }
  78. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  79. FileShare.None, buffersize);
  80. }
  81. public static StreamWriter CreateText(string path)
  82. {
  83. return new StreamWriter (path, false);
  84. }
  85. public static void Delete (string path)
  86. {
  87. if (null == path)
  88. throw new ArgumentNullException("path");
  89. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  90. throw new ArgumentException("path");
  91. if (Directory.Exists (path))
  92. throw new UnauthorizedAccessException("path is a directory");
  93. string DirName = Path.GetDirectoryName(path);
  94. if (DirName != String.Empty && !Directory.Exists (DirName))
  95. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  96. MonoIOError error;
  97. if (!MonoIO.DeleteFile (path, out error)){
  98. Exception e = MonoIO.GetException (error);
  99. if (! (e is FileNotFoundException))
  100. throw e;
  101. }
  102. }
  103. public static bool Exists (string path)
  104. {
  105. // For security reasons no exceptions are
  106. // thrown, only false is returned if there is
  107. // any problem with the path or permissions.
  108. // Minimizes what information can be
  109. // discovered by using this method.
  110. if (null == path || String.Empty == path.Trim()
  111. || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
  112. return false;
  113. }
  114. MonoIOError error;
  115. return MonoIO.ExistsFile (path, out error);
  116. }
  117. public static FileAttributes GetAttributes (string path)
  118. {
  119. if (null == path) {
  120. throw new ArgumentNullException("path");
  121. }
  122. if (String.Empty == path.Trim()) {
  123. throw new ArgumentException("Path is empty");
  124. }
  125. if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
  126. throw new ArgumentException("Path contains invalid chars");
  127. }
  128. MonoIOError error;
  129. return MonoIO.GetFileAttributes (path, out error);
  130. }
  131. public static DateTime GetCreationTime (string path)
  132. {
  133. MonoIOStat stat;
  134. MonoIOError error;
  135. MonoIO.GetFileStat (path, out stat, out error);
  136. return DateTime.FromFileTime (stat.CreationTime);
  137. }
  138. public static DateTime GetLastAccessTime (string path)
  139. {
  140. MonoIOStat stat;
  141. MonoIOError error;
  142. MonoIO.GetFileStat (path, out stat, out error);
  143. return DateTime.FromFileTime (stat.LastAccessTime);
  144. }
  145. public static DateTime GetLastWriteTime (string path)
  146. {
  147. MonoIOStat stat;
  148. MonoIOError error;
  149. MonoIO.GetFileStat (path, out stat, out error);
  150. return DateTime.FromFileTime (stat.LastWriteTime);
  151. }
  152. public static void Move (string src, string dest)
  153. {
  154. if (src == null)
  155. throw new ArgumentNullException ("src");
  156. if (dest == null)
  157. throw new ArgumentNullException ("dest");
  158. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  159. throw new ArgumentException ("src");
  160. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  161. throw new ArgumentException ("dest");
  162. if (!Exists (src))
  163. throw new FileNotFoundException (src + " does not exist");
  164. if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))
  165. throw new ArgumentException(dest + " is a directory");
  166. string DirName;
  167. DirName = Path.GetDirectoryName(src);
  168. if (DirName != String.Empty && !Directory.Exists (DirName))
  169. throw new DirectoryNotFoundException("Source directory not found: " + DirName);
  170. DirName = Path.GetDirectoryName(dest);
  171. if (DirName != String.Empty && !Directory.Exists (DirName))
  172. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  173. MonoIOError error;
  174. if (!MonoIO.MoveFile (src, dest, out error))
  175. throw MonoIO.GetException (error);
  176. }
  177. public static FileStream Open (string path, FileMode mode)
  178. {
  179. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  180. }
  181. public static FileStream Open (string path, FileMode mode, FileAccess access)
  182. {
  183. return new FileStream (path, mode, access, FileShare.None);
  184. }
  185. public static FileStream Open (string path, FileMode mode, FileAccess access,
  186. FileShare share)
  187. {
  188. return new FileStream (path, mode, access, share);
  189. }
  190. public static FileStream OpenRead (string path)
  191. {
  192. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  193. }
  194. public static StreamReader OpenText (string path)
  195. {
  196. return new StreamReader (path);
  197. }
  198. public static FileStream OpenWrite (string path)
  199. {
  200. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  201. }
  202. public static void SetAttributes (string path,
  203. FileAttributes attributes)
  204. {
  205. MonoIOError error;
  206. if (!MonoIO.SetFileAttributes (path, attributes,
  207. out error)) {
  208. throw MonoIO.GetException (path, error);
  209. }
  210. }
  211. public static void SetCreationTime (string path,
  212. DateTime creation_time)
  213. {
  214. MonoIOError error;
  215. if (!MonoIO.SetFileTime (path, creation_time.Ticks,
  216. -1, -1, out error)) {
  217. throw MonoIO.GetException (path, error);
  218. }
  219. }
  220. public static void SetLastAccessTime (string path,DateTime last_access_time)
  221. {
  222. MonoIOError error;
  223. if (!MonoIO.SetFileTime (path, -1,
  224. last_access_time.Ticks, -1,
  225. out error)) {
  226. throw MonoIO.GetException (path, error);
  227. }
  228. }
  229. public static void SetLastWriteTime (string path,
  230. DateTime last_write_time)
  231. {
  232. MonoIOError error;
  233. if (!MonoIO.SetFileTime (path, -1, -1,
  234. last_write_time.Ticks,
  235. out error)) {
  236. throw MonoIO.GetException (path, error);
  237. }
  238. }
  239. }
  240. }