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
52 public FileInfo (string fileName)
55 throw new ArgumentNullException ("fileName");
58 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
60 OriginalPath = fileName;
61 FullPath = Path.GetFullPath (fileName);
64 private FileInfo (SerializationInfo info, StreamingContext context)
65 : base (info, context)
69 internal override void InternalRefresh ()
71 exists = File.Exists (FullPath);
76 public override bool Exists {
80 if (stat.Attributes == MonoIO.InvalidFileAttributes)
83 if ((stat.Attributes & FileAttributes.Directory) != 0)
90 public override string Name {
92 return Path.GetFileName (FullPath);
96 public bool IsReadOnly {
99 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
101 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
106 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
108 FileAttributes attrs = File.GetAttributes(FullPath);
111 attrs |= FileAttributes.ReadOnly;
113 attrs &= ~FileAttributes.ReadOnly;
115 File.SetAttributes(FullPath, attrs);
119 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
121 public void Encrypt ()
123 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
124 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
125 // we throw the same (instead of a NotImplementedException) because most code should already be
126 // handling this exception to work properly.
127 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
130 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
132 public void Decrypt ()
134 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
135 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
136 // we throw the same (instead of a NotImplementedException) because most code should already be
137 // handling this exception to work properly.
138 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 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
219 if (!MonoIO.Exists (FullPath, out error))
220 // a weird MS.NET behaviour
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);
229 public void MoveTo (string destFileName)
231 if (destFileName == null)
232 throw new ArgumentNullException ("destFileName");
233 if (destFileName.Length == 0)
234 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
236 var destFullPath = Path.GetFullPath (destFileName);
237 if (destFullPath == FullPath)
239 if (!File.Exists (FullPath))
240 throw new FileNotFoundException ();
242 File.Move (FullPath, destFullPath);
243 this.FullPath = destFullPath;
246 public FileInfo CopyTo (string destFileName)
248 return CopyTo (destFileName, false);
251 public FileInfo CopyTo (string destFileName, bool overwrite)
253 if (destFileName == null)
254 throw new ArgumentNullException ("destFileName");
255 if (destFileName.Length == 0)
256 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
258 string dest = Path.GetFullPath (destFileName);
260 if (overwrite && File.Exists (dest))
263 File.Copy (FullPath, dest);
265 return new FileInfo (dest);
268 public override string ToString ()
274 public FileSecurity GetAccessControl ()
276 return File.GetAccessControl (FullPath);
279 public FileSecurity GetAccessControl (AccessControlSections includeSections)
281 return File.GetAccessControl (FullPath, includeSections);
285 public FileInfo Replace (string destinationFileName,
286 string destinationBackupFileName)
288 string destinationFullPath = null;
290 throw new FileNotFoundException ();
291 if (destinationFileName == null)
292 throw new ArgumentNullException ("destinationFileName");
293 if (destinationFileName.Length == 0)
294 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
296 destinationFullPath = Path.GetFullPath (destinationFileName);
298 if (!File.Exists (destinationFullPath))
299 throw new FileNotFoundException ();
301 FileAttributes attrs = File.GetAttributes (destinationFullPath);
303 if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
304 throw new UnauthorizedAccessException ();
306 if (destinationBackupFileName != null) {
307 if (destinationBackupFileName.Length == 0)
308 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
309 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
311 File.Copy (FullPath, destinationFullPath,true);
312 File.Delete (FullPath);
313 return new FileInfo (destinationFullPath);
317 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
318 public FileInfo Replace (string destinationFileName,
319 string destinationBackupFileName,
320 bool ignoreMetadataErrors)
322 return Replace (destinationFileName, destinationBackupFileName);
325 public void SetAccessControl (FileSecurity fileSecurity)
327 File.SetAccessControl (FullPath, fileSecurity);