2005-11-24 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / DirectoryInfo.cs
1 // 
2 // System.IO.DirectoryInfo.cs 
3 //
4 // Author:
5 //   Miguel de Icaza, miguel@ximian.com
6 //   Jim Richardson, develop@wtfo-guru.com
7 //   Dan Lewis, dihlewis@yahoo.co.uk
8 //
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)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System.Collections;
34 using System.Runtime.InteropServices;
35
36 namespace System.IO {
37         
38         [Serializable]
39 #if NET_2_0
40         [ComVisible (true)]
41 #endif
42         public sealed class DirectoryInfo : FileSystemInfo {
43         
44                 public DirectoryInfo (string path)
45                 {
46                         CheckPath (path);
47
48                         FullPath = Path.GetFullPath (path);
49
50                         // Path.GetFullPath ends with / when it's the root directory (fix endless recursion problem)
51                         if (Path.GetPathRoot (path) != path) {
52                                 char end = path [path.Length - 1];
53                                 if ((end == Path.DirectorySeparatorChar) || (end == Path.AltDirectorySeparatorChar))
54                                         FullPath += Path.DirectorySeparatorChar;
55                         }
56
57                         OriginalPath = path;
58                 }
59
60                 // properties
61
62                 public override bool Exists {
63                         get {
64                                 Refresh (false);
65
66                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
67                                         return false;
68
69                                 if ((stat.Attributes & FileAttributes.Directory) == 0)
70                                         return false;
71
72                                 return true;
73                         }
74                 }
75
76                 public override string Name {
77                         get {
78                                 string result = Path.GetFileName (FullPath);
79                                 if (result == null || result == "")
80                                         return FullPath;
81                                 return result;
82                         }
83                 }
84
85                 public DirectoryInfo Parent {
86                         get {
87                                 string dirname = Path.GetDirectoryName (FullPath);
88                                 if (dirname == null)
89                                         return null;
90
91                                 return new DirectoryInfo (dirname);
92                         }
93                 }
94
95                 public DirectoryInfo Root {
96                         get {
97                                 string root = Path.GetPathRoot (FullPath);
98                                 if (root == null)
99                                         return null;
100
101                                 return new DirectoryInfo (root);
102                         }
103                 }
104
105                 // creational methods
106
107                 public void Create () {
108                         Directory.CreateDirectory (FullPath);
109                 }
110
111                 public DirectoryInfo CreateSubdirectory (string name) {
112                         CheckPath (name);
113                         
114                         string path = Path.Combine (FullPath, name);
115                         Directory.CreateDirectory (path);
116
117                         return new DirectoryInfo (path);
118                 }
119
120                 // directory listing methods
121
122                 public FileInfo [] GetFiles () {
123                         return GetFiles ("*");
124                 }
125
126                 public FileInfo [] GetFiles (string pattern) {
127                         string [] names = Directory.GetFiles (FullPath, pattern);
128
129                         ArrayList infos = new ArrayList ();
130                         foreach (string name in names)
131                                 infos.Add (new FileInfo (name));
132
133                         return (FileInfo []) infos.ToArray (typeof (FileInfo));
134                 }
135
136                 public DirectoryInfo [] GetDirectories () {
137                         return GetDirectories ("*");
138                 }
139
140                 public DirectoryInfo [] GetDirectories (string pattern) {
141                         string [] names = Directory.GetDirectories (FullPath, pattern);
142
143                         ArrayList infos = new ArrayList ();
144                         foreach (string name in names)
145                                 infos.Add (new DirectoryInfo (name));
146
147                         return (DirectoryInfo []) infos.ToArray (typeof (DirectoryInfo));
148                 }
149
150                 public FileSystemInfo [] GetFileSystemInfos () {
151                         return GetFileSystemInfos ("*");
152                 }
153
154                 public FileSystemInfo [] GetFileSystemInfos (string pattern) {
155                         ArrayList infos = new ArrayList ();
156                         infos.AddRange (GetDirectories (pattern));
157                         infos.AddRange (GetFiles (pattern));
158
159                         return (FileSystemInfo []) infos.ToArray (typeof (FileSystemInfo));
160                 }
161
162                 // directory management methods
163
164                 public override void Delete () {
165                         Delete (false);
166                 }
167
168                 public void Delete (bool recurse) {
169                         Directory.Delete (FullPath, recurse);
170                 }
171
172                 public void MoveTo (string dest) {
173                         Directory.Move (FullPath, Path.GetFullPath (dest));
174                 }
175
176                 public override string ToString () {
177                         return OriginalPath;
178                 }
179         }
180 }