2 // System.IO.Directory.cs
5 // Jim Richardson (develop@wtfo-guru.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Dan Lewis (dihlewis@yahoo.co.uk)
8 // Eduardo Garcia (kiwnix@yahoo.es)
9 // Ville Palo (vi64pa@kolumbus.fi)
11 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
12 // Copyright (C) 2002 Ximian, Inc.
14 // Created: Monday, August 13, 2001
16 //------------------------------------------------------------------------------
19 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
21 // Permission is hereby granted, free of charge, to any person obtaining
22 // a copy of this software and associated documentation files (the
23 // "Software"), to deal in the Software without restriction, including
24 // without limitation the rights to use, copy, modify, merge, publish,
25 // distribute, sublicense, and/or sell copies of the Software, and to
26 // permit persons to whom the Software is furnished to do so, subject to
27 // the following conditions:
29 // The above copyright notice and this permission notice shall be
30 // included in all copies or substantial portions of the Software.
32 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 using System.Collections;
42 using System.Security;
43 using System.Security.Permissions;
58 private Directory () {}
61 public static DirectoryInfo CreateDirectory (string path)
64 throw new ArgumentNullException ("path");
67 throw new ArgumentException ("Path is empty");
69 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
70 throw new ArgumentException ("Path contains invalid chars");
72 if (path.Trim ().Length == 0)
73 throw new ArgumentException ("Only blank characters in path");
75 // LAMESPEC: with .net 1.0 version this throw NotSupportedException and msdn says so too
76 // but v1.1 throws ArgumentException.
78 throw new ArgumentException ("Only ':' In path");
80 return CreateDirectoriesInternal (path);
83 static DirectoryInfo CreateDirectoriesInternal (string path)
85 if (SecurityManager.SecurityEnabled) {
86 new FileIOPermission (FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, path).Demand ();
89 DirectoryInfo info = new DirectoryInfo (path);
90 if (info.Parent != null && !info.Parent.Exists)
91 info.Parent.Create ();
94 if (!MonoIO.CreateDirectory (path, out error)) {
95 // LAMESPEC: 1.1 and 1.2alpha allow CreateDirectory on a file path.
96 // So CreateDirectory ("/tmp/somefile") will succeed if 'somefile' is
97 // not a directory. However, 1.0 will throw an exception.
98 // We behave like 1.0 here (emulating 1.1-like behavior is just a matter
99 // of comparing error to ERROR_FILE_EXISTS, but it's lame to do:
100 // DirectoryInfo di = Directory.CreateDirectory (something);
101 // and having di.Exists return false afterwards.
102 // I hope we don't break anyone's code, as they should be catching
103 // the exception anyway.
104 if (error != MonoIOError.ERROR_ALREADY_EXISTS)
105 throw MonoIO.GetException (path, error);
111 public static void Delete (string path)
114 throw new ArgumentNullException ("path");
117 throw new ArgumentException ("Path is empty");
119 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
120 throw new ArgumentException ("Path contains invalid chars");
122 if (path.Trim().Length == 0)
123 throw new ArgumentException ("Only blank characters in path");
126 throw new NotSupportedException ("Only ':' In path");
130 if (!MonoIO.RemoveDirectory (path, out error)) {
133 * In io-layer/io.c rmdir returns error_file_not_found if directory does not exists.
134 * So maybe this could be handled somewhere else?
136 if (error == MonoIOError.ERROR_FILE_NOT_FOUND)
137 throw new DirectoryNotFoundException ("Directory '" + path + "' doesnt exists.");
139 throw MonoIO.GetException (path, error);
143 static void RecursiveDelete (string path)
145 foreach (string dir in GetDirectories (path))
146 RecursiveDelete (dir);
148 foreach (string file in GetFiles (path))
151 Directory.Delete (path);
154 public static void Delete (string path, bool recurse)
156 CheckPathExceptions (path);
158 if (recurse == false){
163 RecursiveDelete (path);
166 public static bool Exists (string path)
174 exists = MonoIO.ExistsDirectory (path, out error);
175 if (error != MonoIOError.ERROR_SUCCESS &&
176 error != MonoIOError.ERROR_PATH_NOT_FOUND) {
177 throw MonoIO.GetException (path, error);
183 public static DateTime GetLastAccessTime (string path)
185 return File.GetLastAccessTime (path);
188 public static DateTime GetLastAccessTimeUtc (string path)
190 return GetLastAccessTime (path).ToUniversalTime ();
193 public static DateTime GetLastWriteTime (string path)
195 return File.GetLastWriteTime (path);
198 public static DateTime GetLastWriteTimeUtc (string path)
200 return GetLastWriteTime (path).ToUniversalTime ();
203 public static DateTime GetCreationTime (string path)
205 return File.GetCreationTime (path);
208 public static DateTime GetCreationTimeUtc (string path)
210 return GetCreationTime (path).ToUniversalTime ();
213 public static string GetCurrentDirectory ()
217 string result = MonoIO.GetCurrentDirectory (out error);
218 if (error != MonoIOError.ERROR_SUCCESS)
219 throw MonoIO.GetException (error);
221 if ((result != null) && (result.Length > 0) && SecurityManager.SecurityEnabled) {
222 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, result).Demand ();
227 public static string [] GetDirectories (string path)
229 return GetDirectories (path, "*");
232 public static string [] GetDirectories (string path, string pattern)
234 return GetFileSystemEntries (path, pattern, FileAttributes.Directory, FileAttributes.Directory);
237 public static string GetDirectoryRoot (string path)
239 return new String(Path.DirectorySeparatorChar,1);
242 public static string [] GetFiles (string path)
244 return GetFiles (path, "*");
247 public static string [] GetFiles (string path, string pattern)
249 return GetFileSystemEntries (path, pattern, FileAttributes.Directory, 0);
254 public static string[] GetFiles (string path, string searchPattern, SearchOption searchOption)
256 throw new NotImplementedException ();
260 public static string [] GetFileSystemEntries (string path)
262 return GetFileSystemEntries (path, "*");
265 public static string [] GetFileSystemEntries (string path, string pattern)
267 return GetFileSystemEntries (path, pattern, 0, 0);
270 public static string[] GetLogicalDrives ()
272 return Environment.GetLogicalDrives ();
275 static bool IsRootDirectory (string path)
278 if (Path.DirectorySeparatorChar == '/' && path == "/")
282 if (Path.DirectorySeparatorChar == '\\')
283 if (path.Length == 3 && path.EndsWith (":\\"))
289 public static DirectoryInfo GetParent (string path)
292 throw new ArgumentNullException ();
293 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
294 throw new ArgumentException ("Path contains invalid characters");
296 throw new ArgumentException ("The Path do not have a valid format");
298 // return null if the path is the root directory
299 if (IsRootDirectory (path))
302 return new DirectoryInfo (Path.GetDirectoryName (path));
305 public static void Move (string src, string dest)
308 throw new ArgumentNullException ("src");
311 throw new ArgumentNullException ("dest");
313 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
314 throw new ArgumentException ("Invalid source directory name: " + src, "src");
316 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
317 throw new ArgumentException ("Invalid target directory name: " + dest, "dest");
320 throw new IOException ("Source directory cannot be same as a target directory.");
323 throw new IOException (dest + " already exists.");
326 throw new DirectoryNotFoundException (src + " does not exist");
329 if (!MonoIO.MoveFile (src, dest, out error))
330 throw MonoIO.GetException (error);
333 public static void SetCreationTime (string path, DateTime creation_time)
335 File.SetCreationTime (path, creation_time);
338 public static void SetCreationTimeUtc (string path, DateTime creation_time)
340 SetCreationTime (path, creation_time.ToLocalTime ());
343 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
344 public static void SetCurrentDirectory (string path)
347 throw new ArgumentNullException ("path");
348 if (path.Trim () == String.Empty)
349 throw new ArgumentException ("path string must not be an empty string or whitespace string");
354 throw new DirectoryNotFoundException ("Directory \"" +
355 path + "\" not found.");
357 MonoIO.SetCurrentDirectory (path, out error);
358 if (error != MonoIOError.ERROR_SUCCESS)
359 throw MonoIO.GetException (path, error);
362 public static void SetLastAccessTime (string path, DateTime last_access_time)
364 File.SetLastAccessTime (path, last_access_time);
367 public static void SetLastAccessTimeUtc (string path, DateTime last_access_time)
369 SetLastAccessTime (path, last_access_time.ToLocalTime ());
372 public static void SetLastWriteTime (string path, DateTime last_write_time)
374 File.SetLastWriteTime (path, last_write_time);
377 public static void SetLastWriteTimeUtc (string path, DateTime last_write_time)
379 SetLastWriteTime (path, last_write_time.ToLocalTime ());
384 private static void CheckPathExceptions (string path)
387 throw new System.ArgumentNullException("Path is Null");
389 throw new System.ArgumentException("Path is Empty");
390 if (path.Trim().Length == 0)
391 throw new ArgumentException ("Only blank characters in path");
392 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
393 throw new ArgumentException ("Path contains invalid chars");
396 private static string [] GetFileSystemEntries (string path, string pattern, FileAttributes mask, FileAttributes attrs)
398 if (path == null || pattern == null)
399 throw new ArgumentNullException ();
401 if (pattern == String.Empty)
402 return new string [] {};
404 if (path.Trim () == "")
405 throw new ArgumentException ("The Path does not have a valid format");
407 string wild = Path.Combine (path, pattern);
408 string wildpath = Path.GetDirectoryName (wild);
409 if (wildpath.IndexOfAny (Path.InvalidPathChars) != -1)
410 throw new ArgumentException ("Path contains invalid characters");
412 if (wildpath.IndexOfAny (Path.InvalidPathChars) != -1) {
413 if (path.IndexOfAny (SearchPattern.InvalidChars) == -1)
414 throw new ArgumentException ("Path contains invalid characters", "path");
416 throw new ArgumentException ("Pattern contains invalid characters", "pattern");
420 if (!MonoIO.ExistsDirectory (wildpath, out error)) {
421 if (error == MonoIOError.ERROR_SUCCESS) {
422 MonoIOError file_error;
423 if (MonoIO.ExistsFile (wildpath, out file_error)) {
424 return new string [] { wildpath };
428 if (error != MonoIOError.ERROR_PATH_NOT_FOUND)
429 throw MonoIO.GetException (wildpath, error);
431 if (wildpath.IndexOfAny (SearchPattern.WildcardChars) == -1)
432 throw new DirectoryNotFoundException ("Directory '" + wildpath + "' not found.");
434 if (path.IndexOfAny (SearchPattern.WildcardChars) == -1)
435 throw new ArgumentException ("Pattern is invalid", "pattern");
437 throw new ArgumentException ("Path is invalid", "path");
440 string [] result = MonoIO.GetFileSystemEntries (wildpath, pattern, (int) attrs, (int) mask, out error);
442 throw MonoIO.GetException (wildpath, error);