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;
38 using System.Security;
41 using System.Security.AccessControl;
48 public sealed class FileInfo : FileSystemInfo
57 public FileInfo (string fileName)
60 throw new ArgumentNullException ("fileName");
63 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
65 OriginalPath = fileName;
66 FullPath = Path.GetFullPath (fileName);
69 private FileInfo (SerializationInfo info, StreamingContext context)
70 : base (info, context)
74 internal override void InternalRefresh ()
76 exists = File.Exists (FullPath);
81 public override bool Exists {
85 if (stat.Attributes == MonoIO.InvalidFileAttributes)
88 if ((stat.Attributes & FileAttributes.Directory) != 0)
95 public override string Name {
97 return Path.GetFileName (FullPath);
102 public bool IsReadOnly {
105 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
107 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
112 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
114 FileAttributes attrs = File.GetAttributes(FullPath);
117 attrs |= FileAttributes.ReadOnly;
119 attrs &= ~FileAttributes.ReadOnly;
121 File.SetAttributes(FullPath, attrs);
125 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
127 public void Encrypt ()
129 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
130 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
131 // we throw the same (instead of a NotImplementedException) because most code should already be
132 // handling this exception to work properly.
133 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
136 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
138 public void Decrypt ()
140 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
141 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
142 // we throw the same (instead of a NotImplementedException) because most code should already be
143 // handling this exception to work properly.
144 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
151 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
157 public string DirectoryName {
159 return Path.GetDirectoryName (FullPath);
163 public DirectoryInfo Directory {
165 return new DirectoryInfo (DirectoryName);
169 // streamreader methods
171 public StreamReader OpenText ()
173 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
176 public StreamWriter CreateText ()
178 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
181 public StreamWriter AppendText ()
183 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
186 // filestream methods
188 public FileStream Create ()
190 return File.Create (FullPath);
193 public FileStream OpenRead ()
195 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
198 public FileStream OpenWrite ()
200 return Open (FileMode.OpenOrCreate, FileAccess.Write);
203 public FileStream Open (FileMode mode)
205 return Open (mode, FileAccess.ReadWrite);
208 public FileStream Open (FileMode mode, FileAccess access)
210 return Open (mode, access, FileShare.None);
213 public FileStream Open (FileMode mode, FileAccess access, FileShare share)
215 return new FileStream (FullPath, mode, access, share);
220 public override void Delete ()
224 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
226 if (!MonoIO.Exists (FullPath, out error))
227 // a weird MS.NET behaviour
230 if (MonoIO.ExistsDirectory (FullPath, out error))
231 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
232 if (!MonoIO.DeleteFile (FullPath, out error))
233 throw MonoIO.GetException (OriginalPath, error);
236 public void MoveTo (string destFileName)
238 if (destFileName == null)
239 throw new ArgumentNullException ("destFileName");
240 if (destFileName == Name || destFileName == FullName)
242 if (!File.Exists (FullPath))
243 throw new FileNotFoundException ();
245 File.Move (FullPath, destFileName);
246 this.FullPath = Path.GetFullPath (destFileName);
249 public FileInfo CopyTo (string destFileName)
251 return CopyTo (destFileName, false);
254 public FileInfo CopyTo (string destFileName, bool overwrite)
256 if (destFileName == null)
257 throw new ArgumentNullException ("destFileName");
258 if (destFileName.Length == 0)
259 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
261 string dest = Path.GetFullPath (destFileName);
263 if (overwrite && File.Exists (dest))
266 File.Copy (FullPath, dest);
268 return new FileInfo (dest);
271 public override string ToString ()
274 // for Moonlight we *never* return paths, since ToString is not [SecurityCritical] we simply return the Name
282 public FileSecurity GetAccessControl ()
284 return File.GetAccessControl (FullPath);
287 public FileSecurity GetAccessControl (AccessControlSections includeSections)
289 return File.GetAccessControl (FullPath, includeSections);
293 public FileInfo Replace (string destinationFileName,
294 string destinationBackupFileName)
296 string destinationFullPath = null;
298 throw new FileNotFoundException ();
299 if (destinationFileName == null)
300 throw new ArgumentNullException ("destinationFileName");
301 if (destinationFileName.Length == 0)
302 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
304 destinationFullPath = Path.GetFullPath (destinationFileName);
306 if (!File.Exists (destinationFullPath))
307 throw new FileNotFoundException ();
309 FileAttributes attrs = File.GetAttributes (destinationFullPath);
311 if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
312 throw new UnauthorizedAccessException ();
314 if (destinationBackupFileName != null) {
315 if (destinationBackupFileName.Length == 0)
316 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
317 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
319 File.Copy (FullPath, destinationFullPath,true);
320 File.Delete (FullPath);
321 return new FileInfo (destinationFullPath);
325 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
326 public FileInfo Replace (string destinationFileName,
327 string destinationBackupFileName,
328 bool ignoreMetadataErrors)
330 return Replace (destinationFileName, destinationBackupFileName);
333 public void SetAccessControl (FileSecurity fileSecurity)
335 File.SetAccessControl (FullPath, fileSecurity);