2007-04-30 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / FileInfo.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.IO.FileInfo.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 //                 Dan Lewis (dihlewis@yahoo.co.uk)
9 // Created:        Monday, August 13, 2001 
10 //
11 //------------------------------------------------------------------------------
12
13 //
14 // Copyright (C) 2004, 2006 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
36 using System.Runtime.InteropServices;
37
38 #if NET_2_0
39 using System.Security.AccessControl;
40 #endif
41
42 namespace System.IO {
43
44         [Serializable]
45 #if NET_2_0
46         [ComVisible (true)]
47 #endif
48         public sealed class FileInfo : FileSystemInfo {
49         
50
51                 private bool exists;
52
53                 public FileInfo (string path) {
54                         CheckPath (path);
55                 
56                         OriginalPath = path;
57                         FullPath = Path.GetFullPath (path);
58                 }
59                 
60                 internal override void InternalRefresh ()
61                 {
62                         exists = File.Exists (FullPath);
63                 }
64
65
66                 // public properties
67
68                 public override bool Exists {
69                         get {
70                                 Refresh (false);
71
72                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
73                                         return false;
74
75                                 if ((stat.Attributes & FileAttributes.Directory) != 0)
76                                         return false;
77
78                                 return exists;
79                         }
80                 }
81
82                 public override string Name {
83                         get {
84                                 return Path.GetFileName (FullPath);
85                         }
86                 }
87
88 #if NET_2_0
89                 public bool IsReadOnly {
90                         get {
91                                 if (!Exists)
92                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
93                                         
94                                 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
95                         }
96                                 
97                         set {
98                                 if (!Exists)
99                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
100                                         
101                                 FileAttributes attrs = File.GetAttributes(FullPath);
102
103                                 if (value) 
104                                         attrs |= FileAttributes.ReadOnly;
105                                 else
106                                         attrs &= ~FileAttributes.ReadOnly;                                      
107
108                                 File.SetAttributes(FullPath, attrs);
109                         }
110                 }
111
112                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
113                 [ComVisible (false)]
114                 public void Encrypt ()
115                 {
116                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
117                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
118                         // we throw the same (instead of a NotImplementedException) because most code should already be
119                         // handling this exception to work properly.
120                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
121                 }
122
123                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
124                 [ComVisible (false)]
125                 public void Decrypt ()
126                 {
127                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
128                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
129                         // we throw the same (instead of a NotImplementedException) because most code should already be
130                         // handling this exception to work properly.
131                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
132                 }
133 #endif
134
135                 public long Length {
136                         get {
137                                 if (!Exists)
138                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
139
140                                 return stat.Length;
141                         }
142                 }
143
144                 public string DirectoryName {
145                         get {
146                                 return Path.GetDirectoryName (FullPath);
147                         }
148                 }
149
150                 public DirectoryInfo Directory {
151                         get {
152                                 return new DirectoryInfo (DirectoryName);
153                         }
154                 }
155
156                 // streamreader methods
157
158                 public StreamReader OpenText () {
159                         return new StreamReader (Open (FileMode.Open, FileAccess.Read));
160                 }
161
162                 public StreamWriter CreateText () {
163                         return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
164                 }
165                 
166                 public StreamWriter AppendText () {
167                         return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
168                 }
169
170                 // filestream methods
171
172                 public FileStream Create ()
173                 {
174                         return File.Create (FullPath);
175                 }
176                 
177                 
178                 public FileStream OpenRead () {
179                         return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
180                 }
181
182                 public FileStream OpenWrite () {
183                         return Open (FileMode.OpenOrCreate, FileAccess.Write);
184                 }
185
186                 public FileStream Open (FileMode mode) {
187                         return Open (mode, FileAccess.ReadWrite);
188                 }
189
190                 public FileStream Open (FileMode mode, FileAccess access) {
191                         return Open (mode, access, FileShare.None);
192                 }
193
194                 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
195                         return new FileStream (FullPath, mode, access, share);
196                 }
197
198                 // file methods
199
200                 public override void Delete () {
201                         MonoIOError error;
202                                                 
203                         if (!MonoIO.Exists (FullPath, out error)) {
204                                 // a weird MS.NET behaviour
205                                 return;
206                         }
207
208                         if (MonoIO.ExistsDirectory (FullPath, out error)) {
209                                 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
210                         }
211                         
212                         if (!MonoIO.DeleteFile (FullPath, out error)) {
213                                 throw MonoIO.GetException (OriginalPath,
214                                                            error);
215                         }
216                 }
217                 
218                 public void MoveTo (string dest) {
219
220                         if (dest == null)
221                                 throw new ArgumentNullException ();
222
223                         if (dest == Name || dest == FullName)
224                                 return;
225
226                         MonoIOError error;
227                         if (MonoIO.Exists (dest, out error) ||
228                                 MonoIO.ExistsDirectory (dest, out error))
229                                 throw new IOException ();
230                         File.Move (FullPath, dest);
231                         this.FullPath = Path.GetFullPath (dest);
232                 }
233
234                 public FileInfo CopyTo (string path) {
235                         return CopyTo (path, false);
236                 }
237
238                 public FileInfo CopyTo (string path, bool overwrite) {
239                         string dest = Path.GetFullPath (path);
240
241                         if (overwrite && File.Exists (path))
242                                 File.Delete (path);
243
244                         File.Copy (FullPath, dest);
245                 
246                         return new FileInfo (dest);
247                 }
248
249                 public override string ToString () {
250                         return OriginalPath;
251                 }
252
253 #if NET_2_0
254                 public FileSecurity GetAccessControl ()
255                 {
256                         throw new NotImplementedException ();
257                 }
258                 
259                 public FileSecurity GetAccessControl (AccessControlSections includeSections)
260                 {
261                         throw new NotImplementedException ();
262                 }
263
264                 [ComVisible (false)]
265                 public FileInfo Replace (string destinationFileName,
266                                          string destinationBackupFileName)
267                 {
268                         throw new NotImplementedException ();
269                 }
270                 
271                 [ComVisible (false)]
272                 public FileInfo Replace (string destinationFileName,
273                                          string destinationBackupFileName,
274                                          bool ignoreMetadataErrors)
275                 {
276                         throw new NotImplementedException ();
277                 }
278
279                 public void SetAccessControl (FileSecurity fileSecurity)
280                 {
281                         throw new NotImplementedException ();
282                 }
283 #endif
284         }
285 }