1 //------------------------------------------------------------------------------
3 // System.IO.FileInfo.cs
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
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:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
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;
40 using System.Security.AccessControl;
47 public sealed class FileInfo : FileSystemInfo
51 public FileInfo (string fileName)
54 throw new ArgumentNullException ("fileName");
58 OriginalPath = fileName;
59 FullPath = Path.GetFullPath (fileName);
62 private FileInfo (SerializationInfo info, StreamingContext context)
63 : base (info, context)
67 internal override void InternalRefresh ()
69 exists = File.Exists (FullPath);
74 public override bool Exists {
78 if (stat.Attributes == MonoIO.InvalidFileAttributes)
81 if ((stat.Attributes & FileAttributes.Directory) != 0)
88 public override string Name {
90 return Path.GetFileName (FullPath);
95 public bool IsReadOnly {
98 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
100 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
105 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
107 FileAttributes attrs = File.GetAttributes(FullPath);
110 attrs |= FileAttributes.ReadOnly;
112 attrs &= ~FileAttributes.ReadOnly;
114 File.SetAttributes(FullPath, attrs);
118 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
120 public void Encrypt ()
122 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
123 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
124 // we throw the same (instead of a NotImplementedException) because most code should already be
125 // handling this exception to work properly.
126 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
129 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
131 public void Decrypt ()
133 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
134 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
135 // we throw the same (instead of a NotImplementedException) because most code should already be
136 // handling this exception to work properly.
137 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
144 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
150 public string DirectoryName {
152 return Path.GetDirectoryName (FullPath);
156 public DirectoryInfo Directory {
158 return new DirectoryInfo (DirectoryName);
162 // streamreader methods
164 public StreamReader OpenText ()
166 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
169 public StreamWriter CreateText ()
171 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
174 public StreamWriter AppendText ()
176 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
179 // filestream methods
181 public FileStream Create ()
183 return File.Create (FullPath);
186 public FileStream OpenRead ()
188 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
191 public FileStream OpenWrite ()
193 return Open (FileMode.OpenOrCreate, FileAccess.Write);
196 public FileStream Open (FileMode mode)
198 return Open (mode, FileAccess.ReadWrite);
201 public FileStream Open (FileMode mode, FileAccess access)
203 return Open (mode, access, FileShare.None);
206 public FileStream Open (FileMode mode, FileAccess access, FileShare share)
208 return new FileStream (FullPath, mode, access, share);
213 public override void Delete ()
217 if (!MonoIO.Exists (FullPath, out error))
218 // a weird MS.NET behaviour
221 if (MonoIO.ExistsDirectory (FullPath, out error))
222 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
223 if (!MonoIO.DeleteFile (FullPath, out error))
224 throw MonoIO.GetException (OriginalPath, error);
227 public void MoveTo (string destFileName)
229 if (destFileName == null)
230 throw new ArgumentNullException ("destFileName");
231 if (destFileName == Name || destFileName == FullName)
233 if (!File.Exists (FullPath))
234 throw new FileNotFoundException ();
236 File.Move (FullPath, destFileName);
237 this.FullPath = Path.GetFullPath (destFileName);
240 public FileInfo CopyTo (string destFileName)
242 return CopyTo (destFileName, false);
245 public FileInfo CopyTo (string destFileName, bool overwrite)
247 if (destFileName == null)
248 throw new ArgumentNullException ("destFileName");
249 if (destFileName.Length == 0)
250 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
252 string dest = Path.GetFullPath (destFileName);
254 if (overwrite && File.Exists (dest))
257 File.Copy (FullPath, dest);
259 return new FileInfo (dest);
262 public override string ToString ()
265 // for Moonlight we *never* return paths, since ToString is not [SecurityCritical] we simply return the Name
273 public FileSecurity GetAccessControl ()
275 throw new NotImplementedException ();
278 public FileSecurity GetAccessControl (AccessControlSections includeSections)
280 throw new NotImplementedException ();
284 public FileInfo Replace (string destinationFileName,
285 string destinationBackupFileName)
287 string destinationFullPath = null;
289 throw new FileNotFoundException ();
290 if (destinationFileName == null)
291 throw new ArgumentNullException ("destinationFileName");
292 if (destinationFileName.Length == 0)
293 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
295 destinationFullPath = Path.GetFullPath (destinationFileName);
297 if (!File.Exists (destinationFullPath))
298 throw new FileNotFoundException ();
300 FileAttributes attrs = File.GetAttributes (destinationFullPath);
302 if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
303 throw new UnauthorizedAccessException ();
305 if (destinationBackupFileName != null) {
306 if (destinationBackupFileName.Length == 0)
307 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
308 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
310 File.Copy (FullPath, destinationFullPath,true);
311 File.Delete (FullPath);
312 return new FileInfo (destinationFullPath);
316 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
317 public FileInfo Replace (string destinationFileName,
318 string destinationBackupFileName,
319 bool ignoreMetadataErrors)
321 return Replace (destinationFileName, destinationBackupFileName);
324 public void SetAccessControl (FileSecurity fileSecurity)
326 throw new NotImplementedException ();