2 // System.IO.Directory.cs
\r
5 // Jim Richardson (develop@wtfo-guru.com)
\r
6 // Miguel de Icaza (miguel@ximian.com)
\r
7 // Dan Lewis (dihlewis@yahoo.co.uk)
\r
9 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
\r
10 // Copyright (C) 2002 Ximian, Inc.
\r
12 // Created: Monday, August 13, 2001
\r
14 //------------------------------------------------------------------------------
\r
17 using System.Security.Permissions;
\r
18 using System.Collections;
\r
22 public sealed class Directory : Object
\r
24 private Directory () {}
26 public static DirectoryInfo CreateDirectory (string path)
\r
29 throw new ArgumentNullException ();
\r
30 if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)
\r
31 throw new ArgumentException ();
\r
33 if (!MonoIO.CreateDirectory (path))
\r
34 throw MonoIO.GetException ();
\r
36 return new DirectoryInfo (path);
\r
39 public static void Delete (string path)
\r
42 throw new ArgumentNullException ();
\r
43 if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)
\r
44 throw new ArgumentException ();
\r
46 if (!MonoIO.RemoveDirectory (path))
\r
47 throw MonoIO.GetException ();
\r
50 static void RecursiveDelete (string path)
\r
52 foreach (string dir in GetDirectories (path))
\r
53 RecursiveDelete (dir);
\r
55 foreach (string file in GetFiles (path))
\r
58 Directory.Delete (path);
\r
61 public static void Delete (string path, bool recurse)
\r
64 throw new ArgumentNullException ();
\r
65 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
\r
66 throw new ArgumentException ("Path contains invalid characters");
\r
68 if (recurse == false){
\r
73 RecursiveDelete (path);
\r
76 public static bool Exists (string path)
\r
78 return MonoIO.ExistsDirectory (path);
\r
81 public static DateTime GetLastAccessTime (string path)
\r
83 return File.GetLastAccessTime (path);
\r
86 public static DateTime GetLastWriteTime (string path)
\r
88 return File.GetLastWriteTime (path);
\r
91 public static DateTime GetCreationTime (string path)
\r
93 return File.GetLastWriteTime (path);
\r
96 public static string GetCurrentDirectory ()
\r
99 // Implementation complete 08/25/2001 14:24 except for
\r
100 // LAMESPEC: documentation specifies invalid exceptions (i think)
\r
101 // also shouldn't need Write to getcurrrent should we?
\r
102 string str = Environment.CurrentDirectory;
\r
103 CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, str);
\r
105 return Environment.CurrentDirectory;
\r
108 public static string [] GetDirectories (string path)
\r
110 return GetDirectories (path, "*");
\r
113 public static string [] GetDirectories (string path, string pattern)
\r
115 return GetFileSystemEntries (path, pattern, FileAttributes.Directory, FileAttributes.Directory);
\r
118 public static string GetDirectoryRoot (string path)
\r
120 return "" + Path.DirectorySeparatorChar;
\r
123 public static string [] GetFiles (string path)
\r
125 return GetFiles (path, "*");
\r
128 public static string [] GetFiles (string path, string pattern)
\r
130 return GetFileSystemEntries (path, pattern, FileAttributes.Directory, 0);
\r
133 public static string [] GetFileSystemEntries (string path)
\r
135 return GetFileSystemEntries (path, "*");
\r
138 public static string [] GetFileSystemEntries (string path, string pattern)
\r
140 return GetFileSystemEntries (path, pattern, 0, 0);
\r
143 public static string[] GetLogicalDrives ()
\r
145 return new string [] { "A:\\", "C:\\" };
\r
148 public static DirectoryInfo GetParent (string path)
\r
150 return new DirectoryInfo (Path.GetDirectoryName (path));
\r
153 public static void Move (string src, string dest)
\r
155 File.Move (src, dest);
\r
158 public static void SetCreationTime (string path, DateTime creation_time)
\r
160 File.SetCreationTime (path, creation_time);
\r
163 public static void SetCurrentDirectory (string path)
\r
166 // Implementation complete 08/25/2001 14:24 except for
\r
167 // LAMESPEC: documentation specifies invalid exceptions IOException (i think)
\r
168 CheckArgument.Path (path, true);
\r
169 CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, path);
\r
171 if (!Exists (path))
\r
173 throw new DirectoryNotFoundException ("Directory \"" + path + "\" not found.");
\r
175 Environment.CurrentDirectory = path;
\r
178 public static void SetLastAccessTime (string path, DateTime last_access_time)
\r
180 File.SetLastAccessTime (path, last_access_time);
\r
183 public static void SetLastWriteTime (string path, DateTime last_write_time)
\r
185 File.SetLastWriteTime (path, last_write_time);
\r
190 private static string [] GetFileSystemEntries (string path, string pattern, FileAttributes mask, FileAttributes attrs)
\r
192 SearchPattern search;
\r
196 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
\r
197 throw new ArgumentException ("Path contains invalid characters.");
\r
199 if (!Directory.Exists (path))
200 throw new DirectoryNotFoundException ("Directory '" + path + "' not found.");
202 search = new SearchPattern (pattern);
\r
204 find = MonoIO.FindFirstFile (Path.Combine (path , "*"), out stat);
\r
205 if (find == MonoIO.InvalidHandle) {
\r
206 switch (MonoIO.GetLastError ()) {
\r
207 case MonoIOError.ERROR_FILE_NOT_FOUND:
\r
208 case MonoIOError.ERROR_PATH_NOT_FOUND:
\r
209 string message = String.Format ("Could not find a part of the path \"{0}\"", path);
\r
210 throw new DirectoryNotFoundException (message);
\r
211 case MonoIOError.ERROR_NO_MORE_FILES:
212 return new string [0];
215 throw MonoIO.GetException (path);
\r
219 ArrayList entries = new ArrayList ();
\r
222 // Ignore entries of "." and ".." -
223 // the documentation doesn't mention
224 // it (surprise!) but empirical
225 // testing indicates .net never
226 // returns "." or ".." in a
227 // GetDirectories() list.
228 if ((stat.Attributes & mask) == attrs &&
229 search.IsMatch (stat.Name) &&
232 entries.Add (Path.Combine (path, stat.Name));
\r
234 if (!MonoIO.FindNextFile (find, out stat))
\r
237 MonoIO.FindClose (find);
\r
239 return (string []) entries.ToArray (typeof (string));
\r