1 //------------------------------------------------------------------------------
\r
3 // System.IO.FileInfo.cs
\r
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
\r
7 // Author: Jim Richardson, develop@wtfo-guru.com
\r
8 // Dan Lewis (dihlewis@yahoo.co.uk)
\r
9 // Created: Monday, August 13, 2001
\r
11 //------------------------------------------------------------------------------
\r
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.
38 namespace System.IO {
\r
41 public sealed class FileInfo : FileSystemInfo {
\r
44 private bool exists;
\r
46 public FileInfo (string path) {
\r
49 OriginalPath = path;
\r
50 FullPath = Path.GetFullPath (path);
\r
53 internal override void InternalRefresh ()
\r
55 exists = File.Exists (FullPath);
\r
59 // public properties
\r
61 public override bool Exists {
\r
65 if (stat.Attributes == MonoIO.InvalidFileAttributes)
\r
68 if ((stat.Attributes & FileAttributes.Directory) != 0)
\r
75 public override string Name {
\r
77 return Path.GetFileName (FullPath);
\r
81 public long Length {
\r
84 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
\r
90 public string DirectoryName {
\r
92 return Path.GetDirectoryName (FullPath);
\r
96 public DirectoryInfo Directory {
\r
98 return new DirectoryInfo (DirectoryName);
\r
102 // streamreader methods
\r
104 public StreamReader OpenText () {
\r
105 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
\r
108 public StreamWriter CreateText () {
\r
109 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
\r
112 public StreamWriter AppendText () {
\r
113 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
\r
116 // filestream methods
\r
118 public FileStream Create ()
\r
120 return File.Create (FullPath);
\r
124 public FileStream OpenRead () {
\r
125 return Open (FileMode.Open, FileAccess.Read);
\r
128 public FileStream OpenWrite () {
\r
129 return Open (FileMode.OpenOrCreate, FileAccess.Write);
\r
132 public FileStream Open (FileMode mode) {
\r
133 return Open (mode, FileAccess.ReadWrite);
\r
136 public FileStream Open (FileMode mode, FileAccess access) {
\r
137 return Open (mode, access, FileShare.None);
\r
140 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
\r
141 return new FileStream (FullPath, mode, access, share);
\r
146 public override void Delete () {
\r
149 if (!MonoIO.Exists (FullPath, out error)) {
\r
150 // a weird MS.NET behaviour
\r
154 if (MonoIO.ExistsDirectory (FullPath, out error)) {
\r
155 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
\r
158 if (!MonoIO.DeleteFile (FullPath, out error)) {
\r
159 throw MonoIO.GetException (OriginalPath,
\r
164 public void MoveTo (string dest) {
\r
167 throw new ArgumentNullException ();
\r
169 if (dest == Name || dest == FullName)
\r
173 if (MonoIO.Exists (dest, out error) ||
\r
174 MonoIO.ExistsDirectory (dest, out error))
\r
175 throw new IOException ();
\r
176 File.Move (FullPath, dest);
\r
177 this.FullPath = Path.GetFullPath (dest);
\r
180 public FileInfo CopyTo (string path) {
\r
181 return CopyTo (path, false);
\r
184 public FileInfo CopyTo (string path, bool overwrite) {
\r
185 string dest = Path.GetFullPath (path);
\r
187 if (overwrite && File.Exists (path))
\r
188 File.Delete (path);
\r
190 File.Copy (FullPath, dest);
\r
192 return new FileInfo (dest);
\r
195 public override string ToString () {
\r
196 return OriginalPath;
\r