2002-08-09 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / System.IO / Directory.cs
1 // \r
2 // System.IO.Directory.cs \r
3 //\r
4 // Authors:\r
5 //   Jim Richardson  (develop@wtfo-guru.com)\r
6 //   Miguel de Icaza (miguel@ximian.com)\r
7 //   Dan Lewis       (dihlewis@yahoo.co.uk)\r
8 //\r
9 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
10 // Copyright (C) 2002 Ximian, Inc.\r
11 // \r
12 // Created:        Monday, August 13, 2001 \r
13 //\r
14 //------------------------------------------------------------------------------\r
15 \r
16 using System;\r
17 using System.Security.Permissions;\r
18 using System.Collections;\r
19 \r
20 namespace System.IO\r
21 {\r
22         public sealed class Directory : Object\r
23         {\r
24                 private Directory () {}
25
26                 public static DirectoryInfo CreateDirectory (string path)\r
27                 {\r
28                         if (path == null)\r
29                                 throw new ArgumentNullException ();\r
30                         if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)\r
31                                 throw new ArgumentException ();\r
32 \r
33                         if (!MonoIO.CreateDirectory (path))\r
34                                 throw MonoIO.GetException ();\r
35 \r
36                         return new DirectoryInfo (path);\r
37                 }\r
38 \r
39                 public static void Delete (string path)\r
40                 {\r
41                         if (path == null)\r
42                                 throw new ArgumentNullException ();\r
43                         if (path == "" || path.IndexOfAny (Path.InvalidPathChars) != -1)\r
44                                 throw new ArgumentException ();\r
45 \r
46                         if (!MonoIO.RemoveDirectory (path))\r
47                                 throw MonoIO.GetException ();\r
48                 }\r
49 \r
50                 static void RecursiveDelete (string path)\r
51                 {\r
52                         foreach (string dir in GetDirectories (path))\r
53                                 RecursiveDelete (dir);\r
54 \r
55                         foreach (string file in GetFiles (path))\r
56                                 File.Delete (file);\r
57 \r
58                         Directory.Delete (path);\r
59                 }\r
60                 \r
61                 public static void Delete (string path, bool recurse)\r
62                 {\r
63                         if (path == null)\r
64                                 throw new ArgumentNullException ();\r
65                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)\r
66                                 throw new ArgumentException ("Path contains invalid characters");\r
67 \r
68                         if (recurse == false){\r
69                                 Delete (path);\r
70                                 return;\r
71                         }\r
72 \r
73                         RecursiveDelete (path);\r
74                 }\r
75                 \r
76                 public static bool Exists (string path)\r
77                 {\r
78                         return MonoIO.ExistsDirectory (path);\r
79                 }\r
80 \r
81                 public static DateTime GetLastAccessTime (string path)\r
82                 {\r
83                         return File.GetLastAccessTime (path);\r
84                 }\r
85                 \r
86                 public static DateTime GetLastWriteTime (string path)\r
87                 {\r
88                         return File.GetLastWriteTime (path);\r
89                 }\r
90                 \r
91                 public static DateTime GetCreationTime (string path)\r
92                 {\r
93                         return File.GetLastWriteTime (path);\r
94                 }\r
95                 \r
96                 public static string GetCurrentDirectory ()\r
97                 {\r
98                         /*\r
99                         // Implementation complete 08/25/2001 14:24 except for\r
100                         // LAMESPEC: documentation specifies invalid exceptions (i think)\r
101                         //           also shouldn't need Write to getcurrrent should we?\r
102                         string str = Environment.CurrentDirectory;\r
103                         CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, str);\r
104                         */\r
105                         return Environment.CurrentDirectory;\r
106                 }\r
107                 \r
108                 public static string [] GetDirectories (string path)\r
109                 {\r
110                         return GetDirectories (path, "*");\r
111                 }\r
112                 \r
113                 public static string [] GetDirectories (string path, string pattern)\r
114                 {\r
115                         return GetFileSystemEntries (path, pattern, FileAttributes.Directory, FileAttributes.Directory);\r
116                 }\r
117                 \r
118                 public static string GetDirectoryRoot (string path)\r
119                 {\r
120                         return "" + Path.DirectorySeparatorChar;\r
121                 }\r
122                 \r
123                 public static string [] GetFiles (string path)\r
124                 {\r
125                         return GetFiles (path, "*");\r
126                 }\r
127                 \r
128                 public static string [] GetFiles (string path, string pattern)\r
129                 {\r
130                         return GetFileSystemEntries (path, pattern, FileAttributes.Directory, 0);\r
131                 }\r
132 \r
133                 public static string [] GetFileSystemEntries (string path)\r
134                 {       \r
135                         return GetFileSystemEntries (path, "*");\r
136                 }\r
137 \r
138                 public static string [] GetFileSystemEntries (string path, string pattern)\r
139                 {\r
140                         return GetFileSystemEntries (path, pattern, 0, 0);\r
141                 }\r
142                 \r
143                 public static string[] GetLogicalDrives ()\r
144                 {       \r
145                         return new string [] { "A:\\", "C:\\" };\r
146                 }\r
147 \r
148                 public static DirectoryInfo GetParent (string path)\r
149                 {\r
150                         return new DirectoryInfo (Path.GetDirectoryName (path));\r
151                 }\r
152 \r
153                 public static void Move (string src, string dest)\r
154                 {\r
155                         File.Move (src, dest);\r
156                 }\r
157 \r
158                 public static void SetCreationTime (string path, DateTime creation_time)\r
159                 {\r
160                         File.SetCreationTime (path, creation_time);\r
161                 }\r
162                 \r
163                 public static void SetCurrentDirectory (string path)\r
164                 {\r
165                         /*\r
166                         // Implementation complete 08/25/2001 14:24 except for\r
167                         // LAMESPEC: documentation specifies invalid exceptions IOException (i think)\r
168                         CheckArgument.Path (path, true);\r
169                         CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, path);      \r
170                         */\r
171                         if (!Exists (path))\r
172                         {\r
173                                 throw new DirectoryNotFoundException ("Directory \"" + path + "\" not found.");\r
174                         }\r
175                         Environment.CurrentDirectory = path;\r
176                 }\r
177 \r
178                 public static void SetLastAccessTime (string path, DateTime last_access_time)\r
179                 {\r
180                         File.SetLastAccessTime (path, last_access_time);\r
181                 }\r
182                 \r
183                 public static void SetLastWriteTime (string path, DateTime last_write_time)\r
184                 {\r
185                         File.SetLastWriteTime (path, last_write_time);\r
186                 }\r
187                 \r
188                 // private\r
189 \r
190                 private static string [] GetFileSystemEntries (string path, string pattern, FileAttributes mask, FileAttributes attrs)\r
191                 {\r
192                         SearchPattern search;\r
193                         MonoIOStat stat;\r
194                         IntPtr find;\r
195 \r
196                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)\r
197                                 throw new ArgumentException ("Path contains invalid characters.");\r
198 \r
199                         if (!Directory.Exists (path))
200                                 throw new DirectoryNotFoundException ("Directory '" + path + "' not found.");
201
202                         search = new SearchPattern (pattern);\r
203 \r
204                         find = MonoIO.FindFirstFile (Path.Combine (path , "*"), out stat);\r
205                         if (find == MonoIO.InvalidHandle) {\r
206                                 switch (MonoIO.GetLastError ()) {\r
207                                 case MonoIOError.ERROR_FILE_NOT_FOUND:\r
208                                 case MonoIOError.ERROR_PATH_NOT_FOUND:\r
209                                         string message = String.Format ("Could not find a part of the path \"{0}\"", path);\r
210                                         throw new DirectoryNotFoundException (message);\r
211                                 case MonoIOError.ERROR_NO_MORE_FILES:
212                                         return new string [0];
213 \r
214                                 default:\r
215                                         throw MonoIO.GetException (path);\r
216                                 }\r
217                         }\r
218                         \r
219                         ArrayList entries = new ArrayList ();\r
220 \r
221                         while (true) {\r
222                                 // Ignore entries of "." and ".." -
223                                 // the documentation doesn't mention
224                                 // it (surprise!) but empirical
225                                 // testing indicates .net never
226                                 // returns "." or ".." in a
227                                 // GetDirectories() list.
228                                 if ((stat.Attributes & mask) == attrs &&
229                                     search.IsMatch (stat.Name) &&
230                                     stat.Name != "." &&
231                                     stat.Name != "..")\r
232                                         entries.Add (Path.Combine (path, stat.Name));\r
233 \r
234                                 if (!MonoIO.FindNextFile (find, out stat))\r
235                                         break;\r
236                         }\r
237                         MonoIO.FindClose (find);\r
238 \r
239                         return (string []) entries.ToArray (typeof (string));\r
240                 }\r
241         }\r
242 }\r