New test.
[mono.git] / mcs / class / corlib / System.IO / DirectoryInfo.cs
1 // 
2 // System.IO.DirectoryInfo.cs 
3 //
4 // Authors:
5 //   Miguel de Icaza, miguel@ximian.com
6 //   Jim Richardson, develop@wtfo-guru.com
7 //   Dan Lewis, dihlewis@yahoo.co.uk
8 //   Sebastien Pouliot  <sebastien@ximian.com>
9 //
10 // Copyright (C) 2002 Ximian, Inc.
11 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
12 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Collections;
35 using System.Runtime.InteropServices;
36 using System.Text;
37 #if NET_2_0
38 using System.Security.AccessControl;
39 #endif
40
41 namespace System.IO {
42         
43         [Serializable]
44 #if NET_2_0
45         [ComVisible (true)]
46 #endif
47         public sealed class DirectoryInfo : FileSystemInfo {
48
49                 private string current;
50                 private string parent;
51         
52                 public DirectoryInfo (string path)
53                 {
54                         CheckPath (path);
55
56                         FullPath = Path.GetFullPath (path);
57                         OriginalPath = path;
58
59                         int len = FullPath.Length - 1;
60                         if ((len > 1) && (FullPath [len] == Path.DirectorySeparatorChar))
61                                 len--;
62                         int last = FullPath.LastIndexOf (Path.DirectorySeparatorChar, len);
63                         if ((last == -1) || ((last == 0) && (len == 0))) {
64                                 current = FullPath;
65                                 parent = null;
66                         } else {
67                                 current = FullPath.Substring (last + 1, len - last);
68                                 if (last == 0 && !Environment.IsRunningOnWindows)
69                                         parent = Path.DirectorySeparatorStr;
70                                 else
71                                         parent = FullPath.Substring (0, last);
72                                 // adjust for drives, i.e. a special case for windows
73                                 if (Environment.IsRunningOnWindows) {
74                                         if ((parent.Length == 2) && (parent [1] == ':') && Char.IsLetter (parent [0]))
75                                                 parent += Path.DirectorySeparatorChar;
76                                 }
77                         }
78                 }
79
80                 // properties
81
82                 public override bool Exists {
83                         get {
84                                 Refresh (false);
85
86                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
87                                         return false;
88
89                                 if ((stat.Attributes & FileAttributes.Directory) == 0)
90                                         return false;
91
92                                 return true;
93                         }
94                 }
95
96                 public override string Name {
97                         get { return current; }
98                 }
99
100                 public DirectoryInfo Parent {
101                         get {
102                                 if ((parent == null) || (parent.Length == 0))
103                                         return null;
104                                 return new DirectoryInfo (parent);
105                         }
106                 }
107
108                 public DirectoryInfo Root {
109                         get {
110                                 string root = Path.GetPathRoot (FullPath);
111                                 if (root == null)
112                                         return null;
113
114                                 return new DirectoryInfo (root);
115                         }
116                 }
117
118                 // creational methods
119
120                 public void Create () {
121                         Directory.CreateDirectory (FullPath);
122                 }
123
124                 public DirectoryInfo CreateSubdirectory (string name) {
125                         CheckPath (name);
126                         
127                         string path = Path.Combine (FullPath, name);
128                         Directory.CreateDirectory (path);
129
130                         return new DirectoryInfo (path);
131                 }
132
133                 // directory listing methods
134
135                 public FileInfo [] GetFiles () {
136                         return GetFiles ("*");
137                 }
138
139                 public FileInfo [] GetFiles (string pattern)
140                 {
141                         string [] names = Directory.GetFiles (FullPath, pattern);
142
143                         FileInfo[] infos = new FileInfo [names.Length];
144                         int i = 0;
145                         foreach (string name in names)
146                                 infos [i++] = new FileInfo (name);
147
148                         return infos;
149                 }
150
151                 public DirectoryInfo [] GetDirectories () {
152                         return GetDirectories ("*");
153                 }
154
155                 public DirectoryInfo [] GetDirectories (string pattern)
156                 {
157                         string [] names = Directory.GetDirectories (FullPath, pattern);
158
159                         DirectoryInfo[] infos = new DirectoryInfo [names.Length];
160                         int i = 0;
161                         foreach (string name in names)
162                                 infos [i++] = new DirectoryInfo (name);
163
164                         return infos;
165                 }
166
167                 public FileSystemInfo [] GetFileSystemInfos () {
168                         return GetFileSystemInfos ("*");
169                 }
170
171                 public FileSystemInfo [] GetFileSystemInfos (string pattern)
172                 {
173                         string[] dirs = Directory.GetDirectories (FullPath, pattern);
174                         string[] files = Directory.GetFiles (FullPath, pattern);
175
176                         FileSystemInfo[] infos = new FileSystemInfo [dirs.Length + files.Length];
177                         int i = 0;
178                         foreach (string dir in dirs)
179                                 infos [i++] = new DirectoryInfo (dir);
180                         foreach (string file in files)
181                                 infos [i++] = new FileInfo (file);
182
183                         return infos;
184                 }
185
186                 // directory management methods
187
188                 public override void Delete () {
189                         Delete (false);
190                 }
191
192                 public void Delete (bool recurse) {
193                         Directory.Delete (FullPath, recurse);
194                 }
195
196                 public void MoveTo (string dest) {
197                         Directory.Move (FullPath, Path.GetFullPath (dest));
198                 }
199
200                 public override string ToString () {
201                         return OriginalPath;
202                 }
203 #if NET_2_0
204                 // additional search methods
205
206                 [MonoTODO ("AllDirectories isn't implemented")]
207                 public DirectoryInfo[] GetDirectories (string pattern, SearchOption searchOption)
208                 {
209                         switch (searchOption) {
210                         case SearchOption.TopDirectoryOnly:
211                                 return GetDirectories (pattern);
212                         case SearchOption.AllDirectories:
213                                 throw new NotImplementedException ();
214                         default:
215                                 string msg = Locale.GetText ("Invalid enum value '{0}' for '{1}'.", searchOption, "SearchOption");
216                                 throw new ArgumentOutOfRangeException ("searchOption", msg);
217                         }
218                 }       
219
220                 [MonoTODO ("AllDirectories isn't implemented")]
221                 public FileInfo[] GetFiles (string pattern, SearchOption searchOption)
222                 {
223                         switch (searchOption) {
224                         case SearchOption.TopDirectoryOnly:
225                                 return GetFiles (pattern);
226                         case SearchOption.AllDirectories:
227                                 throw new NotImplementedException ();
228                         default:
229                                 string msg = Locale.GetText ("Invalid enum value '{0}' for '{1}'.", searchOption, "SearchOption");
230                                 throw new ArgumentOutOfRangeException ("searchOption", msg);
231                         }
232                 }
233
234                 // access control methods
235
236                 [MonoTODO ("DirectorySecurity isn't implemented")]
237                 public void Create (DirectorySecurity directorySecurity)
238                 {
239                         if (directorySecurity != null)
240                                 throw new NotImplementedException ();
241                         Create ();
242                 }
243
244                 [MonoTODO ("DirectorySecurity isn't implemented")]
245                 public DirectoryInfo CreateSubdirectory (string name, DirectorySecurity directorySecurity)
246                 {
247                         if (directorySecurity != null)
248                                 throw new NotImplementedException ();
249                         return CreateSubdirectory (name);
250                 }
251
252                 [MonoTODO ("DirectorySecurity isn't implemented")]
253                 public DirectorySecurity GetAccessControl ()
254                 {
255                         throw new NotImplementedException ();
256                 }
257
258                 [MonoTODO ("DirectorySecurity isn't implemented")]
259                 public DirectorySecurity GetAccessControl (AccessControlSections includeSections)
260                 {
261                         throw new NotImplementedException ();
262                 }
263
264                 [MonoTODO ("DirectorySecurity isn't implemented")]
265                 public void SetAccessControl (DirectorySecurity directorySecurity)
266                 {
267                         if (directorySecurity != null)
268                                 throw new ArgumentNullException ("directorySecurity");
269                         throw new NotImplementedException ();
270                 }
271 #endif
272         }
273 }