2003-02-25 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / System.IO / File.cs
1 // \r
2 // System.IO.FIle.cs \r
3 //\r
4 // \r
5 // Authors:\r
6 //   Miguel de Icaza (miguel@ximian.com)\r
7 //   Jim Richardson  (develop@wtfo-guru.com)\r
8 //   Dan Lewis       (dihlewis@yahoo.co.uk)\r
9 //\r
10 // Copyright 2002 Ximian, Inc. http://www.ximian.com\r
11 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
12 //\r
13 \r
14 using System;\r
15 \r
16 namespace System.IO\r
17 {\r
18         /// <summary>\r
19         /// \r
20         /// </summary>\r
21         public sealed class File\r
22         {\r
23                 private File () {}
24
25                 \r
26                 \r
27                 public static StreamWriter AppendText (string path)\r
28                 {       \r
29                         return new StreamWriter (path, true);\r
30                 }\r
31 \r
32                 [MonoTODO("Security Permision Checks")]\r
33                 public static void Copy (string sourceFilename, string destFilename)\r
34                 {\r
35                         Copy (sourceFilename, destFilename, false);\r
36                 }\r
37 \r
38                 public static void Copy (string src, string dest, bool overwrite)\r
39                 {       \r
40                         if (src == null)\r
41                                 throw new ArgumentNullException ("src");\r
42                         if (dest == null)\r
43                                 throw new ArgumentNullException ("dest");\r
44                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
45                                 throw new ArgumentException ("src");\r
46                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
47                                 throw new ArgumentException ("dest");\r
48                         if (!Exists (src))
49                                 throw new FileNotFoundException (src + " does not exist");\r
50
51                         if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
52                                 throw new ArgumentException(src + " is a directory");
53                         }
54                         
55                         if (Exists (dest)) {\r
56                                 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){\r
57                                         throw new ArgumentException(dest + " is a directory");  \r
58                                 }\r
59                                 if (!overwrite)\r
60                                         throw new IOException (dest + " already exists");\r
61                         }\r
62 \r
63                         string DirName = Path.GetDirectoryName(dest);\r
64                         if (DirName != String.Empty && !Directory.Exists (DirName))
65                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
66
67                         MonoIOError error;
68                         
69                         if (!MonoIO.CopyFile (src, dest, overwrite, out error))
70                                 throw MonoIO.GetException (error);
71                 }
72 \r
73                 public static FileStream Create (string path)\r
74                 {\r
75                         return Create (path, 8192);\r
76                 }\r
77 \r
78                 public static FileStream Create (string path, int buffersize)\r
79                 {\r
80                         if (null == path)\r
81                                 throw new ArgumentNullException("path");\r
82                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
83                                 throw new ArgumentException("path");\r
84 \r
85                         string DirName = Path.GetDirectoryName(path);\r
86                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
87                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
88                         if (Exists(path)){\r
89                                 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){\r
90                                         throw new UnauthorizedAccessException(path + " is a read-only");        \r
91                                 }\r
92                         }\r
93 \r
94                         return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,\r
95                                                FileShare.None, buffersize);\r
96                 }\r
97
98                 public static StreamWriter CreateText(string path)
99                 \r
100                 {\r
101                         return new StreamWriter (path, false);
102                 \r
103                 }
104                 
105                 \r
106                 \r
107                 public static void Delete (string path)\r
108                 {\r
109                         if (null == path)\r
110                                 throw new ArgumentNullException("path");\r
111                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
112                                 throw new ArgumentException("path");\r
113                         if (Directory.Exists (path))\r
114                                 throw new UnauthorizedAccessException("path is a directory");\r
115 \r
116                         string DirName = Path.GetDirectoryName(path);\r
117                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
118                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
119
120                         MonoIOError error;
121                         
122                         if (!MonoIO.DeleteFile (path, out error)){
123                                 Exception e = MonoIO.GetException (error);
124                                 if (! (e is FileNotFoundException))
125                                         throw e;
126                         }
127                 }\r
128
129                 public static bool Exists (string path)
130                 {
131                         // For security reasons no exceptions are
132                         // thrown, only false is returned if there is
133                         // any problem with the path or permissions.
134                         // Minimizes what information can be
135                         // discovered by using this method.
136                         if (null == path || String.Empty == path.Trim()
137                             || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
138                                 return false;
139                         }
140
141                         MonoIOError error;
142                         
143                         return MonoIO.ExistsFile (path, out error);
144                 }
145
146                 public static FileAttributes GetAttributes (string path)
147                 {
148                         if (null == path) {
149                                 throw new ArgumentNullException("path");
150                         }
151                         
152                         if (String.Empty == path.Trim()) {
153                                 throw new ArgumentException("Path is empty");
154                         }
155
156                         if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
157                                 throw new ArgumentException("Path contains invalid chars");
158                         }
159
160                         MonoIOError error;
161                         
162                         return MonoIO.GetFileAttributes (path, out error);
163                 }
164
165                 public static DateTime GetCreationTime (string path)
166                 {
167                         MonoIOStat stat;
168                         MonoIOError error;
169                         
170                         if (!MonoIO.GetFileStat (path, out stat, out error))
171                                 throw new IOException (path);
172                         return DateTime.FromFileTime (stat.CreationTime);
173                 }
174
175                 public static DateTime GetLastAccessTime (string path)
176                 {
177                         MonoIOStat stat;
178                         MonoIOError error;
179                         
180                         if (!MonoIO.GetFileStat (path, out stat, out error))
181                                 throw MonoIO.GetException (path, error);
182                         return DateTime.FromFileTime (stat.LastAccessTime);
183                 }
184
185                 public static DateTime GetLastWriteTime (string path)
186                 {
187                         MonoIOStat stat;
188                         MonoIOError error;
189                         
190                         if (!MonoIO.GetFileStat (path, out stat, out error))
191                                 throw MonoIO.GetException (path, error);
192                         return DateTime.FromFileTime (stat.LastWriteTime);
193                 }
194
195                 public static void Move (string src, string dest)\r
196                 {\r
197                         if (src == null)\r
198                                 throw new ArgumentNullException ("src");\r
199                         if (dest == null)\r
200                                 throw new ArgumentNullException ("dest");\r
201                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
202                                 throw new ArgumentException ("src");\r
203                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
204                                 throw new ArgumentException ("dest");\r
205                         if (!Exists (src))\r
206                                 throw new FileNotFoundException (src + " does not exist");\r
207                         if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))\r
208                                         throw new ArgumentException(dest + " is a directory");  \r
209 \r
210                         string DirName;\r
211                         DirName = Path.GetDirectoryName(src);\r
212                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
213                                 throw new DirectoryNotFoundException("Source directory not found: " + DirName);\r
214                         DirName = Path.GetDirectoryName(dest);\r
215                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
216                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
217
218                         MonoIOError error;
219                         
220                         if (!MonoIO.MoveFile (src, dest, out error))
221                                 throw MonoIO.GetException (error);
222                 }
223                 \r
224                 public static FileStream Open (string path, FileMode mode)\r
225                 {       \r
226                         return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);\r
227                 }\r
228                 \r
229                 public static FileStream Open (string path, FileMode mode, FileAccess access)\r
230                 {       \r
231                         return new FileStream (path, mode, access, FileShare.None);\r
232                 }\r
233 \r
234                 public static FileStream Open (string path, FileMode mode, FileAccess access,\r
235                                                FileShare share)\r
236                 {\r
237                         return new FileStream (path, mode, access, share);\r
238                 }\r
239                 \r
240                 public static FileStream OpenRead (string path)\r
241                 {       \r
242                         return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);\r
243                 }\r
244 \r
245                 public static StreamReader OpenText (string path)\r
246                 {\r
247                         return new StreamReader (path);\r
248                 }\r
249 \r
250                 public static FileStream OpenWrite (string path)\r
251                 {\r
252                         return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);\r
253                 }\r
254
255                 public static void SetAttributes (string path,
256                                                   FileAttributes attributes)
257                 {
258                         MonoIOError error;
259                         
260                         if (!MonoIO.SetFileAttributes (path, attributes,
261                                                        out error)) {
262                                 throw MonoIO.GetException (path, error);
263                         }
264                 }
265
266                 public static void SetCreationTime (string path,
267                                                     DateTime creation_time)
268                 {
269                         MonoIOError error;
270                         
271                         if (!MonoIO.SetFileTime (path, creation_time.Ticks,
272                                                  -1, -1, out error)) {
273                                 throw MonoIO.GetException (path, error);
274                         }
275                 }
276
277                 public static void SetLastAccessTime (string path,DateTime last_access_time)
278                 {
279                         MonoIOError error;
280                         
281                         if (!MonoIO.SetFileTime (path, -1,
282                                                  last_access_time.Ticks, -1,
283                                                  out error)) {
284                                 throw MonoIO.GetException (path, error);
285                         }
286                 }
287
288                 public static void SetLastWriteTime (string path,
289                                                      DateTime last_write_time)
290                 {
291                         MonoIOError error;
292                         
293                         if (!MonoIO.SetFileTime (path, -1, -1,
294                                                  last_write_time.Ticks,
295                                                  out error)) {
296                                 throw MonoIO.GetException (path, error);
297                         }
298                 }
299         }
300 }