(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.IO / FileInfo.cs
blob3345f61dc79dd7ea66f06fd38a75affae389c49a
1 //------------------------------------------------------------------------------
2 //
3 // System.IO.FileInfo.cs
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 //
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:
23 //
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 //
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;
38 namespace System.IO {
40 [Serializable]
41 public sealed class FileInfo : FileSystemInfo {
44 private bool exists;
46 public FileInfo (string path) {
47 CheckPath (path);
49 OriginalPath = path;
50 FullPath = Path.GetFullPath (path);
53 internal override void InternalRefresh ()
55 exists = File.Exists (FullPath);
59 // public properties
61 public override bool Exists {
62 get {
63 Refresh (false);
65 if (stat.Attributes == MonoIO.InvalidFileAttributes)
66 return false;
68 if ((stat.Attributes & FileAttributes.Directory) != 0)
69 return false;
71 return exists;
75 public override string Name {
76 get {
77 return Path.GetFileName (FullPath);
81 public long Length {
82 get {
83 if (!Exists)
84 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
86 return stat.Length;
90 public string DirectoryName {
91 get {
92 return Path.GetDirectoryName (FullPath);
96 public DirectoryInfo Directory {
97 get {
98 return new DirectoryInfo (DirectoryName);
102 // streamreader methods
104 public StreamReader OpenText () {
105 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
108 public StreamWriter CreateText () {
109 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
112 public StreamWriter AppendText () {
113 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
116 // filestream methods
118 public FileStream Create ()
120 return File.Create (FullPath);
124 public FileStream OpenRead () {
125 return Open (FileMode.Open, FileAccess.Read);
128 public FileStream OpenWrite () {
129 return Open (FileMode.OpenOrCreate, FileAccess.Write);
132 public FileStream Open (FileMode mode) {
133 return Open (mode, FileAccess.ReadWrite);
136 public FileStream Open (FileMode mode, FileAccess access) {
137 return Open (mode, access, FileShare.None);
140 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
141 return new FileStream (FullPath, mode, access, share);
144 // file methods
146 public override void Delete () {
147 MonoIOError error;
149 if (!MonoIO.Exists (FullPath, out error)) {
150 // a weird MS.NET behaviour
151 return;
154 if (MonoIO.ExistsDirectory (FullPath, out error)) {
155 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
158 if (!MonoIO.DeleteFile (FullPath, out error)) {
159 throw MonoIO.GetException (OriginalPath,
160 error);
164 public void MoveTo (string dest) {
166 if (dest == null)
167 throw new ArgumentNullException ();
169 if (dest == Name || dest == FullName)
170 return;
172 MonoIOError error;
173 if (MonoIO.Exists (dest, out error) ||
174 MonoIO.ExistsDirectory (dest, out error))
175 throw new IOException ();
176 File.Move (FullPath, dest);
177 this.FullPath = Path.GetFullPath (dest);
180 public FileInfo CopyTo (string path) {
181 return CopyTo (path, false);
184 public FileInfo CopyTo (string path, bool overwrite) {
185 string dest = Path.GetFullPath (path);
187 if (overwrite && File.Exists (path))
188 File.Delete (path);
190 File.Copy (FullPath, dest);
192 return new FileInfo (dest);
195 public override string ToString () {
196 return OriginalPath;