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
51 private string displayPath;
53 public FileInfo (string fileName)
56 throw new ArgumentNullException ("fileName");
59 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
61 OriginalPath = fileName;
62 FullPath = Path.GetFullPath (fileName);
64 displayPath = OriginalPath;
67 private FileInfo (SerializationInfo info, StreamingContext context)
68 : base (info, context)
70 displayPath = OriginalPath;
73 internal override void InternalRefresh ()
75 exists = File.Exists (FullPath);
80 public override bool Exists {
84 if (stat.Attributes == MonoIO.InvalidFileAttributes)
87 if ((stat.Attributes & FileAttributes.Directory) != 0)
94 public override string Name {
96 return Path.GetFileName (FullPath);
100 public bool IsReadOnly {
103 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
105 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
110 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
112 FileAttributes attrs = File.GetAttributes(FullPath);
115 attrs |= FileAttributes.ReadOnly;
117 attrs &= ~FileAttributes.ReadOnly;
119 File.SetAttributes(FullPath, attrs);
123 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
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).")]
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."));
148 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
154 public string DirectoryName {
156 return Path.GetDirectoryName (FullPath);
160 public DirectoryInfo Directory {
162 return new DirectoryInfo (DirectoryName);
166 // streamreader methods
168 public StreamReader OpenText ()
170 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
173 public StreamWriter CreateText ()
175 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
178 public StreamWriter AppendText ()
180 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
183 // filestream methods
185 public FileStream Create ()
187 return File.Create (FullPath);
190 public FileStream OpenRead ()
192 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
195 public FileStream OpenWrite ()
197 return Open (FileMode.OpenOrCreate, FileAccess.Write);
200 public FileStream Open (FileMode mode)
202 return Open (mode, FileAccess.ReadWrite);
205 public FileStream Open (FileMode mode, FileAccess access)
207 return Open (mode, access, FileShare.None);
210 public FileStream Open (FileMode mode, FileAccess access, FileShare share)
212 return new FileStream (FullPath, mode, access, share);
217 public override void Delete ()
221 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
223 if (!MonoIO.Exists (FullPath, out error))
224 // a weird MS.NET behaviour
227 if (MonoIO.ExistsDirectory (FullPath, out error))
228 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
229 if (!MonoIO.DeleteFile (FullPath, out error))
230 throw MonoIO.GetException (OriginalPath, error);
233 public void MoveTo (string destFileName)
235 if (destFileName == null)
236 throw new ArgumentNullException ("destFileName");
237 if (destFileName.Length == 0)
238 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
240 var destFullPath = Path.GetFullPath (destFileName);
241 if (destFullPath == FullPath)
243 if (!File.Exists (FullPath))
244 throw new FileNotFoundException ();
246 File.Move (FullPath, destFullPath);
247 this.FullPath = destFullPath;
249 displayPath = destFileName;
252 public FileInfo CopyTo (string destFileName)
254 return CopyTo (destFileName, false);
257 public FileInfo CopyTo (string destFileName, bool overwrite)
259 if (destFileName == null)
260 throw new ArgumentNullException ("destFileName");
261 if (destFileName.Length == 0)
262 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
264 string dest = Path.GetFullPath (destFileName);
266 if (overwrite && File.Exists (dest))
269 File.Copy (FullPath, dest);
271 return new FileInfo (dest);
274 public override string ToString ()
280 public FileSecurity GetAccessControl ()
282 return File.GetAccessControl (FullPath);
285 public FileSecurity GetAccessControl (AccessControlSections includeSections)
287 return File.GetAccessControl (FullPath, includeSections);
291 public FileInfo Replace (string destinationFileName,
292 string destinationBackupFileName)
294 string destinationFullPath = null;
296 throw new FileNotFoundException ();
297 if (destinationFileName == null)
298 throw new ArgumentNullException ("destinationFileName");
299 if (destinationFileName.Length == 0)
300 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
302 destinationFullPath = Path.GetFullPath (destinationFileName);
304 if (!File.Exists (destinationFullPath))
305 throw new FileNotFoundException ();
307 FileAttributes attrs = File.GetAttributes (destinationFullPath);
309 if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
310 throw new UnauthorizedAccessException ();
312 if (destinationBackupFileName != null) {
313 if (destinationBackupFileName.Length == 0)
314 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
315 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
317 File.Copy (FullPath, destinationFullPath,true);
318 File.Delete (FullPath);
319 return new FileInfo (destinationFullPath);
323 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
324 public FileInfo Replace (string destinationFileName,
325 string destinationBackupFileName,
326 bool ignoreMetadataErrors)
328 return Replace (destinationFileName, destinationBackupFileName);
331 public void SetAccessControl (FileSecurity fileSecurity)
333 File.SetAccessControl (FullPath, fileSecurity);