2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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) {
103                         CheckPath (name);
104                         \r
105                         string path = Path.Combine (FullPath, name);\r
106                         Directory.CreateDirectory (path);\r
107 \r
108                         return new DirectoryInfo (path);\r
109                 }\r
110 \r
111                 // directory listing methods\r
112 \r
113                 public FileInfo [] GetFiles () {\r
114                         return GetFiles ("*");\r
115                 }\r
116 \r
117                 public FileInfo [] GetFiles (string pattern) {\r
118                         string [] names = Directory.GetFiles (FullPath, pattern);\r
119 \r
120                         ArrayList infos = new ArrayList ();\r
121                         foreach (string name in names)\r
122                                 infos.Add (new FileInfo (name));\r
123 \r
124                         return (FileInfo []) infos.ToArray (typeof (FileInfo));\r
125                 }\r
126 \r
127                 public DirectoryInfo [] GetDirectories () {\r
128                         return GetDirectories ("*");\r
129                 }\r
130 \r
131                 public DirectoryInfo [] GetDirectories (string pattern) {\r
132                         string [] names = Directory.GetDirectories (FullPath, pattern);\r
133 \r
134                         ArrayList infos = new ArrayList ();\r
135                         foreach (string name in names)\r
136                                 infos.Add (new DirectoryInfo (name));\r
137 \r
138                         return (DirectoryInfo []) infos.ToArray (typeof (DirectoryInfo));\r
139                 }\r
140 \r
141                 public FileSystemInfo [] GetFileSystemInfos () {\r
142                         return GetFileSystemInfos ("*");\r
143                 }\r
144 \r
145                 public FileSystemInfo [] GetFileSystemInfos (string pattern) {\r
146                         ArrayList infos = new ArrayList ();\r
147                         infos.AddRange (GetDirectories (pattern));\r
148                         infos.AddRange (GetFiles (pattern));\r
149 \r
150                         return (FileSystemInfo []) infos.ToArray (typeof (FileSystemInfo));\r
151                 }\r
152 \r
153                 // directory management methods\r
154 \r
155                 public override void Delete () {\r
156                         Delete (false);\r
157                 }\r
158 \r
159                 public void Delete (bool recurse) {\r
160                         Directory.Delete (FullPath, recurse);\r
161                 }\r
162 \r
163                 public void MoveTo (string dest) {\r
164                         Directory.Move (FullPath, Path.GetFullPath (dest));\r
165                 }\r
166 \r
167                 public override string ToString () {\r
168                         return OriginalPath;\r
169                 }\r
170         }\r
171 }\r