7ba28dce4c7e3978e6b05deb27e4b3c40f997042
[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, 2006 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.Runtime.InteropServices;
37
38 namespace System.IO {
39
40         [Serializable]
41 #if NET_2_0
42         [ComVisible (true)]
43 #endif
44         public sealed class FileInfo : FileSystemInfo {
45         
46
47                 private bool exists;
48
49                 public FileInfo (string path) {
50                         CheckPath (path);
51                 
52                         OriginalPath = path;
53                         FullPath = Path.GetFullPath (path);
54                 }
55                 
56                 internal override void InternalRefresh ()
57                 {
58                         exists = File.Exists (FullPath);
59                 }
60
61
62                 // public properties
63
64                 public override bool Exists {
65                         get {
66                                 Refresh (false);
67
68                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)
69                                         return false;
70
71                                 if ((stat.Attributes & FileAttributes.Directory) != 0)
72                                         return false;
73
74                                 return exists;
75                         }
76                 }
77
78                 public override string Name {
79                         get {
80                                 return Path.GetFileName (FullPath);
81                         }
82                 }
83
84 #if NET_2_0
85                 public bool IsReadOnly {
86                         get {
87                                 if (!Exists)
88                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
89                                         
90                                 return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
91                         }
92                                 
93                         set {
94                                 if (!Exists)
95                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
96                                         
97                                 FileAttributes attrs = File.GetAttributes(FullPath);
98
99                                 if (value) 
100                                         attrs |= FileAttributes.ReadOnly;
101                                 else
102                                         attrs &= ~FileAttributes.ReadOnly;                                      
103
104                                 File.SetAttributes(FullPath, attrs);
105                         }
106                 }
107
108                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
109                 [ComVisible (false)]
110                 public void Encrypt ()
111                 {
112                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
113                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
114                         // we throw the same (instead of a NotImplementedException) because most code should already be
115                         // handling this exception to work properly.
116                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
117                 }
118
119                 [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
120                 [ComVisible (false)]
121                 public void Decrypt ()
122                 {
123                         // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
124                         // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
125                         // we throw the same (instead of a NotImplementedException) because most code should already be
126                         // handling this exception to work properly.
127                         throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
128                 }
129 #endif
130
131                 public long Length {
132                         get {
133                                 if (!Exists)
134                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
135
136                                 return stat.Length;
137                         }
138                 }
139
140                 public string DirectoryName {
141                         get {
142                                 return Path.GetDirectoryName (FullPath);
143                         }
144                 }
145
146                 public DirectoryInfo Directory {
147                         get {
148                                 return new DirectoryInfo (DirectoryName);
149                         }
150                 }
151
152                 // streamreader methods
153
154                 public StreamReader OpenText () {
155                         return new StreamReader (Open (FileMode.Open, FileAccess.Read));
156                 }
157
158                 public StreamWriter CreateText () {
159                         return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
160                 }
161                 
162                 public StreamWriter AppendText () {
163                         return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
164                 }
165
166                 // filestream methods
167
168                 public FileStream Create ()
169                 {
170                         return File.Create (FullPath);
171                 }
172                 
173                 
174                 public FileStream OpenRead () {
175                         return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
176                 }
177
178                 public FileStream OpenWrite () {
179                         return Open (FileMode.OpenOrCreate, FileAccess.Write);
180                 }
181
182                 public FileStream Open (FileMode mode) {
183                         return Open (mode, FileAccess.ReadWrite);
184                 }
185
186                 public FileStream Open (FileMode mode, FileAccess access) {
187                         return Open (mode, access, FileShare.None);
188                 }
189
190                 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
191                         return new FileStream (FullPath, mode, access, share);
192                 }
193
194                 // file methods
195
196                 public override void Delete () {
197                         MonoIOError error;
198                                                 
199                         if (!MonoIO.Exists (FullPath, out error)) {
200                                 // a weird MS.NET behaviour
201                                 return;
202                         }
203
204                         if (MonoIO.ExistsDirectory (FullPath, out error)) {
205                                 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
206                         }
207                         
208                         if (!MonoIO.DeleteFile (FullPath, out error)) {
209                                 throw MonoIO.GetException (OriginalPath,
210                                                            error);
211                         }
212                 }
213                 
214                 public void MoveTo (string dest) {
215
216                         if (dest == null)
217                                 throw new ArgumentNullException ();
218
219                         if (dest == Name || dest == FullName)
220                                 return;
221
222                         MonoIOError error;
223                         if (MonoIO.Exists (dest, out error) ||
224                                 MonoIO.ExistsDirectory (dest, out error))
225                                 throw new IOException ();
226                         File.Move (FullPath, dest);
227                         this.FullPath = Path.GetFullPath (dest);
228                 }
229
230                 public FileInfo CopyTo (string path) {
231                         return CopyTo (path, false);
232                 }
233
234                 public FileInfo CopyTo (string path, bool overwrite) {
235                         string dest = Path.GetFullPath (path);
236
237                         if (overwrite && File.Exists (path))
238                                 File.Delete (path);
239
240                         File.Copy (FullPath, dest);
241                 
242                         return new FileInfo (dest);
243                 }
244
245                 public override string ToString () {
246                         return OriginalPath;
247                 }
248         }
249 }