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;
40 using System.Security.AccessControl;
49 public sealed class FileInfo : FileSystemInfo
53 public FileInfo (string fileName)
56 throw new ArgumentNullException ("fileName");
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 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 == Name || destFileName == FullName)
235 if (!File.Exists (FullPath))
236 throw new FileNotFoundException ();
238 File.Move (FullPath, destFileName);
239 this.FullPath = Path.GetFullPath (destFileName);
242 public FileInfo CopyTo (string destFileName)
244 return CopyTo (destFileName, false);
247 public FileInfo CopyTo (string destFileName, bool overwrite)
249 if (destFileName == null)
250 throw new ArgumentNullException ("destFileName");
251 if (destFileName.Length == 0)
252 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
254 string dest = Path.GetFullPath (destFileName);
256 if (overwrite && File.Exists (dest))
259 File.Copy (FullPath, dest);
261 return new FileInfo (dest);
264 public override string ToString ()
270 public FileSecurity GetAccessControl ()
272 throw new NotImplementedException ();
275 public FileSecurity GetAccessControl (AccessControlSections includeSections)
277 throw new NotImplementedException ();
281 public FileInfo Replace (string destinationFileName,
282 string destinationBackupFileName)
284 throw new NotImplementedException ();
288 public FileInfo Replace (string destinationFileName,
289 string destinationBackupFileName,
290 bool ignoreMetadataErrors)
292 throw new NotImplementedException ();
295 public void SetAccessControl (FileSecurity fileSecurity)
297 throw new NotImplementedException ();