6 // Miguel de Icaza (miguel@ximian.com)
7 // Jim Richardson (develop@wtfo-guru.com)
8 // Dan Lewis (dihlewis@yahoo.co.uk)
9 // Ville Palo (vi64pa@kolumbus.fi)
11 // Copyright 2002 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 public sealed class File
51 public static StreamWriter AppendText (string path)
53 return new StreamWriter (path, true);
56 [MonoTODO("Security Permision Checks")]
57 public static void Copy (string sourceFilename, string destFilename)
59 Copy (sourceFilename, destFilename, false);
62 public static void Copy (string src, string dest, bool overwrite)
65 throw new ArgumentNullException ("src");
67 throw new ArgumentNullException ("dest");
68 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
69 throw new ArgumentException ("src");
70 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
71 throw new ArgumentException ("dest");
73 throw new FileNotFoundException (src + " does not exist");
75 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
76 throw new ArgumentException(src + " is a directory");
80 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
81 throw new ArgumentException(dest + " is a directory");
84 throw new IOException (dest + " already exists");
87 string DirName = Path.GetDirectoryName(dest);
88 if (DirName != String.Empty && !Directory.Exists (DirName))
89 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
93 if (!MonoIO.CopyFile (src, dest, overwrite, out error))
94 throw MonoIO.GetException (error);
97 public static FileStream Create (string path)
99 return Create (path, 8192);
102 public static FileStream Create (string path, int buffersize)
105 throw new ArgumentNullException("path");
106 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
107 throw new ArgumentException("path");
109 string DirName = Path.GetDirectoryName(path);
110 if (DirName != String.Empty && !Directory.Exists (DirName))
111 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
113 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
114 throw new UnauthorizedAccessException(path + " is a read-only");
118 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
119 FileShare.None, buffersize);
122 public static StreamWriter CreateText(string path)
125 return new StreamWriter (path, false);
131 public static void Delete (string path)
134 throw new ArgumentNullException("path");
135 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
136 throw new ArgumentException("path");
137 if (Directory.Exists (path))
138 throw new UnauthorizedAccessException("path is a directory");
140 string DirName = Path.GetDirectoryName(path);
141 if (DirName != String.Empty && !Directory.Exists (DirName))
142 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
146 if (!MonoIO.DeleteFile (path, out error)){
147 Exception e = MonoIO.GetException (path, error);
148 if (! (e is FileNotFoundException))
153 public static bool Exists (string path)
155 // For security reasons no exceptions are
156 // thrown, only false is returned if there is
157 // any problem with the path or permissions.
158 // Minimizes what information can be
159 // discovered by using this method.
160 if (null == path || String.Empty == path.Trim()
161 || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
168 exists = MonoIO.ExistsFile (path, out error);
169 if (error != MonoIOError.ERROR_SUCCESS &&
170 error != MonoIOError.ERROR_FILE_NOT_FOUND &&
171 error != MonoIOError.ERROR_PATH_NOT_FOUND) {
172 throw MonoIO.GetException (path, error);
178 public static FileAttributes GetAttributes (string path)
181 throw new ArgumentNullException("path");
184 if (String.Empty == path.Trim()) {
185 throw new ArgumentException("Path is empty");
188 if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
189 throw new ArgumentException("Path contains invalid chars");
193 FileAttributes attrs;
195 attrs = MonoIO.GetFileAttributes (path, out error);
196 if (error != MonoIOError.ERROR_SUCCESS) {
197 throw MonoIO.GetException (path, error);
203 public static DateTime GetCreationTime (string path)
207 CheckPathExceptions (path);
209 if (!MonoIO.GetFileStat (path, out stat, out error))
210 throw new IOException (path);
211 return DateTime.FromFileTime (stat.CreationTime);
214 public static DateTime GetCreationTimeUtc (string path)
216 return GetCreationTime (path).ToUniversalTime ();
219 public static DateTime GetLastAccessTime (string path)
223 CheckPathExceptions (path);
225 if (!MonoIO.GetFileStat (path, out stat, out error))
226 throw new IOException (path);
227 return DateTime.FromFileTime (stat.LastAccessTime);
230 public static DateTime GetLastAccessTimeUtc (string path)
232 return GetLastAccessTime (path).ToUniversalTime ();
235 public static DateTime GetLastWriteTime (string path)
239 CheckPathExceptions (path);
241 if (!MonoIO.GetFileStat (path, out stat, out error))
242 throw new IOException (path);
243 return DateTime.FromFileTime (stat.LastWriteTime);
246 public static DateTime GetLastWriteTimeUtc (string path)
248 return GetLastWriteTime (path).ToUniversalTime ();
251 public static void Move (string src, string dest)
256 throw new ArgumentNullException ("src");
258 throw new ArgumentNullException ("dest");
259 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
260 throw new ArgumentException ("src");
261 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
262 throw new ArgumentException ("dest");
263 if (!MonoIO.Exists (src, out error))
264 throw new FileNotFoundException (src + " does not exist");
265 if (MonoIO.ExistsDirectory (dest, out error))
266 throw new IOException (dest + " is a directory");
267 if (MonoIO.Exists (dest, out error))
268 throw new IOException (dest + " already exists");
271 DirName = Path.GetDirectoryName(src);
272 if (DirName != String.Empty && !Directory.Exists (DirName))
273 throw new DirectoryNotFoundException("Source directory not found: " + DirName);
274 DirName = Path.GetDirectoryName(dest);
275 if (DirName != String.Empty && !Directory.Exists (DirName))
276 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
278 if (!MonoIO.MoveFile (src, dest, out error))
279 throw MonoIO.GetException (error);
282 public static FileStream Open (string path, FileMode mode)
284 return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
287 public static FileStream Open (string path, FileMode mode, FileAccess access)
289 return new FileStream (path, mode, access, FileShare.None);
292 public static FileStream Open (string path, FileMode mode, FileAccess access,
295 return new FileStream (path, mode, access, share);
298 public static FileStream OpenRead (string path)
300 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
303 public static StreamReader OpenText (string path)
305 return new StreamReader (path);
308 public static FileStream OpenWrite (string path)
310 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
313 public static void SetAttributes (string path,
314 FileAttributes attributes)
317 CheckPathExceptions (path);
319 if (!MonoIO.SetFileAttributes (path, attributes,
321 throw MonoIO.GetException (path, error);
325 public static void SetCreationTime (string path,
326 DateTime creation_time)
329 CheckPathExceptions (path);
330 if (!MonoIO.Exists (path, out error))
331 throw MonoIO.GetException (path, error);
333 if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
334 throw MonoIO.GetException (path, error);
338 public static void SetCreationTimeUtc (string path,
339 DateTime creation_time)
341 SetCreationTime (path, creation_time.ToLocalTime ());
344 public static void SetLastAccessTime (string path,DateTime last_access_time)
347 CheckPathExceptions (path);
348 if (!MonoIO.Exists (path, out error))
349 throw MonoIO.GetException (path, error);
351 if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
352 throw MonoIO.GetException (path, error);
356 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
358 SetLastAccessTime (path, last_access_time.ToLocalTime ());
361 public static void SetLastWriteTime (string path,
362 DateTime last_write_time)
365 CheckPathExceptions (path);
366 if (!MonoIO.Exists (path, out error))
367 throw MonoIO.GetException (path, error);
369 if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
370 throw MonoIO.GetException (path, error);
374 public static void SetLastWriteTimeUtc (string path,
375 DateTime last_write_time)
377 SetLastWriteTime (path, last_write_time.ToLocalTime ());
382 private static void CheckPathExceptions (string path)
385 throw new System.ArgumentNullException("Path is Null");
387 throw new System.ArgumentException("Path is Empty");
388 if (path.Trim().Length == 0)
389 throw new ArgumentException ("Only blank characters in path");
390 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
391 throw new ArgumentException ("Path contains invalid chars");