2003-06-02 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / System.IO / FileInfo.cs
1 //------------------------------------------------------------------------------\r
2 // \r
3 // System.IO.FileInfo.cs \r
4 //\r
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
6 // \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
10 //\r
11 //------------------------------------------------------------------------------\r
12 \r
13 using System;\r
14 \r
15 namespace System.IO {\r
16 \r
17         [Serializable]
18         public sealed class FileInfo : FileSystemInfo {\r
19         \r
20
21                 private bool exists = false;
22
23                 public FileInfo (string path) {\r
24                         CheckPath (path);\r
25                 \r
26                         OriginalPath = path;\r
27                         FullPath = Path.GetFullPath (path);
28                         exists = File.Exists (path);
29                 }\r
30 \r
31                 // public properties\r
32 \r
33                 public override bool Exists {\r
34                         get {\r
35                                 Refresh (false);\r
36 \r
37                                 if (stat.Attributes == MonoIO.InvalidFileAttributes)\r
38                                         return false;\r
39 \r
40                                 if ((stat.Attributes & FileAttributes.Directory) != 0)\r
41                                         return false;\r
42 \r
43                                 return exists;
44                         }\r
45                 }\r
46 \r
47                 public override string Name {\r
48                         get {\r
49                                 return Path.GetFileName (FullPath);\r
50                         }\r
51                 }\r
52 \r
53                 public long Length {\r
54                         get {\r
55                                 if (!Exists)\r
56                                         throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".");\r
57 \r
58                                 return stat.Length;\r
59                         }\r
60                 }\r
61 \r
62                 public string DirectoryName {\r
63                         get {\r
64                                 return Path.GetDirectoryName (FullPath);\r
65                         }\r
66                 }\r
67 \r
68                 public DirectoryInfo Directory {\r
69                         get {\r
70                                 return new DirectoryInfo (DirectoryName);\r
71                         }\r
72                 }\r
73 \r
74                 // streamreader methods\r
75 \r
76                 public StreamReader OpenText () {\r
77                         return new StreamReader (Open (FileMode.Open, FileAccess.Read));\r
78                 }\r
79 \r
80                 public StreamWriter CreateText () {\r
81                         return new StreamWriter (Open (FileMode.Create, FileAccess.Write));\r
82                 }\r
83                 \r
84                 public StreamWriter AppendText () {\r
85                         return new StreamWriter (Open (FileMode.Append, FileAccess.Write));\r
86                 }\r
87 \r
88                 // filestream methods\r
89
90                 public FileStream Create ()
91                 {
92                         return File.Create (FullPath);
93                 }
94                 
95                 \r
96                 public FileStream OpenRead () {\r
97                         return Open (FileMode.Open, FileAccess.Read);\r
98                 }\r
99 \r
100                 public FileStream OpenWrite () {\r
101                         return Open (FileMode.OpenOrCreate, FileAccess.Write);\r
102                 }\r
103 \r
104                 public FileStream Open (FileMode mode) {\r
105                         return Open (mode, FileAccess.ReadWrite);\r
106                 }\r
107 \r
108                 public FileStream Open (FileMode mode, FileAccess access) {\r
109                         return Open (mode, access, FileShare.None);\r
110                 }\r
111 \r
112                 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {\r
113                         return new FileStream (FullPath, mode, access, share);\r
114                 }\r
115 \r
116                 // file methods\r
117
118                 public override void Delete () {
119                         MonoIOError error;
120                                                 
121                         if (!MonoIO.Exists (FullPath, out error)) {
122                                 // a weird MS.NET behaviour
123                                 return;
124                         }
125
126                         if (MonoIO.ExistsDirectory (FullPath, out error)) {
127                                 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
128                         }
129                         
130                         if (!MonoIO.DeleteFile (FullPath, out error)) {
131                                 throw MonoIO.GetException (OriginalPath,
132                                                            error);
133                         }
134                 }
135                 
136                 public void MoveTo (string dest) {
137                         if (dest == null)
138                                 throw new ArgumentNullException ();
139                         MonoIOError error;
140                         if (MonoIO.Exists (dest, out error) ||
141                                 MonoIO.ExistsDirectory (dest, out error))
142                                 throw new IOException ();
143                         File.Move (FullPath, dest);
144                 }
145
146                 public FileInfo CopyTo (string path) {\r
147                         return CopyTo (path, false);\r
148                 }\r
149 \r
150                 public FileInfo CopyTo (string path, bool overwrite) {\r
151                         string dest = Path.GetFullPath (path);\r
152
153                         if (overwrite && File.Exists (path))
154                                 File.Delete (path);
155
156                         File.Copy (FullPath, dest);\r
157                 \r
158                         return new FileInfo (dest);\r
159                 }\r
160 \r
161                 public override string ToString () {\r
162                         return OriginalPath;\r
163                 }\r
164         }\r
165 }\r