File.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. if (!MonoIO.GetFileStat (path, out stat, out error))
  136. throw new IOException (path);
  137. return DateTime.FromFileTime (stat.CreationTime);
  138. }
  139. public static DateTime GetLastAccessTime (string path)
  140. {
  141. MonoIOStat stat;
  142. MonoIOError error;
  143. if (!MonoIO.GetFileStat (path, out stat, out error))
  144. throw MonoIO.GetException (path, error);
  145. return DateTime.FromFileTime (stat.LastAccessTime);
  146. }
  147. public static DateTime GetLastWriteTime (string path)
  148. {
  149. MonoIOStat stat;
  150. MonoIOError error;
  151. if (!MonoIO.GetFileStat (path, out stat, out error))
  152. throw MonoIO.GetException (path, error);
  153. return DateTime.FromFileTime (stat.LastWriteTime);
  154. }
  155. public static void Move (string src, string dest)
  156. {
  157. if (src == null)
  158. throw new ArgumentNullException ("src");
  159. if (dest == null)
  160. throw new ArgumentNullException ("dest");
  161. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  162. throw new ArgumentException ("src");
  163. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  164. throw new ArgumentException ("dest");
  165. if (!Exists (src))
  166. throw new FileNotFoundException (src + " does not exist");
  167. if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))
  168. throw new ArgumentException(dest + " is a directory");
  169. string DirName;
  170. DirName = Path.GetDirectoryName(src);
  171. if (DirName != String.Empty && !Directory.Exists (DirName))
  172. throw new DirectoryNotFoundException("Source directory not found: " + DirName);
  173. DirName = Path.GetDirectoryName(dest);
  174. if (DirName != String.Empty && !Directory.Exists (DirName))
  175. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  176. MonoIOError error;
  177. if (!MonoIO.MoveFile (src, dest, out error))
  178. throw MonoIO.GetException (error);
  179. }
  180. public static FileStream Open (string path, FileMode mode)
  181. {
  182. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  183. }
  184. public static FileStream Open (string path, FileMode mode, FileAccess access)
  185. {
  186. return new FileStream (path, mode, access, FileShare.None);
  187. }
  188. public static FileStream Open (string path, FileMode mode, FileAccess access,
  189. FileShare share)
  190. {
  191. return new FileStream (path, mode, access, share);
  192. }
  193. public static FileStream OpenRead (string path)
  194. {
  195. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  196. }
  197. public static StreamReader OpenText (string path)
  198. {
  199. return new StreamReader (path);
  200. }
  201. public static FileStream OpenWrite (string path)
  202. {
  203. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  204. }
  205. public static void SetAttributes (string path,
  206. FileAttributes attributes)
  207. {
  208. MonoIOError error;
  209. if (!MonoIO.SetFileAttributes (path, attributes,
  210. out error)) {
  211. throw MonoIO.GetException (path, error);
  212. }
  213. }
  214. public static void SetCreationTime (string path,
  215. DateTime creation_time)
  216. {
  217. MonoIOError error;
  218. if (!MonoIO.SetFileTime (path, creation_time.ToFileTime(),
  219. -1, -1, out error)) {
  220. throw MonoIO.GetException (path, error);
  221. }
  222. }
  223. public static void SetLastAccessTime (string path,DateTime last_access_time)
  224. {
  225. MonoIOError error;
  226. if (!MonoIO.SetFileTime (path, -1,
  227. last_access_time.ToFileTime(), -1,
  228. out error)) {
  229. throw MonoIO.GetException (path, error);
  230. }
  231. }
  232. public static void SetLastWriteTime (string path,
  233. DateTime last_write_time)
  234. {
  235. MonoIOError error;
  236. if (!MonoIO.SetFileTime (path, -1, -1,
  237. last_write_time.ToFileTime(),
  238. out error)) {
  239. throw MonoIO.GetException (path, error);
  240. }
  241. }
  242. }
  243. }