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);
97 public bool IsReadOnly {
100 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
102 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
107 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
109 FileAttributes attrs = File.GetAttributes(FullPath);
112 attrs |= FileAttributes.ReadOnly;
114 attrs &= ~FileAttributes.ReadOnly;
116 File.SetAttributes(FullPath, attrs);
120 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
122 public void Encrypt ()
124 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
125 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
126 // we throw the same (instead of a NotImplementedException) because most code should already be
127 // handling this exception to work properly.
128 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
131 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
133 public void Decrypt ()
135 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
136 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
137 // we throw the same (instead of a NotImplementedException) because most code should already be
138 // handling this exception to work properly.
139 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
146 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
152 public string DirectoryName {
154 return Path.GetDirectoryName (FullPath);
158 public DirectoryInfo Directory {
160 return new DirectoryInfo (DirectoryName);
164 // streamreader methods
166 public StreamReader OpenText ()
168 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
171 public StreamWriter CreateText ()
173 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
176 public StreamWriter AppendText ()
178 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
181 // filestream methods
183 public FileStream Create ()
185 return File.Create (FullPath);
188 public FileStream OpenRead ()
190 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
193 public FileStream OpenWrite ()
195 return Open (FileMode.OpenOrCreate, FileAccess.Write);
198 public FileStream Open (FileMode mode)
200 return Open (mode, FileAccess.ReadWrite);
203 public FileStream Open (FileMode mode, FileAccess access)
205 return Open (mode, access, FileShare.None);
208 public FileStream Open (FileMode mode, FileAccess access, FileShare share)
210 return new FileStream (FullPath, mode, access, share);
215 public override void Delete ()
219 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
221 if (!MonoIO.Exists (FullPath, out error))
222 // a weird MS.NET behaviour
225 if (MonoIO.ExistsDirectory (FullPath, out error))
226 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
227 if (!MonoIO.DeleteFile (FullPath, out error))
228 throw MonoIO.GetException (OriginalPath, error);
231 public void MoveTo (string destFileName)
233 if (destFileName == null)
234 throw new ArgumentNullException ("destFileName");
235 if (destFileName == Name || destFileName == FullName)
237 if (!File.Exists (FullPath))
238 throw new FileNotFoundException ();
240 File.Move (FullPath, destFileName);
241 this.FullPath = Path.GetFullPath (destFileName);
244 public FileInfo CopyTo (string destFileName)
246 return CopyTo (destFileName, false);
249 public FileInfo CopyTo (string destFileName, bool overwrite)
251 if (destFileName == null)
252 throw new ArgumentNullException ("destFileName");
253 if (destFileName.Length == 0)
254 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
256 string dest = Path.GetFullPath (destFileName);
258 if (overwrite && File.Exists (dest))
261 File.Copy (FullPath, dest);
263 return new FileInfo (dest);
266 public override string ToString ()
269 // for Moonlight we *never* return paths, since ToString is not [SecurityCritical] we simply return the Name
277 public FileSecurity GetAccessControl ()
279 return File.GetAccessControl (FullPath);
282 public FileSecurity GetAccessControl (AccessControlSections includeSections)
284 return File.GetAccessControl (FullPath, includeSections);
288 public FileInfo Replace (string destinationFileName,
289 string destinationBackupFileName)
291 string destinationFullPath = null;
293 throw new FileNotFoundException ();
294 if (destinationFileName == null)
295 throw new ArgumentNullException ("destinationFileName");
296 if (destinationFileName.Length == 0)
297 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
299 destinationFullPath = Path.GetFullPath (destinationFileName);
301 if (!File.Exists (destinationFullPath))
302 throw new FileNotFoundException ();
304 FileAttributes attrs = File.GetAttributes (destinationFullPath);
306 if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
307 throw new UnauthorizedAccessException ();
309 if (destinationBackupFileName != null) {
310 if (destinationBackupFileName.Length == 0)
311 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
312 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
314 File.Copy (FullPath, destinationFullPath,true);
315 File.Delete (FullPath);
316 return new FileInfo (destinationFullPath);
320 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
321 public FileInfo Replace (string destinationFileName,
322 string destinationBackupFileName,
323 bool ignoreMetadataErrors)
325 return Replace (destinationFileName, destinationBackupFileName);
328 public void SetAccessControl (FileSecurity fileSecurity)
330 File.SetAccessControl (FullPath, fileSecurity);