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
;
47 public sealed class FileInfo
: FileSystemInfo
56 public FileInfo (string fileName
)
59 throw new ArgumentNullException ("fileName");
63 OriginalPath
= fileName
;
64 FullPath
= Path
.GetFullPath (fileName
);
67 private FileInfo (SerializationInfo info
, StreamingContext context
)
68 : base (info
, context
)
72 internal override void InternalRefresh ()
74 exists
= File
.Exists (FullPath
);
79 public override bool Exists
{
83 if (stat
.Attributes
== MonoIO
.InvalidFileAttributes
)
86 if ((stat
.Attributes
& FileAttributes
.Directory
) != 0)
93 public override string Name
{
95 return Path
.GetFileName (FullPath
);
100 public bool IsReadOnly
{
103 throw new FileNotFoundException ("Could not find file \"" + OriginalPath
+ "\".", OriginalPath
);
105 return ((stat
.Attributes
& FileAttributes
.ReadOnly
) != 0);
110 throw new FileNotFoundException ("Could not find file \"" + OriginalPath
+ "\".", OriginalPath
);
112 FileAttributes attrs
= File
.GetAttributes(FullPath
);
115 attrs
|= FileAttributes
.ReadOnly
;
117 attrs
&= ~FileAttributes
.ReadOnly
;
119 File
.SetAttributes(FullPath
, attrs
);
123 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
125 public void Encrypt ()
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."));
134 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
136 public void Decrypt ()
138 // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
139 // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
140 // we throw the same (instead of a NotImplementedException) because most code should already be
141 // handling this exception to work properly.
142 throw new NotSupportedException (Locale
.GetText ("File encryption isn't supported on any file system."));
149 throw new FileNotFoundException ("Could not find file \"" + OriginalPath
+ "\".", OriginalPath
);
155 public string DirectoryName
{
157 return Path
.GetDirectoryName (FullPath
);
161 public DirectoryInfo Directory
{
163 return new DirectoryInfo (DirectoryName
);
167 // streamreader methods
169 public StreamReader
OpenText ()
171 return new StreamReader (Open (FileMode
.Open
, FileAccess
.Read
));
174 public StreamWriter
CreateText ()
176 return new StreamWriter (Open (FileMode
.Create
, FileAccess
.Write
));
179 public StreamWriter
AppendText ()
181 return new StreamWriter (Open (FileMode
.Append
, FileAccess
.Write
));
184 // filestream methods
186 public FileStream
Create ()
188 return File
.Create (FullPath
);
191 public FileStream
OpenRead ()
193 return Open (FileMode
.Open
, FileAccess
.Read
, FileShare
.Read
);
196 public FileStream
OpenWrite ()
198 return Open (FileMode
.OpenOrCreate
, FileAccess
.Write
);
201 public FileStream
Open (FileMode mode
)
203 return Open (mode
, FileAccess
.ReadWrite
);
206 public FileStream
Open (FileMode mode
, FileAccess access
)
208 return Open (mode
, access
, FileShare
.None
);
211 public FileStream
Open (FileMode mode
, FileAccess access
, FileShare share
)
213 return new FileStream (FullPath
, mode
, access
, share
);
218 public override void Delete ()
222 if (!MonoIO
.Exists (FullPath
, out error
))
223 // a weird MS.NET behaviour
226 if (MonoIO
.ExistsDirectory (FullPath
, out error
))
227 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath
+ "\" is denied.");
228 if (!MonoIO
.DeleteFile (FullPath
, out error
))
229 throw MonoIO
.GetException (OriginalPath
, error
);
232 public void MoveTo (string destFileName
)
234 if (destFileName
== null)
235 throw new ArgumentNullException ("destFileName");
236 if (destFileName
== Name
|| destFileName
== FullName
)
238 if (!File
.Exists (FullPath
))
239 throw new FileNotFoundException ();
241 File
.Move (FullPath
, destFileName
);
242 this.FullPath
= Path
.GetFullPath (destFileName
);
245 public FileInfo
CopyTo (string destFileName
)
247 return CopyTo (destFileName
, false);
250 public FileInfo
CopyTo (string destFileName
, bool overwrite
)
252 if (destFileName
== null)
253 throw new ArgumentNullException ("destFileName");
254 if (destFileName
.Length
== 0)
255 throw new ArgumentException ("An empty file name is not valid.", "destFileName");
257 string dest
= Path
.GetFullPath (destFileName
);
259 if (overwrite
&& File
.Exists (dest
))
262 File
.Copy (FullPath
, dest
);
264 return new FileInfo (dest
);
267 public override string ToString ()
270 // for Moonlight we *never* return paths, since ToString is not [SecurityCritical] we simply return the Name
278 public FileSecurity
GetAccessControl ()
280 throw new NotImplementedException ();
283 public FileSecurity
GetAccessControl (AccessControlSections includeSections
)
285 throw new NotImplementedException ();
289 public FileInfo
Replace (string destinationFileName
,
290 string destinationBackupFileName
)
292 string destinationFullPath
= null;
294 throw new FileNotFoundException ();
295 if (destinationFileName
== null)
296 throw new ArgumentNullException ("destinationFileName");
297 if (destinationFileName
.Length
== 0)
298 throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
300 destinationFullPath
= Path
.GetFullPath (destinationFileName
);
302 if (!File
.Exists (destinationFullPath
))
303 throw new FileNotFoundException ();
305 FileAttributes attrs
= File
.GetAttributes (destinationFullPath
);
307 if ( (attrs
& FileAttributes
.ReadOnly
) == FileAttributes
.ReadOnly
)
308 throw new UnauthorizedAccessException ();
310 if (destinationBackupFileName
!= null) {
311 if (destinationBackupFileName
.Length
== 0)
312 throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
313 File
.Copy (destinationFullPath
, Path
.GetFullPath (destinationBackupFileName
), true);
315 File
.Copy (FullPath
, destinationFullPath
,true);
316 File
.Delete (FullPath
);
317 return new FileInfo (destinationFullPath
);
321 [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
322 public FileInfo
Replace (string destinationFileName
,
323 string destinationBackupFileName
,
324 bool ignoreMetadataErrors
)
326 return Replace (destinationFileName
, destinationBackupFileName
);
329 public void SetAccessControl (FileSecurity fileSecurity
)
331 throw new NotImplementedException ();