2 // System.IO.DirectoryInfo.cs
5 // Miguel de Icaza, miguel@ximian.com
6 // Jim Richardson, develop@wtfo-guru.com
7 // Dan Lewis, dihlewis@yahoo.co.uk
9 // Copyright (C) 2002 Ximian, Inc.
10 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
11 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Runtime.InteropServices;
42 public sealed class DirectoryInfo : FileSystemInfo {
44 public DirectoryInfo (string path)
48 FullPath = Path.GetFullPath (path);
50 // Path.GetFullPath ends with / when it's the root directory (fix endless recursion problem)
51 if ((path.Length > 1) || ((path [0] != Path.DirectorySeparatorChar) && (path [0] != Path.AltDirectorySeparatorChar))) {
52 char end = path [path.Length - 1];
53 if ((end == Path.DirectorySeparatorChar) || (end == Path.AltDirectorySeparatorChar))
54 FullPath += Path.DirectorySeparatorChar;
62 public override bool Exists {
66 if (stat.Attributes == MonoIO.InvalidFileAttributes)
69 if ((stat.Attributes & FileAttributes.Directory) == 0)
76 public override string Name {
78 string result = Path.GetFileName (FullPath);
79 if (result == null || result == "")
85 public DirectoryInfo Parent {
87 string dirname = Path.GetDirectoryName (FullPath);
91 return new DirectoryInfo (dirname);
95 public DirectoryInfo Root {
97 string root = Path.GetPathRoot (FullPath);
101 return new DirectoryInfo (root);
105 // creational methods
107 public void Create () {
108 Directory.CreateDirectory (FullPath);
111 public DirectoryInfo CreateSubdirectory (string name) {
114 string path = Path.Combine (FullPath, name);
115 Directory.CreateDirectory (path);
117 return new DirectoryInfo (path);
120 // directory listing methods
122 public FileInfo [] GetFiles () {
123 return GetFiles ("*");
126 public FileInfo [] GetFiles (string pattern) {
127 string [] names = Directory.GetFiles (FullPath, pattern);
129 ArrayList infos = new ArrayList ();
130 foreach (string name in names)
131 infos.Add (new FileInfo (name));
133 return (FileInfo []) infos.ToArray (typeof (FileInfo));
136 public DirectoryInfo [] GetDirectories () {
137 return GetDirectories ("*");
140 public DirectoryInfo [] GetDirectories (string pattern) {
141 string [] names = Directory.GetDirectories (FullPath, pattern);
143 ArrayList infos = new ArrayList ();
144 foreach (string name in names)
145 infos.Add (new DirectoryInfo (name));
147 return (DirectoryInfo []) infos.ToArray (typeof (DirectoryInfo));
150 public FileSystemInfo [] GetFileSystemInfos () {
151 return GetFileSystemInfos ("*");
154 public FileSystemInfo [] GetFileSystemInfos (string pattern) {
155 ArrayList infos = new ArrayList ();
156 infos.AddRange (GetDirectories (pattern));
157 infos.AddRange (GetFiles (pattern));
159 return (FileSystemInfo []) infos.ToArray (typeof (FileSystemInfo));
162 // directory management methods
164 public override void Delete () {
168 public void Delete (bool recurse) {
169 Directory.Delete (FullPath, recurse);
172 public void MoveTo (string dest) {
173 Directory.Move (FullPath, Path.GetFullPath (dest));
176 public override string ToString () {