Directory.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // Eduardo Garcia ([email protected])
  9. //
  10. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  11. // Copyright (C) 2002 Ximian, Inc.
  12. //
  13. // Created: Monday, August 13, 2001
  14. //
  15. //------------------------------------------------------------------------------
  16. using System;
  17. using System.Security.Permissions;
  18. using System.Collections;
  19. using System.Text;
  20. namespace System.IO
  21. {
  22. public sealed class Directory : Object
  23. {
  24. private Directory () {}
  25. public static DirectoryInfo CreateDirectory (string path)
  26. {
  27. if (path == null)
  28. throw new ArgumentNullException ("path");
  29. if (path == "")
  30. throw new ArgumentException ("Path is empty");
  31. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  32. throw new ArgumentException ("Path contains invalid chars");
  33. if (path.Trim ().Length == 0)
  34. throw new ArgumentException ("Only blank characters in path");
  35. if (path == ":")
  36. throw new NotSupportedException ("Only ':' In path");
  37. return CreateDirectoriesInternal (path);
  38. }
  39. static DirectoryInfo CreateDirectoriesInternal (string path)
  40. {
  41. DirectoryInfo info = new DirectoryInfo (path);
  42. if (info.Parent != null && !info.Parent.Exists)
  43. info.Parent.Create ();
  44. MonoIOError error;
  45. if (!MonoIO.CreateDirectory (path, out error)) {
  46. if (error != MonoIOError.ERROR_ALREADY_EXISTS)
  47. throw MonoIO.GetException (error);
  48. }
  49. return info;
  50. }
  51. public static void Delete (string path)
  52. {
  53. if (path == null) {
  54. throw new ArgumentNullException ("path");
  55. }
  56. if (path == "") {
  57. throw new ArgumentException ("Path is empty");
  58. }
  59. if (path.IndexOfAny (Path.InvalidPathChars) != -1) {
  60. throw new ArgumentException ("Path contains invalid chars");
  61. }
  62. if (path.Trim().Length == 0)
  63. throw new ArgumentException ("Only blank characters in path");
  64. if (path == ":")
  65. throw new NotSupportedException ("Only ':' In path");
  66. MonoIOError error;
  67. if (!MonoIO.RemoveDirectory (path, out error)) {
  68. throw MonoIO.GetException (error);
  69. }
  70. }
  71. static void RecursiveDelete (string path)
  72. {
  73. foreach (string dir in GetDirectories (path))
  74. RecursiveDelete (dir);
  75. foreach (string file in GetFiles (path))
  76. File.Delete (file);
  77. Directory.Delete (path);
  78. }
  79. public static void Delete (string path, bool recurse)
  80. {
  81. if (path == null)
  82. throw new ArgumentNullException ();
  83. if (path == "")
  84. throw new System.ArgumentException("Path is Empty");
  85. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  86. throw new ArgumentException ("Path contains invalid characters");
  87. if (path.Trim().Length == 0)
  88. throw new ArgumentException ("Only blank characters in path");
  89. if (path == ":")
  90. throw new NotSupportedException ("Only ':' In path");
  91. if (recurse == false){
  92. Delete (path);
  93. return;
  94. }
  95. RecursiveDelete (path);
  96. }
  97. public static bool Exists (string path)
  98. {
  99. if (path == null)
  100. return false;
  101. MonoIOError error;
  102. return MonoIO.ExistsDirectory (path, out error);
  103. }
  104. public static DateTime GetLastAccessTime (string path)
  105. {
  106. return File.GetLastAccessTime (path);
  107. }
  108. public static DateTime GetLastWriteTime (string path)
  109. {
  110. return File.GetLastWriteTime (path);
  111. }
  112. public static DateTime GetCreationTime (string path)
  113. {
  114. if (path == null)
  115. throw new System.ArgumentNullException("Path is Null");
  116. if (path == "")
  117. throw new System.ArgumentException("Path is Empty");
  118. if (path.Trim().Length == 0)
  119. throw new ArgumentException ("Only blank characters in path");
  120. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  121. throw new ArgumentException ("Path contains invalid chars");
  122. if (path == ":")
  123. throw new NotSupportedException ("Only ':' In path");
  124. return File.GetLastWriteTime (path);
  125. }
  126. public static string GetCurrentDirectory ()
  127. {
  128. /*
  129. // Implementation complete 08/25/2001 14:24 except for
  130. // LAMESPEC: documentation specifies invalid exceptions (i think)
  131. // also shouldn't need Write to getcurrrent should we?
  132. string str = Environment.CurrentDirectory;
  133. CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, str);
  134. */
  135. return Environment.CurrentDirectory;
  136. }
  137. public static string [] GetDirectories (string path)
  138. {
  139. return GetDirectories (path, "*");
  140. }
  141. public static string [] GetDirectories (string path, string pattern)
  142. {
  143. return GetFileSystemEntries (path, pattern, FileAttributes.Directory, FileAttributes.Directory);
  144. }
  145. public static string GetDirectoryRoot (string path)
  146. {
  147. return new String(Path.DirectorySeparatorChar,1);
  148. }
  149. public static string [] GetFiles (string path)
  150. {
  151. return GetFiles (path, "*");
  152. }
  153. public static string [] GetFiles (string path, string pattern)
  154. {
  155. return GetFileSystemEntries (path, pattern, FileAttributes.Directory, 0);
  156. }
  157. public static string [] GetFileSystemEntries (string path)
  158. {
  159. return GetFileSystemEntries (path, "*");
  160. }
  161. public static string [] GetFileSystemEntries (string path, string pattern)
  162. {
  163. return GetFileSystemEntries (path, pattern, 0, 0);
  164. }
  165. public static string[] GetLogicalDrives ()
  166. {
  167. //FIXME: Hardcoded Paths
  168. if (Environment.OSVersion.Platform == PlatformID.Unix)
  169. return new string[] { "/" };
  170. else
  171. return new string [] { "A:\\", "C:\\" };
  172. }
  173. public static DirectoryInfo GetParent (string path)
  174. {
  175. if (path == null)
  176. throw new ArgumentNullException ();
  177. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  178. throw new ArgumentException ("Path contains invalid characters");
  179. if (path == "")
  180. throw new ArgumentException ("The Path do not have a valid format");
  181. return new DirectoryInfo (Path.GetDirectoryName (path));
  182. }
  183. public static void Move (string src, string dest)
  184. {
  185. if (src == null)
  186. throw new ArgumentNullException ("src");
  187. if (dest == null)
  188. throw new ArgumentNullException ("dest");
  189. if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
  190. throw new ArgumentException ("Invalid source directory name: " + src, "src");
  191. if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
  192. throw new ArgumentException ("Invalid target directory name: " + dest, "dest");
  193. if (!Exists (src))
  194. throw new DirectoryNotFoundException (src + " does not exist");
  195. MonoIOError error;
  196. if (!MonoIO.MoveFile (src, dest, out error))
  197. throw MonoIO.GetException (error);
  198. }
  199. public static void SetCreationTime (string path, DateTime creation_time)
  200. {
  201. File.SetCreationTime (path, creation_time);
  202. }
  203. public static void SetCurrentDirectory (string path)
  204. {
  205. /*
  206. // Implementation complete 08/25/2001 14:24 except for
  207. // LAMESPEC: documentation specifies invalid exceptions IOException (i think)
  208. CheckArgument.Path (path, true);
  209. CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, path);
  210. */
  211. if (!Exists (path))
  212. {
  213. throw new DirectoryNotFoundException ("Directory \"" + path + "\" not found.");
  214. }
  215. Environment.CurrentDirectory = path;
  216. }
  217. public static void SetLastAccessTime (string path, DateTime last_access_time)
  218. {
  219. File.SetLastAccessTime (path, last_access_time);
  220. }
  221. public static void SetLastWriteTime (string path, DateTime last_write_time)
  222. {
  223. File.SetLastWriteTime (path, last_write_time);
  224. }
  225. // private
  226. private static string [] GetFileSystemEntries (string path, string pattern, FileAttributes mask, FileAttributes attrs)
  227. {
  228. SearchPattern search;
  229. MonoIOStat stat;
  230. IntPtr find;
  231. if (path == null)
  232. throw new ArgumentNullException ();
  233. if (path == "")
  234. throw new ArgumentException ("The Path do not have a valid format");
  235. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  236. throw new ArgumentException ("Path contains invalid characters");
  237. if (!Directory.Exists (path)) {
  238. throw new DirectoryNotFoundException ("Directory '" + path + "' not found.");
  239. }
  240. search = new SearchPattern (pattern);
  241. MonoIOError error;
  242. find = MonoIO.FindFirstFile (Path.Combine (path , "*"), out stat, out error);
  243. if (find == MonoIO.InvalidHandle) {
  244. switch (error) {
  245. case MonoIOError.ERROR_FILE_NOT_FOUND:
  246. case MonoIOError.ERROR_PATH_NOT_FOUND:
  247. string message = String.Format ("Could not find a part of the path \"{0}\"", path);
  248. throw new DirectoryNotFoundException (message);
  249. case MonoIOError.ERROR_NO_MORE_FILES:
  250. return new string [0];
  251. default:
  252. throw MonoIO.GetException (path,
  253. error);
  254. }
  255. }
  256. ArrayList entries = new ArrayList ();
  257. while (true) {
  258. // Ignore entries of "." and ".." -
  259. // the documentation doesn't mention
  260. // it (surprise!) but empirical
  261. // testing indicates .net never
  262. // returns "." or ".." in a
  263. // GetDirectories() list.
  264. if ((stat.Attributes & mask) == attrs &&
  265. search.IsMatch (stat.Name) &&
  266. stat.Name != "." &&
  267. stat.Name != "..")
  268. entries.Add (Path.Combine (path, stat.Name));
  269. if (!MonoIO.FindNextFile (find, out stat,
  270. out error))
  271. break;
  272. }
  273. MonoIO.FindClose (find, out error);
  274. return (string []) entries.ToArray (typeof (string));
  275. }
  276. }
  277. }