File.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // Ville Palo ([email protected])
  10. //
  11. // Copyright 2002 Ximian, Inc. http://www.ximian.com
  12. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  13. //
  14. using System;
  15. namespace System.IO
  16. {
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public sealed class File
  21. {
  22. private File () {}
  23. public static StreamWriter AppendText (string path)
  24. {
  25. return new StreamWriter (path, true);
  26. }
  27. [MonoTODO("Security Permision Checks")]
  28. public static void Copy (string sourceFilename, string destFilename)
  29. {
  30. Copy (sourceFilename, destFilename, false);
  31. }
  32. public static void Copy (string src, string dest, bool overwrite)
  33. {
  34. if (src == null)
  35. throw new ArgumentNullException ("src");
  36. if (dest == null)
  37. throw new ArgumentNullException ("dest");
  38. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  39. throw new ArgumentException ("src");
  40. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  41. throw new ArgumentException ("dest");
  42. if (!Exists (src))
  43. throw new FileNotFoundException (src + " does not exist");
  44. if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
  45. throw new ArgumentException(src + " is a directory");
  46. }
  47. if (Exists (dest)) {
  48. if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
  49. throw new ArgumentException(dest + " is a directory");
  50. }
  51. if (!overwrite)
  52. throw new IOException (dest + " already exists");
  53. }
  54. string DirName = Path.GetDirectoryName(dest);
  55. if (DirName != String.Empty && !Directory.Exists (DirName))
  56. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  57. MonoIOError error;
  58. if (!MonoIO.CopyFile (src, dest, overwrite, out error))
  59. throw MonoIO.GetException (error);
  60. }
  61. public static FileStream Create (string path)
  62. {
  63. return Create (path, 8192);
  64. }
  65. public static FileStream Create (string path, int buffersize)
  66. {
  67. if (null == path)
  68. throw new ArgumentNullException("path");
  69. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  70. throw new ArgumentException("path");
  71. string DirName = Path.GetDirectoryName(path);
  72. if (DirName != String.Empty && !Directory.Exists (DirName))
  73. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  74. if (Exists(path)){
  75. if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
  76. throw new UnauthorizedAccessException(path + " is a read-only");
  77. }
  78. }
  79. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  80. FileShare.None, buffersize);
  81. }
  82. public static StreamWriter CreateText(string path)
  83. {
  84. return new StreamWriter (path, false);
  85. }
  86. public static void Delete (string path)
  87. {
  88. if (null == path)
  89. throw new ArgumentNullException("path");
  90. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  91. throw new ArgumentException("path");
  92. if (Directory.Exists (path))
  93. throw new UnauthorizedAccessException("path is a directory");
  94. string DirName = Path.GetDirectoryName(path);
  95. if (DirName != String.Empty && !Directory.Exists (DirName))
  96. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  97. MonoIOError error;
  98. if (!MonoIO.DeleteFile (path, out error)){
  99. Exception e = MonoIO.GetException (error);
  100. if (! (e is FileNotFoundException))
  101. throw e;
  102. }
  103. }
  104. public static bool Exists (string path)
  105. {
  106. // For security reasons no exceptions are
  107. // thrown, only false is returned if there is
  108. // any problem with the path or permissions.
  109. // Minimizes what information can be
  110. // discovered by using this method.
  111. if (null == path || String.Empty == path.Trim()
  112. || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
  113. return false;
  114. }
  115. MonoIOError error;
  116. return MonoIO.ExistsFile (path, out error);
  117. }
  118. public static FileAttributes GetAttributes (string path)
  119. {
  120. if (null == path) {
  121. throw new ArgumentNullException("path");
  122. }
  123. if (String.Empty == path.Trim()) {
  124. throw new ArgumentException("Path is empty");
  125. }
  126. if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
  127. throw new ArgumentException("Path contains invalid chars");
  128. }
  129. MonoIOError error;
  130. return MonoIO.GetFileAttributes (path, out error);
  131. }
  132. public static DateTime GetCreationTime (string path)
  133. {
  134. MonoIOStat stat;
  135. MonoIOError error;
  136. CheckPathExceptions (path);
  137. if (!MonoIO.GetFileStat (path, out stat, out error))
  138. throw new IOException (path);
  139. return DateTime.FromFileTime (stat.CreationTime);
  140. }
  141. public static DateTime GetCreationTimeUtc (string path)
  142. {
  143. return GetCreationTime (path).ToUniversalTime ();
  144. }
  145. public static DateTime GetLastAccessTime (string path)
  146. {
  147. MonoIOStat stat;
  148. MonoIOError error;
  149. CheckPathExceptions (path);
  150. if (!MonoIO.GetFileStat (path, out stat, out error))
  151. throw new IOException (path);
  152. return DateTime.FromFileTime (stat.LastAccessTime);
  153. }
  154. public static DateTime GetLastAccessTimeUtc (string path)
  155. {
  156. return GetLastAccessTime (path).ToUniversalTime ();
  157. }
  158. public static DateTime GetLastWriteTime (string path)
  159. {
  160. MonoIOStat stat;
  161. MonoIOError error;
  162. CheckPathExceptions (path);
  163. if (!MonoIO.GetFileStat (path, out stat, out error))
  164. throw new IOException (path);
  165. return DateTime.FromFileTime (stat.LastWriteTime);
  166. }
  167. public static DateTime GetLastWriteTimeUtc (string path)
  168. {
  169. return GetLastWriteTime (path).ToUniversalTime ();
  170. }
  171. public static void Move (string src, string dest)
  172. {
  173. MonoIOError error;
  174. if (src == null)
  175. throw new ArgumentNullException ("src");
  176. if (dest == null)
  177. throw new ArgumentNullException ("dest");
  178. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  179. throw new ArgumentException ("src");
  180. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  181. throw new ArgumentException ("dest");
  182. if (!MonoIO.Exists (src, out error))
  183. throw new FileNotFoundException (src + " does not exist");
  184. if (MonoIO.ExistsDirectory (dest, out error))
  185. throw new ArgumentException(dest + " is a directory");
  186. string DirName;
  187. DirName = Path.GetDirectoryName(src);
  188. if (DirName != String.Empty && !Directory.Exists (DirName))
  189. throw new DirectoryNotFoundException("Source directory not found: " + DirName);
  190. DirName = Path.GetDirectoryName(dest);
  191. if (DirName != String.Empty && !Directory.Exists (DirName))
  192. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  193. if (!MonoIO.MoveFile (src, dest, out error))
  194. throw MonoIO.GetException (error);
  195. }
  196. public static FileStream Open (string path, FileMode mode)
  197. {
  198. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  199. }
  200. public static FileStream Open (string path, FileMode mode, FileAccess access)
  201. {
  202. return new FileStream (path, mode, access, FileShare.None);
  203. }
  204. public static FileStream Open (string path, FileMode mode, FileAccess access,
  205. FileShare share)
  206. {
  207. return new FileStream (path, mode, access, share);
  208. }
  209. public static FileStream OpenRead (string path)
  210. {
  211. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  212. }
  213. public static StreamReader OpenText (string path)
  214. {
  215. return new StreamReader (path);
  216. }
  217. public static FileStream OpenWrite (string path)
  218. {
  219. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  220. }
  221. public static void SetAttributes (string path,
  222. FileAttributes attributes)
  223. {
  224. MonoIOError error;
  225. CheckPathExceptions (path);
  226. if (!MonoIO.SetFileAttributes (path, attributes,
  227. out error)) {
  228. throw MonoIO.GetException (path, error);
  229. }
  230. }
  231. public static void SetCreationTime (string path,
  232. DateTime creation_time)
  233. {
  234. MonoIOError error;
  235. CheckPathExceptions (path);
  236. if (!MonoIO.Exists (path, out error))
  237. throw MonoIO.GetException (path, error);
  238. if (!MonoIO.SetFileTime (path, creation_time.ToFileTime(),
  239. -1, -1, out error)) {
  240. throw MonoIO.GetException (path, error);
  241. }
  242. }
  243. public static void SetCreationTimeUtc (string path,
  244. DateTime creation_time)
  245. {
  246. SetCreationTime (path, creation_time.ToLocalTime ());
  247. }
  248. public static void SetLastAccessTime (string path,DateTime last_access_time)
  249. {
  250. MonoIOError error;
  251. CheckPathExceptions (path);
  252. if (!MonoIO.Exists (path, out error))
  253. throw MonoIO.GetException (path, error);
  254. if (!MonoIO.SetFileTime (path, -1,
  255. last_access_time.ToFileTime(), -1,
  256. out error)) {
  257. throw MonoIO.GetException (path, error);
  258. }
  259. }
  260. public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
  261. {
  262. SetLastAccessTime (path, last_access_time.ToLocalTime ());
  263. }
  264. public static void SetLastWriteTime (string path,
  265. DateTime last_write_time)
  266. {
  267. MonoIOError error;
  268. CheckPathExceptions (path);
  269. if (!MonoIO.Exists (path, out error))
  270. throw MonoIO.GetException (path, error);
  271. if (!MonoIO.SetFileTime (path, -1, -1,
  272. last_write_time.ToFileTime(),
  273. out error)) {
  274. throw MonoIO.GetException (path, error);
  275. }
  276. }
  277. public static void SetLastWriteTimeUtc (string path,
  278. DateTime last_write_time)
  279. {
  280. SetLastWriteTime (path, last_write_time.ToLocalTime ());
  281. }
  282. #region Private
  283. private static void CheckPathExceptions (string path)
  284. {
  285. if (path == null)
  286. throw new System.ArgumentNullException("Path is Null");
  287. if (path == "")
  288. throw new System.ArgumentException("Path is Empty");
  289. if (path.Trim().Length == 0)
  290. throw new ArgumentException ("Only blank characters in path");
  291. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  292. throw new ArgumentException ("Path contains invalid chars");
  293. }
  294. #endregion
  295. }
  296. }