2003-11-14 Ben Maurer <bmaurer@users.sourceforge.net>
[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 //   Ville Palo      (vi64pa@kolumbus.fi)
10 //\r
11 // Copyright 2002 Ximian, Inc. http://www.ximian.com\r
12 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
13 //\r
14 \r
15 using System;\r
16 \r
17 namespace System.IO\r
18 {\r
19         /// <summary>\r
20         /// \r
21         /// </summary>\r
22         public sealed class File\r
23         {\r
24                 private File () {}
25
26                 \r
27                 \r
28                 public static StreamWriter AppendText (string path)\r
29                 {       \r
30                         return new StreamWriter (path, true);\r
31                 }\r
32 \r
33                 [MonoTODO("Security Permision Checks")]\r
34                 public static void Copy (string sourceFilename, string destFilename)\r
35                 {\r
36                         Copy (sourceFilename, destFilename, false);\r
37                 }\r
38 \r
39                 public static void Copy (string src, string dest, bool overwrite)\r
40                 {       \r
41                         if (src == null)\r
42                                 throw new ArgumentNullException ("src");\r
43                         if (dest == null)\r
44                                 throw new ArgumentNullException ("dest");\r
45                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
46                                 throw new ArgumentException ("src");\r
47                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
48                                 throw new ArgumentException ("dest");\r
49                         if (!Exists (src))
50                                 throw new FileNotFoundException (src + " does not exist");\r
51
52                         if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
53                                 throw new ArgumentException(src + " is a directory");
54                         }
55                         
56                         if (Exists (dest)) {\r
57                                 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){\r
58                                         throw new ArgumentException(dest + " is a directory");  \r
59                                 }\r
60                                 if (!overwrite)\r
61                                         throw new IOException (dest + " already exists");\r
62                         }\r
63 \r
64                         string DirName = Path.GetDirectoryName(dest);\r
65                         if (DirName != String.Empty && !Directory.Exists (DirName))
66                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
67
68                         MonoIOError error;
69                         
70                         if (!MonoIO.CopyFile (src, dest, overwrite, out error))
71                                 throw MonoIO.GetException (error);
72                 }
73 \r
74                 public static FileStream Create (string path)\r
75                 {\r
76                         return Create (path, 8192);\r
77                 }\r
78 \r
79                 public static FileStream Create (string path, int buffersize)\r
80                 {\r
81                         if (null == path)\r
82                                 throw new ArgumentNullException("path");\r
83                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
84                                 throw new ArgumentException("path");\r
85 \r
86                         string DirName = Path.GetDirectoryName(path);\r
87                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
88                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
89                         if (Exists(path)){\r
90                                 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){\r
91                                         throw new UnauthorizedAccessException(path + " is a read-only");        \r
92                                 }\r
93                         }\r
94 \r
95                         return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,\r
96                                                FileShare.None, buffersize);\r
97                 }\r
98
99                 public static StreamWriter CreateText(string path)
100                 \r
101                 {\r
102                         return new StreamWriter (path, false);
103                 \r
104                 }
105                 
106                 \r
107                 \r
108                 public static void Delete (string path)\r
109                 {\r
110                         if (null == path)\r
111                                 throw new ArgumentNullException("path");\r
112                         if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)\r
113                                 throw new ArgumentException("path");\r
114                         if (Directory.Exists (path))\r
115                                 throw new UnauthorizedAccessException("path is a directory");\r
116 \r
117                         string DirName = Path.GetDirectoryName(path);\r
118                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
119                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
120
121                         MonoIOError error;
122                         
123                         if (!MonoIO.DeleteFile (path, out error)){
124                                 Exception e = MonoIO.GetException (error);
125                                 if (! (e is FileNotFoundException))
126                                         throw e;
127                         }
128                 }\r
129
130                 public static bool Exists (string path)
131                 {
132                         // For security reasons no exceptions are
133                         // thrown, only false is returned if there is
134                         // any problem with the path or permissions.
135                         // Minimizes what information can be
136                         // discovered by using this method.
137                         if (null == path || String.Empty == path.Trim()
138                             || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
139                                 return false;
140                         }
141
142                         MonoIOError error;
143                         
144                         return MonoIO.ExistsFile (path, out error);
145                 }
146
147                 public static FileAttributes GetAttributes (string path)
148                 {
149                         if (null == path) {
150                                 throw new ArgumentNullException("path");
151                         }
152                         
153                         if (String.Empty == path.Trim()) {
154                                 throw new ArgumentException("Path is empty");
155                         }
156
157                         if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
158                                 throw new ArgumentException("Path contains invalid chars");
159                         }
160
161                         MonoIOError error;
162                         
163                         return MonoIO.GetFileAttributes (path, out error);
164                 }
165
166                 public static DateTime GetCreationTime (string path)
167                 {
168                         MonoIOStat stat;
169                         MonoIOError error;
170                         CheckPathExceptions (path);
171                         
172                         if (!MonoIO.GetFileStat (path, out stat, out error))
173                                 throw new IOException (path);
174                         return DateTime.FromFileTime (stat.CreationTime);
175                 }
176
177                 public static DateTime GetCreationTimeUtc (string path)
178                 {
179                         return GetCreationTime (path).ToUniversalTime ();
180                 }
181
182                 public static DateTime GetLastAccessTime (string path)
183                 {
184                         MonoIOStat stat;
185                         MonoIOError error;
186                         CheckPathExceptions (path);
187
188                         if (!MonoIO.GetFileStat (path, out stat, out error))
189                                 throw new IOException (path);
190                         return DateTime.FromFileTime (stat.LastAccessTime);
191                 }
192
193                 public static DateTime GetLastAccessTimeUtc (string path)
194                 {
195                         return GetLastAccessTime (path).ToUniversalTime ();
196                 }
197
198                 public static DateTime GetLastWriteTime (string path)
199                 {
200                         MonoIOStat stat;
201                         MonoIOError error;
202                         CheckPathExceptions (path);
203
204                         if (!MonoIO.GetFileStat (path, out stat, out error))
205                                 throw new IOException (path);
206                         return DateTime.FromFileTime (stat.LastWriteTime);
207                 }
208
209                 public static DateTime GetLastWriteTimeUtc (string path)
210                 {
211                         return GetLastWriteTime (path).ToUniversalTime ();
212                 }
213
214                 public static void Move (string src, string dest)\r
215                 {\r
216                         MonoIOError error;
217
218                         if (src == null)\r
219                                 throw new ArgumentNullException ("src");\r
220                         if (dest == null)\r
221                                 throw new ArgumentNullException ("dest");\r
222                         if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)\r
223                                 throw new ArgumentException ("src");\r
224                         if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)\r
225                                 throw new ArgumentException ("dest");\r
226                         if (!MonoIO.Exists (src, out error))\r
227                                 throw new FileNotFoundException (src + " does not exist");\r
228                         if (MonoIO.ExistsDirectory (dest, out error))
229                                         throw new ArgumentException(dest + " is a directory");  \r
230 \r
231                         string DirName;\r
232                         DirName = Path.GetDirectoryName(src);\r
233                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
234                                 throw new DirectoryNotFoundException("Source directory not found: " + DirName);\r
235                         DirName = Path.GetDirectoryName(dest);\r
236                         if (DirName != String.Empty && !Directory.Exists (DirName))\r
237                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
238
239                         if (!MonoIO.MoveFile (src, dest, out error))
240                                 throw MonoIO.GetException (error);
241                 }
242                 \r
243                 public static FileStream Open (string path, FileMode mode)\r
244                 {       \r
245                         return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);\r
246                 }\r
247                 \r
248                 public static FileStream Open (string path, FileMode mode, FileAccess access)\r
249                 {       \r
250                         return new FileStream (path, mode, access, FileShare.None);\r
251                 }\r
252 \r
253                 public static FileStream Open (string path, FileMode mode, FileAccess access,\r
254                                                FileShare share)\r
255                 {\r
256                         return new FileStream (path, mode, access, share);\r
257                 }\r
258                 \r
259                 public static FileStream OpenRead (string path)\r
260                 {       \r
261                         return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);\r
262                 }\r
263 \r
264                 public static StreamReader OpenText (string path)\r
265                 {\r
266                         return new StreamReader (path);\r
267                 }\r
268 \r
269                 public static FileStream OpenWrite (string path)\r
270                 {\r
271                         return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);\r
272                 }\r
273
274                 public static void SetAttributes (string path,
275                                                   FileAttributes attributes)
276                 {
277                         MonoIOError error;
278                         CheckPathExceptions (path);
279                         
280                         if (!MonoIO.SetFileAttributes (path, attributes,
281                                                        out error)) {
282                                 throw MonoIO.GetException (path, error);
283                         }
284                 }
285
286                 public static void SetCreationTime (string path,
287                                                     DateTime creation_time)
288                 {
289                         MonoIOError error;
290                         CheckPathExceptions (path);
291                         if (!MonoIO.Exists (path, out error))
292                                 throw MonoIO.GetException (path, error);
293                         
294                         if (!MonoIO.SetFileTime (path, creation_time.ToFileTime(),
295                                                  -1, -1, out error)) {
296                                 throw MonoIO.GetException (path, error);
297                         }
298                 }
299
300                 public static void SetCreationTimeUtc (string path,
301                                                     DateTime creation_time)
302                 {
303                         SetCreationTime (path, creation_time.ToLocalTime ());
304                 }
305
306                 public static void SetLastAccessTime (string path,DateTime last_access_time)
307                 {
308                         MonoIOError error;
309                         CheckPathExceptions (path);
310                         if (!MonoIO.Exists (path, out error))
311                                 throw MonoIO.GetException (path, error);
312
313                         if (!MonoIO.SetFileTime (path, -1,
314                                                  last_access_time.ToFileTime(), -1,
315                                                  out error)) {
316                                 throw MonoIO.GetException (path, error);
317                         }
318                 }
319
320                 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
321                 {
322                         SetLastAccessTime (path, last_access_time.ToLocalTime ());
323                 }
324
325                 public static void SetLastWriteTime (string path,
326                                                      DateTime last_write_time)
327                 {
328                         MonoIOError error;
329                         CheckPathExceptions (path);
330                         if (!MonoIO.Exists (path, out error))
331                                 throw MonoIO.GetException (path, error);
332
333                         if (!MonoIO.SetFileTime (path, -1, -1,
334                                                  last_write_time.ToFileTime(),
335                                                  out error)) {
336                                 throw MonoIO.GetException (path, error);
337                         }
338                 }
339
340                 public static void SetLastWriteTimeUtc (string path,
341                                                      DateTime last_write_time)
342                 {
343                         SetLastWriteTime (path, last_write_time.ToLocalTime ());
344                 }
345
346                 #region Private
347
348                 private static void CheckPathExceptions (string path)
349                 {
350                         if (path == null)
351                                 throw new System.ArgumentNullException("Path is Null");
352                         if (path == "")
353                                 throw new System.ArgumentException("Path is Empty");
354                         if (path.Trim().Length == 0)
355                                 throw new ArgumentException ("Only blank characters in path");
356                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
357                                 throw new ArgumentException ("Path contains invalid chars");
358                 }
359
360                 #endregion
361         }
362 }