/
Directory.cs
346 lines (277 loc) · 9.39 KB
/
Directory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// System.IO.Directory.cs
//
// Authors:
// Jim Richardson (develop@wtfo-guru.com)
// Miguel de Icaza (miguel@ximian.com)
// Dan Lewis (dihlewis@yahoo.co.uk)
// Eduardo Garcia (kiwnix@yahoo.es)
//
// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
// Copyright (C) 2002 Ximian, Inc.
//
// Created: Monday, August 13, 2001
//
//------------------------------------------------------------------------------
using System;
using System.Security.Permissions;
using System.Collections;
using System.Text;
namespace System.IO
{
public sealed class Directory : Object
{
private Directory () {}
public static DirectoryInfo CreateDirectory (string path)
{
if (path == null)
throw new ArgumentNullException ("path");
if (path == "")
throw new ArgumentException ("Path is empty");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid chars");
if (path.Trim ().Length == 0)
throw new ArgumentException ("Only blank characters in path");
if (path == ":")
throw new NotSupportedException ("Only ':' In path");
return CreateDirectoriesInternal (path);
}
static DirectoryInfo CreateDirectoriesInternal (string path)
{
DirectoryInfo info = new DirectoryInfo (path);
if (info.Parent != null && !info.Parent.Exists)
info.Parent.Create ();
MonoIOError error;
if (!MonoIO.CreateDirectory (path, out error)) {
if (error != MonoIOError.ERROR_ALREADY_EXISTS)
throw MonoIO.GetException (error);
}
return info;
}
public static void Delete (string path)
{
if (path == null) {
throw new ArgumentNullException ("path");
}
if (path == "") {
throw new ArgumentException ("Path is empty");
}
if (path.IndexOfAny (Path.InvalidPathChars) != -1) {
throw new ArgumentException ("Path contains invalid chars");
}
if (path.Trim().Length == 0)
throw new ArgumentException ("Only blank characters in path");
if (path == ":")
throw new NotSupportedException ("Only ':' In path");
MonoIOError error;
if (!MonoIO.RemoveDirectory (path, out error)) {
throw MonoIO.GetException (error);
}
}
static void RecursiveDelete (string path)
{
foreach (string dir in GetDirectories (path))
RecursiveDelete (dir);
foreach (string file in GetFiles (path))
File.Delete (file);
Directory.Delete (path);
}
public static void Delete (string path, bool recurse)
{
if (path == null)
throw new ArgumentNullException ();
if (path == "")
throw new System.ArgumentException("Path is Empty");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid characters");
if (path.Trim().Length == 0)
throw new ArgumentException ("Only blank characters in path");
if (path == ":")
throw new NotSupportedException ("Only ':' In path");
if (recurse == false){
Delete (path);
return;
}
RecursiveDelete (path);
}
public static bool Exists (string path)
{
if (path == null)
return false;
MonoIOError error;
return MonoIO.ExistsDirectory (path, out error);
}
public static DateTime GetLastAccessTime (string path)
{
return File.GetLastAccessTime (path);
}
public static DateTime GetLastWriteTime (string path)
{
return File.GetLastWriteTime (path);
}
public static DateTime GetCreationTime (string path)
{
if (path == null)
throw new System.ArgumentNullException("Path is Null");
if (path == "")
throw new System.ArgumentException("Path is Empty");
if (path.Trim().Length == 0)
throw new ArgumentException ("Only blank characters in path");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid chars");
if (path == ":")
throw new NotSupportedException ("Only ':' In path");
return File.GetLastWriteTime (path);
}
public static string GetCurrentDirectory ()
{
/*
// Implementation complete 08/25/2001 14:24 except for
// LAMESPEC: documentation specifies invalid exceptions (i think)
// also shouldn't need Write to getcurrrent should we?
string str = Environment.CurrentDirectory;
CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, str);
*/
return Environment.CurrentDirectory;
}
public static string [] GetDirectories (string path)
{
return GetDirectories (path, "*");
}
public static string [] GetDirectories (string path, string pattern)
{
return GetFileSystemEntries (path, pattern, FileAttributes.Directory, FileAttributes.Directory);
}
public static string GetDirectoryRoot (string path)
{
return new String(Path.DirectorySeparatorChar,1);
}
public static string [] GetFiles (string path)
{
return GetFiles (path, "*");
}
public static string [] GetFiles (string path, string pattern)
{
return GetFileSystemEntries (path, pattern, FileAttributes.Directory, 0);
}
public static string [] GetFileSystemEntries (string path)
{
return GetFileSystemEntries (path, "*");
}
public static string [] GetFileSystemEntries (string path, string pattern)
{
return GetFileSystemEntries (path, pattern, 0, 0);
}
public static string[] GetLogicalDrives ()
{
//FIXME: Hardcoded Paths
if (Environment.OSVersion.Platform == PlatformID.Unix)
return new string[] { "/" };
else
return new string [] { "A:\\", "C:\\" };
}
public static DirectoryInfo GetParent (string path)
{
if (path == null)
throw new ArgumentNullException ();
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid characters");
if (path == "")
throw new ArgumentException ("The Path do not have a valid format");
return new DirectoryInfo (Path.GetDirectoryName (path));
}
public static void Move (string src, string dest)
{
if (src == null)
throw new ArgumentNullException ("src");
if (dest == null)
throw new ArgumentNullException ("dest");
if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Invalid source directory name: " + src, "src");
if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Invalid target directory name: " + dest, "dest");
if (!Exists (src))
throw new DirectoryNotFoundException (src + " does not exist");
MonoIOError error;
if (!MonoIO.MoveFile (src, dest, out error))
throw MonoIO.GetException (error);
}
public static void SetCreationTime (string path, DateTime creation_time)
{
File.SetCreationTime (path, creation_time);
}
public static void SetCurrentDirectory (string path)
{
/*
// Implementation complete 08/25/2001 14:24 except for
// LAMESPEC: documentation specifies invalid exceptions IOException (i think)
CheckArgument.Path (path, true);
CheckPermission.Demand (FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, path);
*/
if (!Exists (path))
{
throw new DirectoryNotFoundException ("Directory \"" + path + "\" not found.");
}
Environment.CurrentDirectory = path;
}
public static void SetLastAccessTime (string path, DateTime last_access_time)
{
File.SetLastAccessTime (path, last_access_time);
}
public static void SetLastWriteTime (string path, DateTime last_write_time)
{
File.SetLastWriteTime (path, last_write_time);
}
// private
private static string [] GetFileSystemEntries (string path, string pattern, FileAttributes mask, FileAttributes attrs)
{
SearchPattern search;
MonoIOStat stat;
IntPtr find;
if (path == null)
throw new ArgumentNullException ();
if (path == "")
throw new ArgumentException ("The Path do not have a valid format");
if (path.IndexOfAny (Path.InvalidPathChars) != -1)
throw new ArgumentException ("Path contains invalid characters");
if (!Directory.Exists (path)) {
throw new DirectoryNotFoundException ("Directory '" + path + "' not found.");
}
search = new SearchPattern (pattern);
MonoIOError error;
find = MonoIO.FindFirstFile (Path.Combine (path , "*"), out stat, out error);
if (find == MonoIO.InvalidHandle) {
switch (error) {
case MonoIOError.ERROR_FILE_NOT_FOUND:
case MonoIOError.ERROR_PATH_NOT_FOUND:
string message = String.Format ("Could not find a part of the path \"{0}\"", path);
throw new DirectoryNotFoundException (message);
case MonoIOError.ERROR_NO_MORE_FILES:
return new string [0];
default:
throw MonoIO.GetException (path,
error);
}
}
ArrayList entries = new ArrayList ();
while (true) {
// Ignore entries of "." and ".." -
// the documentation doesn't mention
// it (surprise!) but empirical
// testing indicates .net never
// returns "." or ".." in a
// GetDirectories() list.
if ((stat.Attributes & mask) == attrs &&
search.IsMatch (stat.Name) &&
stat.Name != "." &&
stat.Name != "..")
entries.Add (Path.Combine (path, stat.Name));
if (!MonoIO.FindNextFile (find, out stat,
out error))
break;
}
MonoIO.FindClose (find, out error);
return (string []) entries.ToArray (typeof (string));
}
}
}