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 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.
41 public sealed class FileInfo : FileSystemInfo {
46 public FileInfo (string path) {
50 FullPath = Path.GetFullPath (path);
53 internal override void InternalRefresh ()
55 exists = File.Exists (FullPath);
61 public override bool Exists {
65 if (stat.Attributes == MonoIO.InvalidFileAttributes)
68 if ((stat.Attributes & FileAttributes.Directory) != 0)
75 public override string Name {
77 return Path.GetFileName (FullPath);
82 public bool IsReadOnly {
85 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
87 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
92 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
94 FileAttributes attrs = File.GetAttributes(FullPath);
97 attrs |= FileAttributes.ReadOnly;
99 attrs &= ~FileAttributes.ReadOnly;
101 File.SetAttributes(FullPath, attrs);
109 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
115 public string DirectoryName {
117 return Path.GetDirectoryName (FullPath);
121 public DirectoryInfo Directory {
123 return new DirectoryInfo (DirectoryName);
127 // streamreader methods
129 public StreamReader OpenText () {
130 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
133 public StreamWriter CreateText () {
134 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
137 public StreamWriter AppendText () {
138 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
141 // filestream methods
143 public FileStream Create ()
145 return File.Create (FullPath);
149 public FileStream OpenRead () {
150 return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
153 public FileStream OpenWrite () {
154 return Open (FileMode.OpenOrCreate, FileAccess.Write);
157 public FileStream Open (FileMode mode) {
158 return Open (mode, FileAccess.ReadWrite);
161 public FileStream Open (FileMode mode, FileAccess access) {
162 return Open (mode, access, FileShare.None);
165 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
166 return new FileStream (FullPath, mode, access, share);
171 public override void Delete () {
174 if (!MonoIO.Exists (FullPath, out error)) {
175 // a weird MS.NET behaviour
179 if (MonoIO.ExistsDirectory (FullPath, out error)) {
180 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
183 if (!MonoIO.DeleteFile (FullPath, out error)) {
184 throw MonoIO.GetException (OriginalPath,
189 public void MoveTo (string dest) {
192 throw new ArgumentNullException ();
194 if (dest == Name || dest == FullName)
198 if (MonoIO.Exists (dest, out error) ||
199 MonoIO.ExistsDirectory (dest, out error))
200 throw new IOException ();
201 File.Move (FullPath, dest);
202 this.FullPath = Path.GetFullPath (dest);
205 public FileInfo CopyTo (string path) {
206 return CopyTo (path, false);
209 public FileInfo CopyTo (string path, bool overwrite) {
210 string dest = Path.GetFullPath (path);
212 if (overwrite && File.Exists (path))
215 File.Copy (FullPath, dest);
217 return new FileInfo (dest);
220 public override string ToString () {