Directory.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // System.IO.Directory.cs
  3. //
  4. // Authors:
  5. // Jim Richardson ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Dan Lewis ([email protected])
  8. //
  9. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  10. // Copyright (C) 2002 Ximian, Inc.
  11. //
  12. // Created: Monday, August 13, 2001
  13. //
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Security.Permissions;
  17. using System.Collections;
  18. namespace System.IO
  19. {
  20. public sealed class Directory : Object
  21. {
  22. private Directory () {}
  23. public static DirectoryInfo CreateDirectory (string path)
  24. {
  25. if (path == null)
  26. throw new ArgumentNullException ();
  27. if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)
  28. throw new ArgumentException ();
  29. if (!MonoIO.CreateDirectory (path))
  30. throw MonoIO.GetException ();
  31. return new DirectoryInfo (path);
  32. }
  33. public static void Delete (string path)
  34. {
  35. if (path == null)
  36. throw new ArgumentNullException ();
  37. if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)
  38. throw new ArgumentException ();
  39. if (!MonoIO.RemoveDirectory (path))
  40. throw MonoIO.GetException ();
  41. }
  42. static void RecursiveDelete (string path)
  43. {
  44. foreach (string dir in GetDirectories (path))
  45. RecursiveDelete (dir);
  46. foreach (string file in GetFiles (path))
  47. File.Delete (file);
  48. Directory.Delete (path);
  49. }
  50. public static void Delete (string path, bool recurse)
  51. {
  52. if (path == null)
  53. throw new ArgumentNullException ();
  54. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  55. throw new ArgumentException ("Path contains invalid characters");
  56. if (recurse == false){
  57. Delete (path);
  58. return;
  59. }
  60. RecursiveDelete (path);
  61. }
  62. public static bool Exists (string path)
  63. {
  64. return MonoIO.ExistsDirectory (path);
  65. }
  66. public static DateTime GetLastAccessTime (string path)
  67. {
  68. return File.GetLastAccessTime (path);
  69. }
  70. public static DateTime GetLastWriteTime (string path)
  71. {
  72. return File.GetLastWriteTime (path);
  73. }
  74. public static DateTime GetCreationTime (string path)
  75. {
  76. return File.GetLastWriteTime (path);
  77. }
  78. public static string GetCurrentDirectory ()
  79. {
  80. /*
  81. // Implementation complete 08/25/2001 14:24 except for
  82. // LAMESPEC: documentation specifies invalid exceptions (i think)
  83. // also shouldn't need Write to getcurrrent should we?
  84. string str = Environment.CurrentDirectory;
  85. CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, str);
  86. */
  87. return Environment.CurrentDirectory;
  88. }
  89. public static string [] GetDirectories (string path)
  90. {
  91. return GetDirectories (path, "*");
  92. }
  93. public static string [] GetDirectories (string path, string pattern)
  94. {
  95. return GetFileSystemEntries (path, pattern, FileAttributes.Directory, FileAttributes.Directory);
  96. }
  97. public static string GetDirectoryRoot (string path)
  98. {
  99. return "" + Path.DirectorySeparatorChar;
  100. }
  101. public static string [] GetFiles (string path)
  102. {
  103. return GetFiles (path, "*");
  104. }
  105. public static string [] GetFiles (string path, string pattern)
  106. {
  107. return GetFileSystemEntries (path, pattern, FileAttributes.Directory, 0);
  108. }
  109. public static string [] GetFileSystemEntries (string path)
  110. {
  111. return GetFileSystemEntries (path, "*");
  112. }
  113. public static string [] GetFileSystemEntries (string path, string pattern)
  114. {
  115. return GetFileSystemEntries (path, pattern, 0, 0);
  116. }
  117. public static string[] GetLogicalDrives ()
  118. {
  119. return new string [] { "A:\\", "C:\\" };
  120. }
  121. public static DirectoryInfo GetParent (string path)
  122. {
  123. return new DirectoryInfo (Path.GetDirectoryName (path));
  124. }
  125. public static void Move (string src, string dest)
  126. {
  127. File.Move (src, dest);
  128. }
  129. public static void SetCreationTime (string path, DateTime creation_time)
  130. {
  131. File.SetCreationTime (path, creation_time);
  132. }
  133. public static void SetCurrentDirectory (string path)
  134. {
  135. /*
  136. // Implementation complete 08/25/2001 14:24 except for
  137. // LAMESPEC: documentation specifies invalid exceptions IOException (i think)
  138. CheckArgument.Path (path, true);
  139. CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, path);
  140. */
  141. if (!Exists (path))
  142. {
  143. throw new DirectoryNotFoundException ("Directory \"" + path + "\" not found.");
  144. }
  145. Environment.CurrentDirectory = path;
  146. }
  147. public static void SetLastAccessTime (string path, DateTime last_access_time)
  148. {
  149. File.SetLastAccessTime (path, last_access_time);
  150. }
  151. public static void SetLastWriteTime (string path, DateTime last_write_time)
  152. {
  153. File.SetLastWriteTime (path, last_write_time);
  154. }
  155. // private
  156. private static string [] GetFileSystemEntries (string path, string pattern, FileAttributes mask, FileAttributes attrs)
  157. {
  158. SearchPattern search;
  159. MonoIOStat stat;
  160. IntPtr find;
  161. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  162. throw new ArgumentException ("Path contains invalid characters.");
  163. search = new SearchPattern (pattern);
  164. find = MonoIO.FindFirstFile (Path.Combine (path , "*"), out stat);
  165. if (find == MonoIO.InvalidHandle) {
  166. switch (MonoIO.GetLastError ()) {
  167. case MonoIOError.ERROR_FILE_NOT_FOUND:
  168. case MonoIOError.ERROR_PATH_NOT_FOUND:
  169. string message = String.Format ("Could not find a part of the path \"{0}\"", path);
  170. throw new DirectoryNotFoundException (message);
  171. default:
  172. throw MonoIO.GetException (path);
  173. }
  174. }
  175. ArrayList entries = new ArrayList ();
  176. while (true) {
  177. // Ignore entries of "." and ".." -
  178. // the documentation doesn't mention
  179. // it (surprise!) but empirical
  180. // testing indicates .net never
  181. // returns "." or ".." in a
  182. // GetDirectories() list.
  183. if ((stat.Attributes & mask) == attrs &&
  184. search.IsMatch (stat.Name) &&
  185. stat.Name != "." &&
  186. stat.Name != "..")
  187. entries.Add (Path.Combine (path, stat.Name));
  188. if (!MonoIO.FindNextFile (find, out stat))
  189. break;
  190. }
  191. MonoIO.FindClose (find);
  192. return (string []) entries.ToArray (typeof (string));
  193. }
  194. }
  195. }