2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.IO / FileInfo.cs
blob5ddd27bef2de27158c5269f1d6c0a29134f0a053
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
11 //------------------------------------------------------------------------------
14 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
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.
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
39 #if !NET_2_1
40 using System.Security.AccessControl;
41 #endif
43 namespace System.IO {
45 [Serializable]
46 [ComVisible (true)]
47 public sealed class FileInfo : FileSystemInfo
49 private bool exists;
51 #if MOONLIGHT
52 internal FileInfo ()
55 #endif
56 public FileInfo (string fileName)
58 if (fileName == null)
59 throw new ArgumentNullException ("fileName");
61 CheckPath (fileName);
63 OriginalPath = fileName;
64 FullPath = Path.GetFullPath (fileName);
67 private FileInfo (SerializationInfo info, StreamingContext context)
68 : base (info, context)
72 internal override void InternalRefresh ()
74 exists = File.Exists (FullPath);
77 // public properties
79 public override bool Exists {
80 get {
81 Refresh (false);
83 if (stat.Attributes == MonoIO.InvalidFileAttributes)
84 return false;
86 if ((stat.Attributes & FileAttributes.Directory) != 0)
87 return false;
89 return exists;
93 public override string Name {
94 get {
95 return Path.GetFileName (FullPath);
99 #if !NET_2_1
100 public bool IsReadOnly {
101 get {
102 if (!Exists)
103 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
105 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
108 set {
109 if (!Exists)
110 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
112 FileAttributes attrs = File.GetAttributes(FullPath);
114 if (value)
115 attrs |= FileAttributes.ReadOnly;
116 else
117 attrs &= ~FileAttributes.ReadOnly;
119 File.SetAttributes(FullPath, attrs);
123 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
124 [ComVisible (false)]
125 public void Encrypt ()
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."));
134 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
135 [ComVisible (false)]
136 public void Decrypt ()
138 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
139 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
140 // we throw the same (instead of a NotImplementedException) because most code should already be
141 // handling this exception to work properly.
142 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
144 #endif
146 public long Length {
147 get {
148 if (!Exists)
149 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
151 return stat.Length;
155 public string DirectoryName {
156 get {
157 return Path.GetDirectoryName (FullPath);
161 public DirectoryInfo Directory {
162 get {
163 return new DirectoryInfo (DirectoryName);
167 // streamreader methods
169 public StreamReader OpenText ()
171 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
174 public StreamWriter CreateText ()
176 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
179 public StreamWriter AppendText ()
181 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
184 // filestream methods
186 public FileStream Create ()
188 return File.Create (FullPath);
191 public FileStream OpenRead ()
193 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
196 public FileStream OpenWrite ()
198 return Open (FileMode.OpenOrCreate, FileAccess.Write);
201 public FileStream Open (FileMode mode)
203 return Open (mode, FileAccess.ReadWrite);
206 public FileStream Open (FileMode mode, FileAccess access)
208 return Open (mode, access, FileShare.None);
211 public FileStream Open (FileMode mode, FileAccess access, FileShare share)
213 return new FileStream (FullPath, mode, access, share);
216 // file methods
218 public override void Delete ()
220 MonoIOError error;
222 if (!MonoIO.Exists (FullPath, out error))
223 // a weird MS.NET behaviour
224 return;
226 if (MonoIO.ExistsDirectory (FullPath, out error))
227 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
228 if (!MonoIO.DeleteFile (FullPath, out error))
229 throw MonoIO.GetException (OriginalPath, error);
232 public void MoveTo (string destFileName)
234 if (destFileName == null)
235 throw new ArgumentNullException ("destFileName");
236 if (destFileName == Name || destFileName == FullName)
237 return;
238 if (!File.Exists (FullPath))
239 throw new FileNotFoundException ();
241 File.Move (FullPath, destFileName);
242 this.FullPath = Path.GetFullPath (destFileName);
245 public FileInfo CopyTo (string destFileName)
247 return CopyTo (destFileName, false);
250 public FileInfo CopyTo (string destFileName, bool overwrite)
252 if (destFileName == null)
253 throw new ArgumentNullException ("destFileName");
254 if (destFileName.Length == 0)
255 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
257 string dest = Path.GetFullPath (destFileName);
259 if (overwrite && File.Exists (dest))
260 File.Delete (dest);
262 File.Copy (FullPath, dest);
264 return new FileInfo (dest);
267 public override string ToString ()
269 #if NET_2_1
270 // for Moonlight we *never* return paths, since ToString is not [SecurityCritical] we simply return the Name
271 return Name;
272 #else
273 return OriginalPath;
274 #endif
277 #if !NET_2_1
278 public FileSecurity GetAccessControl ()
280 throw new NotImplementedException ();
283 public FileSecurity GetAccessControl (AccessControlSections includeSections)
285 throw new NotImplementedException ();
288 [ComVisible (false)]
289 public FileInfo Replace (string destinationFileName,
290 string destinationBackupFileName)
292 string destinationFullPath = null;
293 if (!Exists)
294 throw new FileNotFoundException ();
295 if (destinationFileName == null)
296 throw new ArgumentNullException ("destinationFileName");
297 if (destinationFileName.Length == 0)
298 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
300 destinationFullPath = Path.GetFullPath (destinationFileName);
302 if (!File.Exists (destinationFullPath))
303 throw new FileNotFoundException ();
305 FileAttributes attrs = File.GetAttributes (destinationFullPath);
307 if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
308 throw new UnauthorizedAccessException ();
310 if (destinationBackupFileName != null) {
311 if (destinationBackupFileName.Length == 0)
312 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
313 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
315 File.Copy (FullPath, destinationFullPath,true);
316 File.Delete (FullPath);
317 return new FileInfo (destinationFullPath);
320 [ComVisible (false)]
321 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
322 public FileInfo Replace (string destinationFileName,
323 string destinationBackupFileName,
324 bool ignoreMetadataErrors)
326 return Replace (destinationFileName, destinationBackupFileName);
329 public void SetAccessControl (FileSecurity fileSecurity)
331 throw new NotImplementedException ();
333 #endif