2004-12-03 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / corlib / System.IO / DirectoryInfo.cs
1 // \r
2 // System.IO.DirectoryInfo.cs \r
3 //\r
4 // Author:\r
5 //   Miguel de Icaza, miguel@ximian.com\r
6 //   Jim Richardson, develop@wtfo-guru.com\r
7 //   Dan Lewis, dihlewis@yahoo.co.uk\r
8 //\r
9 // Copyright (C) 2002 Ximian, Inc.\r
10 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
11 // \r
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35 \r
36 using System;\r
37 using System.Collections;\r
38 \r
39 namespace System.IO {\r
40         \r
41         [Serializable]\r
42         public sealed class DirectoryInfo : FileSystemInfo {\r
43         \r
44                 public DirectoryInfo (string path) {\r
45                         CheckPath (path);\r
46                 \r
47                         OriginalPath = path;\r
48                         FullPath = Path.GetFullPath (path);\r
49                 }\r
50 \r
51                 // properties\r
52 \r
53                 public override bool Exists {\r
54                         get {\r
55                                 Refresh (false);\r
56 \r
57                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)\r
58                                         return false;\r
59 \r
60                                 if ((stat.Attributes & FileAttributes.Directory) == 0)\r
61                                         return false;\r
62 \r
63                                 return true;\r
64                         }\r
65                 }\r
66 \r
67                 public override string Name {\r
68                         get {\r
69                                 string result = Path.GetFileName (FullPath);\r
70                                 if (result == null || result == "")\r
71                                         return FullPath;\r
72                                 return result;\r
73                         }\r
74                 }\r
75 \r
76                 public DirectoryInfo Parent {\r
77                         get {\r
78                                 string dirname = Path.GetDirectoryName (FullPath);\r
79                                 if (dirname == null)\r
80                                         return null;\r
81 \r
82                                 return new DirectoryInfo (dirname);\r
83                         }\r
84                 }\r
85 \r
86                 public DirectoryInfo Root {\r
87                         get {\r
88                                 string root = Path.GetPathRoot (FullPath);\r
89                                 if (root == null)\r
90                                         return null;\r
91 \r
92                                 return new DirectoryInfo (root);\r
93                         }\r
94                 }\r
95 \r
96                 // creational methods\r
97 \r
98                 public void Create () {\r
99                         Directory.CreateDirectory (FullPath);\r
100                 }\r
101 \r
102                 public DirectoryInfo CreateSubdirectory (string name) {\r
103                         string path = Path.Combine (FullPath, name);\r
104                         Directory.CreateDirectory (path);\r
105 \r
106                         return new DirectoryInfo (path);\r
107                 }\r
108 \r
109                 // directory listing methods\r
110 \r
111                 public FileInfo [] GetFiles () {\r
112                         return GetFiles ("*");\r
113                 }\r
114 \r
115                 public FileInfo [] GetFiles (string pattern) {\r
116                         string [] names = Directory.GetFiles (FullPath, pattern);\r
117 \r
118                         ArrayList infos = new ArrayList ();\r
119                         foreach (string name in names)\r
120                                 infos.Add (new FileInfo (name));\r
121 \r
122                         return (FileInfo []) infos.ToArray (typeof (FileInfo));\r
123                 }\r
124 \r
125                 public DirectoryInfo [] GetDirectories () {\r
126                         return GetDirectories ("*");\r
127                 }\r
128 \r
129                 public DirectoryInfo [] GetDirectories (string pattern) {\r
130                         string [] names = Directory.GetDirectories (FullPath, pattern);\r
131 \r
132                         ArrayList infos = new ArrayList ();\r
133                         foreach (string name in names)\r
134                                 infos.Add (new DirectoryInfo (name));\r
135 \r
136                         return (DirectoryInfo []) infos.ToArray (typeof (DirectoryInfo));\r
137                 }\r
138 \r
139                 public FileSystemInfo [] GetFileSystemInfos () {\r
140                         return GetFileSystemInfos ("*");\r
141                 }\r
142 \r
143                 public FileSystemInfo [] GetFileSystemInfos (string pattern) {\r
144                         ArrayList infos = new ArrayList ();\r
145                         infos.AddRange (GetDirectories (pattern));\r
146                         infos.AddRange (GetFiles (pattern));\r
147 \r
148                         return (FileSystemInfo []) infos.ToArray (typeof (FileSystemInfo));\r
149                 }\r
150 \r
151                 // directory management methods\r
152 \r
153                 public override void Delete () {\r
154                         Delete (false);\r
155                 }\r
156 \r
157                 public void Delete (bool recurse) {\r
158                         Directory.Delete (FullPath, recurse);\r
159                 }\r
160 \r
161                 public void MoveTo (string dest) {\r
162                         Directory.Move (FullPath, Path.GetFullPath (dest));\r
163                 }\r
164 \r
165                 public override string ToString () {\r
166                         return OriginalPath;\r
167                 }\r
168         }\r
169 }\r