2 // System.IO.FIle.cs
\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)
11 // Copyright 2002 Ximian, Inc. http://www.ximian.com
\r
12 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
\r
22 public sealed class File
\r
28 public static StreamWriter AppendText (string path)
\r
30 return new StreamWriter (path, true);
\r
33 [MonoTODO("Security Permision Checks")]
\r
34 public static void Copy (string sourceFilename, string destFilename)
\r
36 Copy (sourceFilename, destFilename, false);
\r
39 public static void Copy (string src, string dest, bool overwrite)
\r
42 throw new ArgumentNullException ("src");
\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
50 throw new FileNotFoundException (src + " does not exist");
\r
52 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
53 throw new ArgumentException(src + " is a directory");
56 if (Exists (dest)) {
\r
57 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
\r
58 throw new ArgumentException(dest + " is a directory");
\r
61 throw new IOException (dest + " already exists");
\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);
70 if (!MonoIO.CopyFile (src, dest, overwrite, out error))
71 throw MonoIO.GetException (error);
74 public static FileStream Create (string path)
\r
76 return Create (path, 8192);
\r
79 public static FileStream Create (string path, int buffersize)
\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
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
90 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
\r
91 throw new UnauthorizedAccessException(path + " is a read-only");
\r
95 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
\r
96 FileShare.None, buffersize);
\r
99 public static StreamWriter CreateText(string path)
102 return new StreamWriter (path, false);
108 public static void Delete (string 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
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
123 if (!MonoIO.DeleteFile (path, out error)){
124 Exception e = MonoIO.GetException (error);
125 if (! (e is FileNotFoundException))
130 public static bool Exists (string path)
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) {
144 return MonoIO.ExistsFile (path, out error);
147 public static FileAttributes GetAttributes (string path)
150 throw new ArgumentNullException("path");
153 if (String.Empty == path.Trim()) {
154 throw new ArgumentException("Path is empty");
157 if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
158 throw new ArgumentException("Path contains invalid chars");
163 return MonoIO.GetFileAttributes (path, out error);
166 public static DateTime GetCreationTime (string path)
170 CheckPathExceptions (path);
172 if (!MonoIO.GetFileStat (path, out stat, out error))
173 throw new IOException (path);
174 return DateTime.FromFileTime (stat.CreationTime);
177 public static DateTime GetCreationTimeUtc (string path)
179 return GetCreationTime (path).ToUniversalTime ();
182 public static DateTime GetLastAccessTime (string path)
186 CheckPathExceptions (path);
188 if (!MonoIO.GetFileStat (path, out stat, out error))
189 throw new IOException (path);
190 return DateTime.FromFileTime (stat.LastAccessTime);
193 public static DateTime GetLastAccessTimeUtc (string path)
195 return GetLastAccessTime (path).ToUniversalTime ();
198 public static DateTime GetLastWriteTime (string path)
202 CheckPathExceptions (path);
204 if (!MonoIO.GetFileStat (path, out stat, out error))
205 throw new IOException (path);
206 return DateTime.FromFileTime (stat.LastWriteTime);
209 public static DateTime GetLastWriteTimeUtc (string path)
211 return GetLastWriteTime (path).ToUniversalTime ();
214 public static void Move (string src, string dest)
\r
219 throw new ArgumentNullException ("src");
\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
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
239 if (!MonoIO.MoveFile (src, dest, out error))
240 throw MonoIO.GetException (error);
243 public static FileStream Open (string path, FileMode mode)
\r
245 return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
\r
248 public static FileStream Open (string path, FileMode mode, FileAccess access)
\r
250 return new FileStream (path, mode, access, FileShare.None);
\r
253 public static FileStream Open (string path, FileMode mode, FileAccess access,
\r
256 return new FileStream (path, mode, access, share);
\r
259 public static FileStream OpenRead (string path)
\r
261 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
264 public static StreamReader OpenText (string path)
\r
266 return new StreamReader (path);
\r
269 public static FileStream OpenWrite (string path)
\r
271 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
\r
274 public static void SetAttributes (string path,
275 FileAttributes attributes)
278 CheckPathExceptions (path);
280 if (!MonoIO.SetFileAttributes (path, attributes,
282 throw MonoIO.GetException (path, error);
286 public static void SetCreationTime (string path,
287 DateTime creation_time)
290 CheckPathExceptions (path);
291 if (!MonoIO.Exists (path, out error))
292 throw MonoIO.GetException (path, error);
294 if (!MonoIO.SetFileTime (path, creation_time.ToFileTime(),
295 -1, -1, out error)) {
296 throw MonoIO.GetException (path, error);
300 public static void SetCreationTimeUtc (string path,
301 DateTime creation_time)
303 SetCreationTime (path, creation_time.ToLocalTime ());
306 public static void SetLastAccessTime (string path,DateTime last_access_time)
309 CheckPathExceptions (path);
310 if (!MonoIO.Exists (path, out error))
311 throw MonoIO.GetException (path, error);
313 if (!MonoIO.SetFileTime (path, -1,
314 last_access_time.ToFileTime(), -1,
316 throw MonoIO.GetException (path, error);
320 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
322 SetLastAccessTime (path, last_access_time.ToLocalTime ());
325 public static void SetLastWriteTime (string path,
326 DateTime last_write_time)
329 CheckPathExceptions (path);
330 if (!MonoIO.Exists (path, out error))
331 throw MonoIO.GetException (path, error);
333 if (!MonoIO.SetFileTime (path, -1, -1,
334 last_write_time.ToFileTime(),
336 throw MonoIO.GetException (path, error);
340 public static void SetLastWriteTimeUtc (string path,
341 DateTime last_write_time)
343 SetLastWriteTime (path, last_write_time.ToLocalTime ());
348 private static void CheckPathExceptions (string path)
351 throw new System.ArgumentNullException("Path is Null");
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");