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
8 // Sebastien Pouliot <sebastien@ximian.com>
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)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 using System.Runtime.InteropServices;
38 using System.Security.AccessControl;
47 public sealed class DirectoryInfo : FileSystemInfo {
49 private string current;
50 private string parent;
52 public DirectoryInfo (string path)
56 FullPath = Path.GetFullPath (path);
59 int len = FullPath.Length - 1;
60 if ((len > 1) && (FullPath [len] == Path.DirectorySeparatorChar))
62 int last = FullPath.LastIndexOf (Path.DirectorySeparatorChar, len);
63 if ((last == -1) || ((last == 0) && (len == 0))) {
67 current = FullPath.Substring (last + 1, len - last);
68 if (last == 0 && !Environment.IsRunningOnWindows)
69 parent = Path.DirectorySeparatorStr;
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;
82 public override bool Exists {
86 if (stat.Attributes == MonoIO.InvalidFileAttributes)
89 if ((stat.Attributes & FileAttributes.Directory) == 0)
96 public override string Name {
97 get { return current; }
100 public DirectoryInfo Parent {
102 if ((parent == null) || (parent.Length == 0))
104 return new DirectoryInfo (parent);
108 public DirectoryInfo Root {
110 string root = Path.GetPathRoot (FullPath);
114 return new DirectoryInfo (root);
118 // creational methods
120 public void Create () {
121 Directory.CreateDirectory (FullPath);
124 public DirectoryInfo CreateSubdirectory (string name) {
127 string path = Path.Combine (FullPath, name);
128 Directory.CreateDirectory (path);
130 return new DirectoryInfo (path);
133 // directory listing methods
135 public FileInfo [] GetFiles () {
136 return GetFiles ("*");
139 public FileInfo [] GetFiles (string pattern)
141 string [] names = Directory.GetFiles (FullPath, pattern);
143 FileInfo[] infos = new FileInfo [names.Length];
145 foreach (string name in names)
146 infos [i++] = new FileInfo (name);
151 public DirectoryInfo [] GetDirectories () {
152 return GetDirectories ("*");
155 public DirectoryInfo [] GetDirectories (string pattern)
157 string [] names = Directory.GetDirectories (FullPath, pattern);
159 DirectoryInfo[] infos = new DirectoryInfo [names.Length];
161 foreach (string name in names)
162 infos [i++] = new DirectoryInfo (name);
167 public FileSystemInfo [] GetFileSystemInfos () {
168 return GetFileSystemInfos ("*");
171 public FileSystemInfo [] GetFileSystemInfos (string pattern)
173 string[] dirs = Directory.GetDirectories (FullPath, pattern);
174 string[] files = Directory.GetFiles (FullPath, pattern);
176 FileSystemInfo[] infos = new FileSystemInfo [dirs.Length + files.Length];
178 foreach (string dir in dirs)
179 infos [i++] = new DirectoryInfo (dir);
180 foreach (string file in files)
181 infos [i++] = new FileInfo (file);
186 // directory management methods
188 public override void Delete () {
192 public void Delete (bool recurse) {
193 Directory.Delete (FullPath, recurse);
196 public void MoveTo (string dest) {
197 Directory.Move (FullPath, Path.GetFullPath (dest));
200 public override string ToString () {
204 // additional search methods
206 [MonoTODO ("AllDirectories isn't implemented")]
207 public DirectoryInfo[] GetDirectories (string pattern, SearchOption searchOption)
209 switch (searchOption) {
210 case SearchOption.TopDirectoryOnly:
211 return GetDirectories (pattern);
212 case SearchOption.AllDirectories:
213 throw new NotImplementedException ();
215 string msg = Locale.GetText ("Invalid enum value '{0}' for '{1}'.", searchOption, "SearchOption");
216 throw new ArgumentOutOfRangeException ("searchOption", msg);
220 [MonoTODO ("AllDirectories isn't implemented")]
221 public FileInfo[] GetFiles (string pattern, SearchOption searchOption)
223 switch (searchOption) {
224 case SearchOption.TopDirectoryOnly:
225 return GetFiles (pattern);
226 case SearchOption.AllDirectories:
227 throw new NotImplementedException ();
229 string msg = Locale.GetText ("Invalid enum value '{0}' for '{1}'.", searchOption, "SearchOption");
230 throw new ArgumentOutOfRangeException ("searchOption", msg);
234 // access control methods
236 [MonoTODO ("DirectorySecurity isn't implemented")]
237 public void Create (DirectorySecurity directorySecurity)
239 if (directorySecurity != null)
240 throw new NotImplementedException ();
244 [MonoTODO ("DirectorySecurity isn't implemented")]
245 public DirectoryInfo CreateSubdirectory (string name, DirectorySecurity directorySecurity)
247 if (directorySecurity != null)
248 throw new NotImplementedException ();
249 return CreateSubdirectory (name);
252 [MonoTODO ("DirectorySecurity isn't implemented")]
253 public DirectorySecurity GetAccessControl ()
255 throw new NotImplementedException ();
258 [MonoTODO ("DirectorySecurity isn't implemented")]
259 public DirectorySecurity GetAccessControl (AccessControlSections includeSections)
261 throw new NotImplementedException ();
264 [MonoTODO ("DirectorySecurity isn't implemented")]
265 public void SetAccessControl (DirectorySecurity directorySecurity)
267 if (directorySecurity != null)
268 throw new ArgumentNullException ("directorySecurity");
269 throw new NotImplementedException ();