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;
39 using System.Security.AccessControl;
48 public sealed class FileInfo : FileSystemInfo {
53 public FileInfo (string path) {
57 FullPath = Path.GetFullPath (path);
60 internal override void InternalRefresh ()
62 exists = File.Exists (FullPath);
68 public override bool Exists {
72 if (stat.Attributes == MonoIO.InvalidFileAttributes)
75 if ((stat.Attributes & FileAttributes.Directory) != 0)
82 public override string Name {
84 return Path.GetFileName (FullPath);
89 public bool IsReadOnly {
92 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
94 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
99 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
101 FileAttributes attrs = File.GetAttributes(FullPath);
104 attrs |= FileAttributes.ReadOnly;
106 attrs &= ~FileAttributes.ReadOnly;
108 File.SetAttributes(FullPath, attrs);
112 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
114 public void Encrypt ()
116 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
117 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
118 // we throw the same (instead of a NotImplementedException) because most code should already be
119 // handling this exception to work properly.
120 throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
123 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
125 public void Decrypt ()
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."));
138 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
144 public string DirectoryName {
146 return Path.GetDirectoryName (FullPath);
150 public DirectoryInfo Directory {
152 return new DirectoryInfo (DirectoryName);
156 // streamreader methods
158 public StreamReader OpenText () {
159 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
162 public StreamWriter CreateText () {
163 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
166 public StreamWriter AppendText () {
167 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
170 // filestream methods
172 public FileStream Create ()
174 return File.Create (FullPath);
178 public FileStream OpenRead () {
179 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
182 public FileStream OpenWrite () {
183 return Open (FileMode.OpenOrCreate, FileAccess.Write);
186 public FileStream Open (FileMode mode) {
187 return Open (mode, FileAccess.ReadWrite);
190 public FileStream Open (FileMode mode, FileAccess access) {
191 return Open (mode, access, FileShare.None);
194 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
195 return new FileStream (FullPath, mode, access, share);
200 public override void Delete () {
203 if (!MonoIO.Exists (FullPath, out error)) {
204 // a weird MS.NET behaviour
208 if (MonoIO.ExistsDirectory (FullPath, out error)) {
209 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
212 if (!MonoIO.DeleteFile (FullPath, out error)) {
213 throw MonoIO.GetException (OriginalPath,
218 public void MoveTo (string dest) {
221 throw new ArgumentNullException ();
223 if (dest == Name || dest == FullName)
227 if (MonoIO.Exists (dest, out error) ||
228 MonoIO.ExistsDirectory (dest, out error))
229 throw new IOException ();
230 File.Move (FullPath, dest);
231 this.FullPath = Path.GetFullPath (dest);
234 public FileInfo CopyTo (string path) {
235 return CopyTo (path, false);
238 public FileInfo CopyTo (string path, bool overwrite) {
239 string dest = Path.GetFullPath (path);
241 if (overwrite && File.Exists (path))
244 File.Copy (FullPath, dest);
246 return new FileInfo (dest);
249 public override string ToString () {
254 public FileSecurity GetAccessControl ()
256 throw new NotImplementedException ();
259 public FileSecurity GetAccessControl (AccessControlSections includeSections)
261 throw new NotImplementedException ();
265 public FileInfo Replace (string destinationFileName,
266 string destinationBackupFileName)
268 throw new NotImplementedException ();
272 public FileInfo Replace (string destinationFileName,
273 string destinationBackupFileName,
274 bool ignoreMetadataErrors)
276 throw new NotImplementedException ();
279 public void SetAccessControl (FileSecurity fileSecurity)
281 throw new NotImplementedException ();