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;
39 using System.Security.AccessControl;
45 public sealed class FileInfo : FileSystemInfo
49 public FileInfo (string fileName)
52 throw new ArgumentNullException ("fileName");
55 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
57 OriginalPath = fileName;
58 FullPath = Path.GetFullPath (fileName);
61 private FileInfo (SerializationInfo info, StreamingContext context)
62 : base (info, context)
66 internal override void InternalRefresh ()
68 exists = File.Exists (FullPath);
73 public override bool Exists {
77 if (stat.Attributes == MonoIO.InvalidFileAttributes)
80 if ((stat.Attributes & FileAttributes.Directory) != 0)
87 public override string Name {
89 return Path.GetFileName (FullPath);
93 public bool IsReadOnly {
96 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
98 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
103 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
105 FileAttributes attrs = File.GetAttributes(FullPath);
108 attrs |= FileAttributes.ReadOnly;
110 attrs &= ~FileAttributes.ReadOnly;
112 File.SetAttributes(FullPath, attrs);
116 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
118 public void Encrypt ()
120 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
121 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
122 // we throw the same (instead of a NotImplementedException) because most code should already be
123 // handling this exception to work properly.
124 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
127 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
129 public void Decrypt ()
131 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
132 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
133 // we throw the same (instead of a NotImplementedException) because most code should already be
134 // handling this exception to work properly.
135 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
141 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
147 public string DirectoryName {
149 return Path.GetDirectoryName (FullPath);
153 public DirectoryInfo Directory {
155 return new DirectoryInfo (DirectoryName);
159 // streamreader methods
161 public StreamReader OpenText ()
163 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
166 public StreamWriter CreateText ()
168 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
171 public StreamWriter AppendText ()
173 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
176 // filestream methods
178 public FileStream Create ()
180 return File.Create (FullPath);
183 public FileStream OpenRead ()
185 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
188 public FileStream OpenWrite ()
190 return Open (FileMode.OpenOrCreate, FileAccess.Write);
193 public FileStream Open (FileMode mode)
195 return Open (mode, FileAccess.ReadWrite);
198 public FileStream Open (FileMode mode, FileAccess access)
200 return Open (mode, access, FileShare.None);
203 public FileStream Open (FileMode mode, FileAccess access, FileShare share)
205 return new FileStream (FullPath, mode, access, share);
210 public override void Delete ()
214 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
216 if (!MonoIO.Exists (FullPath, out error))
217 // a weird MS.NET behaviour
220 if (MonoIO.ExistsDirectory (FullPath, out error))
221 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
222 if (!MonoIO.DeleteFile (FullPath, out error))
223 throw MonoIO.GetException (OriginalPath, error);
226 public void MoveTo (string destFileName)
228 if (destFileName == null)
229 throw new ArgumentNullException ("destFileName");
230 if (destFileName == Name || destFileName == FullName)
232 if (!File.Exists (FullPath))
233 throw new FileNotFoundException ();
235 File.Move (FullPath, destFileName);
236 this.FullPath = Path.GetFullPath (destFileName);
239 public FileInfo CopyTo (string destFileName)
241 return CopyTo (destFileName, false);
244 public FileInfo CopyTo (string destFileName, bool overwrite)
246 if (destFileName == null)
247 throw new ArgumentNullException ("destFileName");
248 if (destFileName.Length == 0)
249 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
251 string dest = Path.GetFullPath (destFileName);
253 if (overwrite && File.Exists (dest))
256 File.Copy (FullPath, dest);
258 return new FileInfo (dest);
261 public override string ToString ()
266 public FileSecurity GetAccessControl ()
268 return File.GetAccessControl (FullPath);
271 public FileSecurity GetAccessControl (AccessControlSections includeSections)
273 return File.GetAccessControl (FullPath, includeSections);
277 public FileInfo Replace (string destinationFileName,
278 string destinationBackupFileName)
280 string destinationFullPath = null;
282 throw new FileNotFoundException ();
283 if (destinationFileName == null)
284 throw new ArgumentNullException ("destinationFileName");
285 if (destinationFileName.Length == 0)
286 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
288 destinationFullPath = Path.GetFullPath (destinationFileName);
290 if (!File.Exists (destinationFullPath))
291 throw new FileNotFoundException ();
293 FileAttributes attrs = File.GetAttributes (destinationFullPath);
295 if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
296 throw new UnauthorizedAccessException ();
298 if (destinationBackupFileName != null) {
299 if (destinationBackupFileName.Length == 0)
300 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
301 File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
303 File.Copy (FullPath, destinationFullPath,true);
304 File.Delete (FullPath);
305 return new FileInfo (destinationFullPath);
309 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
310 public FileInfo Replace (string destinationFileName,
311 string destinationBackupFileName,
312 bool ignoreMetadataErrors)
314 return Replace (destinationFileName, destinationBackupFileName);
317 public void SetAccessControl (FileSecurity fileSecurity)
319 File.SetAccessControl (FullPath, fileSecurity);