File.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. //
  15. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. using System;
  37. namespace System.IO
  38. {
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. public sealed class File
  43. {
  44. private File () {}
  45. public static StreamWriter AppendText (string path)
  46. {
  47. return new StreamWriter (path, true);
  48. }
  49. [MonoTODO("Security Permision Checks")]
  50. public static void Copy (string sourceFilename, string destFilename)
  51. {
  52. Copy (sourceFilename, destFilename, false);
  53. }
  54. public static void Copy (string src, string dest, bool overwrite)
  55. {
  56. if (src == null)
  57. throw new ArgumentNullException ("src");
  58. if (dest == null)
  59. throw new ArgumentNullException ("dest");
  60. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  61. throw new ArgumentException ("src");
  62. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  63. throw new ArgumentException ("dest");
  64. if (!Exists (src))
  65. throw new FileNotFoundException (src + " does not exist");
  66. if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
  67. throw new ArgumentException(src + " is a directory");
  68. }
  69. if (Exists (dest)) {
  70. if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
  71. throw new ArgumentException(dest + " is a directory");
  72. }
  73. if (!overwrite)
  74. throw new IOException (dest + " already exists");
  75. }
  76. string DirName = Path.GetDirectoryName(dest);
  77. if (DirName != String.Empty && !Directory.Exists (DirName))
  78. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  79. MonoIOError error;
  80. if (!MonoIO.CopyFile (src, dest, overwrite, out error))
  81. throw MonoIO.GetException (error);
  82. }
  83. public static FileStream Create (string path)
  84. {
  85. return Create (path, 8192);
  86. }
  87. public static FileStream Create (string path, int buffersize)
  88. {
  89. if (null == path)
  90. throw new ArgumentNullException("path");
  91. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  92. throw new ArgumentException("path");
  93. string DirName = Path.GetDirectoryName(path);
  94. if (DirName != String.Empty && !Directory.Exists (DirName))
  95. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  96. if (Exists(path)){
  97. if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
  98. throw new UnauthorizedAccessException(path + " is a read-only");
  99. }
  100. }
  101. return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
  102. FileShare.None, buffersize);
  103. }
  104. public static StreamWriter CreateText(string path)
  105. {
  106. return new StreamWriter (path, false);
  107. }
  108. public static void Delete (string path)
  109. {
  110. if (null == path)
  111. throw new ArgumentNullException("path");
  112. if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
  113. throw new ArgumentException("path");
  114. if (Directory.Exists (path))
  115. throw new UnauthorizedAccessException("path is a directory");
  116. string DirName = Path.GetDirectoryName(path);
  117. if (DirName != String.Empty && !Directory.Exists (DirName))
  118. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  119. MonoIOError error;
  120. if (!MonoIO.DeleteFile (path, out error)){
  121. Exception e = MonoIO.GetException (path, error);
  122. if (! (e is FileNotFoundException))
  123. throw e;
  124. }
  125. }
  126. public static bool Exists (string path)
  127. {
  128. // For security reasons no exceptions are
  129. // thrown, only false is returned if there is
  130. // any problem with the path or permissions.
  131. // Minimizes what information can be
  132. // discovered by using this method.
  133. if (null == path || String.Empty == path.Trim()
  134. || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
  135. return false;
  136. }
  137. MonoIOError error;
  138. bool exists;
  139. exists = MonoIO.ExistsFile (path, out error);
  140. if (error != MonoIOError.ERROR_SUCCESS &&
  141. error != MonoIOError.ERROR_FILE_NOT_FOUND &&
  142. error != MonoIOError.ERROR_PATH_NOT_FOUND) {
  143. throw MonoIO.GetException (path, error);
  144. }
  145. return(exists);
  146. }
  147. public static FileAttributes GetAttributes (string path)
  148. {
  149. if (null == path) {
  150. throw new ArgumentNullException("path");
  151. }
  152. if (String.Empty == path.Trim()) {
  153. throw new ArgumentException("Path is empty");
  154. }
  155. if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
  156. throw new ArgumentException("Path contains invalid chars");
  157. }
  158. MonoIOError error;
  159. FileAttributes attrs;
  160. attrs = MonoIO.GetFileAttributes (path, out error);
  161. if (error != MonoIOError.ERROR_SUCCESS) {
  162. throw MonoIO.GetException (path, error);
  163. }
  164. return(attrs);
  165. }
  166. public static DateTime GetCreationTime (string path)
  167. {
  168. MonoIOStat stat;
  169. MonoIOError error;
  170. CheckPathExceptions (path);
  171. if (!MonoIO.GetFileStat (path, out stat, out error))
  172. throw new IOException (path);
  173. return DateTime.FromFileTime (stat.CreationTime);
  174. }
  175. public static DateTime GetCreationTimeUtc (string path)
  176. {
  177. return GetCreationTime (path).ToUniversalTime ();
  178. }
  179. public static DateTime GetLastAccessTime (string path)
  180. {
  181. MonoIOStat stat;
  182. MonoIOError error;
  183. CheckPathExceptions (path);
  184. if (!MonoIO.GetFileStat (path, out stat, out error))
  185. throw new IOException (path);
  186. return DateTime.FromFileTime (stat.LastAccessTime);
  187. }
  188. public static DateTime GetLastAccessTimeUtc (string path)
  189. {
  190. return GetLastAccessTime (path).ToUniversalTime ();
  191. }
  192. public static DateTime GetLastWriteTime (string path)
  193. {
  194. MonoIOStat stat;
  195. MonoIOError error;
  196. CheckPathExceptions (path);
  197. if (!MonoIO.GetFileStat (path, out stat, out error))
  198. throw new IOException (path);
  199. return DateTime.FromFileTime (stat.LastWriteTime);
  200. }
  201. public static DateTime GetLastWriteTimeUtc (string path)
  202. {
  203. return GetLastWriteTime (path).ToUniversalTime ();
  204. }
  205. public static void Move (string src, string dest)
  206. {
  207. MonoIOError error;
  208. if (src == null)
  209. throw new ArgumentNullException ("src");
  210. if (dest == null)
  211. throw new ArgumentNullException ("dest");
  212. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  213. throw new ArgumentException ("src");
  214. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  215. throw new ArgumentException ("dest");
  216. if (!MonoIO.Exists (src, out error))
  217. throw new FileNotFoundException (src + " does not exist");
  218. if (MonoIO.ExistsDirectory (dest, out error))
  219. throw new IOException (dest + " is a directory");
  220. string DirName;
  221. DirName = Path.GetDirectoryName(src);
  222. if (DirName != String.Empty && !Directory.Exists (DirName))
  223. throw new DirectoryNotFoundException("Source directory not found: " + DirName);
  224. DirName = Path.GetDirectoryName(dest);
  225. if (DirName != String.Empty && !Directory.Exists (DirName))
  226. throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
  227. if (!MonoIO.MoveFile (src, dest, out error))
  228. throw MonoIO.GetException (error);
  229. }
  230. public static FileStream Open (string path, FileMode mode)
  231. {
  232. return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
  233. }
  234. public static FileStream Open (string path, FileMode mode, FileAccess access)
  235. {
  236. return new FileStream (path, mode, access, FileShare.None);
  237. }
  238. public static FileStream Open (string path, FileMode mode, FileAccess access,
  239. FileShare share)
  240. {
  241. return new FileStream (path, mode, access, share);
  242. }
  243. public static FileStream OpenRead (string path)
  244. {
  245. return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
  246. }
  247. public static StreamReader OpenText (string path)
  248. {
  249. return new StreamReader (path);
  250. }
  251. public static FileStream OpenWrite (string path)
  252. {
  253. return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  254. }
  255. public static void SetAttributes (string path,
  256. FileAttributes attributes)
  257. {
  258. MonoIOError error;
  259. CheckPathExceptions (path);
  260. if (!MonoIO.SetFileAttributes (path, attributes,
  261. out error)) {
  262. throw MonoIO.GetException (path, error);
  263. }
  264. }
  265. public static void SetCreationTime (string path,
  266. DateTime creation_time)
  267. {
  268. MonoIOError error;
  269. CheckPathExceptions (path);
  270. if (!MonoIO.Exists (path, out error))
  271. throw MonoIO.GetException (path, error);
  272. if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
  273. throw MonoIO.GetException (path, error);
  274. }
  275. }
  276. public static void SetCreationTimeUtc (string path,
  277. DateTime creation_time)
  278. {
  279. SetCreationTime (path, creation_time.ToLocalTime ());
  280. }
  281. public static void SetLastAccessTime (string path,DateTime last_access_time)
  282. {
  283. MonoIOError error;
  284. CheckPathExceptions (path);
  285. if (!MonoIO.Exists (path, out error))
  286. throw MonoIO.GetException (path, error);
  287. if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
  288. throw MonoIO.GetException (path, error);
  289. }
  290. }
  291. public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
  292. {
  293. SetLastAccessTime (path, last_access_time.ToLocalTime ());
  294. }
  295. public static void SetLastWriteTime (string path,
  296. DateTime last_write_time)
  297. {
  298. MonoIOError error;
  299. CheckPathExceptions (path);
  300. if (!MonoIO.Exists (path, out error))
  301. throw MonoIO.GetException (path, error);
  302. if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
  303. throw MonoIO.GetException (path, error);
  304. }
  305. }
  306. public static void SetLastWriteTimeUtc (string path,
  307. DateTime last_write_time)
  308. {
  309. SetLastWriteTime (path, last_write_time.ToLocalTime ());
  310. }
  311. #region Private
  312. private static void CheckPathExceptions (string path)
  313. {
  314. if (path == null)
  315. throw new System.ArgumentNullException("Path is Null");
  316. if (path == "")
  317. throw new System.ArgumentException("Path is Empty");
  318. if (path.Trim().Length == 0)
  319. throw new ArgumentException ("Only blank characters in path");
  320. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  321. throw new ArgumentException ("Path contains invalid chars");
  322. }
  323. #endregion
  324. }
  325. }