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