Implementation of the 2.0 session state model
[mono.git] / mcs / class / corlib / System.IO / FileInfo.cs
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 
10 //
11 //------------------------------------------------------------------------------
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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.
34 //
35
36 using System;
37
38 namespace System.IO {
39
40         [Serializable]
41         public sealed class FileInfo : FileSystemInfo {
42         
43
44                 private bool exists;
45
46                 public FileInfo (string path) {
47                         CheckPath (path);
48                 
49                         OriginalPath = path;
50                         FullPath = Path.GetFullPath (path);
51                 }
52                 
53                 internal override void InternalRefresh ()
54                 {
55                         exists = File.Exists (FullPath);
56                 }
57
58
59                 // public properties
60
61                 public override bool Exists {
62                         get {
63                                 Refresh (false);
64
65                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
66                                         return false;
67
68                                 if ((stat.Attributes & FileAttributes.Directory) != 0)
69                                         return false;
70
71                                 return exists;
72                         }
73                 }
74
75                 public override string Name {
76                         get {
77                                 return Path.GetFileName (FullPath);
78                         }
79                 }
80
81 #if NET_2_0
82                 public bool IsReadOnly {
83                         get {
84                                 if (!Exists)
85                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
86                                         
87                                 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
88                         }
89                                 
90                         set {
91                                 if (!Exists)
92                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
93                                         
94                                 FileAttributes attrs = File.GetAttributes(FullPath);
95
96                                 if (value) 
97                                         attrs |= FileAttributes.ReadOnly;
98                                 else
99                                         attrs &= ~FileAttributes.ReadOnly;                                      
100
101                                 File.SetAttributes(FullPath, attrs);
102                         }
103                 }
104
105                 void NotSupported ()
106                 {
107                         throw new NotSupportedException ("Mono does not support this operation");
108                 }
109                 
110                 public void Encrypt ()
111                 {
112                         NotSupported ();
113                 }
114
115                 public void Decrypt ()
116                 {
117                         NotSupported ();
118                 }
119 #endif
120
121                 public long Length {
122                         get {
123                                 if (!Exists)
124                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
125
126                                 return stat.Length;
127                         }
128                 }
129
130                 public string DirectoryName {
131                         get {
132                                 return Path.GetDirectoryName (FullPath);
133                         }
134                 }
135
136                 public DirectoryInfo Directory {
137                         get {
138                                 return new DirectoryInfo (DirectoryName);
139                         }
140                 }
141
142                 // streamreader methods
143
144                 public StreamReader OpenText () {
145                         return new StreamReader (Open (FileMode.Open, FileAccess.Read));
146                 }
147
148                 public StreamWriter CreateText () {
149                         return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
150                 }
151                 
152                 public StreamWriter AppendText () {
153                         return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
154                 }
155
156                 // filestream methods
157
158                 public FileStream Create ()
159                 {
160                         return File.Create (FullPath);
161                 }
162                 
163                 
164                 public FileStream OpenRead () {
165                         return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
166                 }
167
168                 public FileStream OpenWrite () {
169                         return Open (FileMode.OpenOrCreate, FileAccess.Write);
170                 }
171
172                 public FileStream Open (FileMode mode) {
173                         return Open (mode, FileAccess.ReadWrite);
174                 }
175
176                 public FileStream Open (FileMode mode, FileAccess access) {
177                         return Open (mode, access, FileShare.None);
178                 }
179
180                 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
181                         return new FileStream (FullPath, mode, access, share);
182                 }
183
184                 // file methods
185
186                 public override void Delete () {
187                         MonoIOError error;
188                                                 
189                         if (!MonoIO.Exists (FullPath, out error)) {
190                                 // a weird MS.NET behaviour
191                                 return;
192                         }
193
194                         if (MonoIO.ExistsDirectory (FullPath, out error)) {
195                                 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
196                         }
197                         
198                         if (!MonoIO.DeleteFile (FullPath, out error)) {
199                                 throw MonoIO.GetException (OriginalPath,
200                                                            error);
201                         }
202                 }
203                 
204                 public void MoveTo (string dest) {
205
206                         if (dest == null)
207                                 throw new ArgumentNullException ();
208
209                         if (dest == Name || dest == FullName)
210                                 return;
211
212                         MonoIOError error;
213                         if (MonoIO.Exists (dest, out error) ||
214                                 MonoIO.ExistsDirectory (dest, out error))
215                                 throw new IOException ();
216                         File.Move (FullPath, dest);
217                         this.FullPath = Path.GetFullPath (dest);
218                 }
219
220                 public FileInfo CopyTo (string path) {
221                         return CopyTo (path, false);
222                 }
223
224                 public FileInfo CopyTo (string path, bool overwrite) {
225                         string dest = Path.GetFullPath (path);
226
227                         if (overwrite && File.Exists (path))
228                                 File.Delete (path);
229
230                         File.Copy (FullPath, dest);
231                 
232                         return new FileInfo (dest);
233                 }
234
235                 public override string ToString () {
236                         return OriginalPath;
237                 }
238         }
239 }