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.
58 public static StreamWriter AppendText (string path)
60 return new StreamWriter (path, true);
63 [MonoTODO("Security Permision Checks")]
64 public static void Copy (string sourceFilename, string destFilename)
66 Copy (sourceFilename, destFilename, false);
69 public static void Copy (string src, string dest, bool overwrite)
72 throw new ArgumentNullException ("src");
74 throw new ArgumentNullException ("dest");
75 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
76 throw new ArgumentException ("src");
77 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
78 throw new ArgumentException ("dest");
80 throw new FileNotFoundException (src + " does not exist", src);
82 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
83 throw new ArgumentException(src + " is a directory");
87 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
88 throw new ArgumentException(dest + " is a directory");
91 throw new IOException (dest + " already exists");
94 string DirName = Path.GetDirectoryName(dest);
95 if (DirName != String.Empty && !Directory.Exists (DirName))
96 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
100 if (!MonoIO.CopyFile (src, dest, overwrite, out error))
101 throw MonoIO.GetException (error);
104 public static FileStream Create (string path)
106 return Create (path, 8192);
109 public static FileStream Create (string path, int buffersize)
112 throw new ArgumentNullException("path");
113 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
114 throw new ArgumentException("path");
116 string DirName = Path.GetDirectoryName(path);
117 if (DirName != String.Empty && !Directory.Exists (DirName))
118 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
120 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
121 throw new UnauthorizedAccessException(path + " is a read-only");
125 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
126 FileShare.None, buffersize);
129 public static StreamWriter CreateText(string path)
132 return new StreamWriter (path, false);
138 public static void Delete (string path)
141 throw new ArgumentNullException("path");
142 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
143 throw new ArgumentException("path");
144 if (Directory.Exists (path))
145 throw new UnauthorizedAccessException("path is a directory");
147 string DirName = Path.GetDirectoryName(path);
148 if (DirName != String.Empty && !Directory.Exists (DirName))
149 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
153 if (!MonoIO.DeleteFile (path, out error)){
154 Exception e = MonoIO.GetException (path, error);
155 if (! (e is FileNotFoundException))
160 public static bool Exists (string path)
162 // For security reasons no exceptions are
163 // thrown, only false is returned if there is
164 // any problem with the path or permissions.
165 // Minimizes what information can be
166 // discovered by using this method.
167 if (null == path || String.Empty == path.Trim()
168 || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
175 exists = MonoIO.ExistsFile (path, out error);
176 if (error != MonoIOError.ERROR_SUCCESS &&
177 error != MonoIOError.ERROR_FILE_NOT_FOUND &&
178 error != MonoIOError.ERROR_PATH_NOT_FOUND) {
179 throw MonoIO.GetException (path, error);
185 public static FileAttributes GetAttributes (string path)
188 throw new ArgumentNullException("path");
191 if (String.Empty == path.Trim()) {
192 throw new ArgumentException("Path is empty");
195 if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
196 throw new ArgumentException("Path contains invalid chars");
200 FileAttributes attrs;
202 attrs = MonoIO.GetFileAttributes (path, out error);
203 if (error != MonoIOError.ERROR_SUCCESS) {
204 throw MonoIO.GetException (path, error);
210 public static DateTime GetCreationTime (string path)
214 CheckPathExceptions (path);
216 if (!MonoIO.GetFileStat (path, out stat, out error))
217 throw new IOException (path);
218 return DateTime.FromFileTime (stat.CreationTime);
221 public static DateTime GetCreationTimeUtc (string path)
223 return GetCreationTime (path).ToUniversalTime ();
226 public static DateTime GetLastAccessTime (string path)
230 CheckPathExceptions (path);
232 if (!MonoIO.GetFileStat (path, out stat, out error))
233 throw new IOException (path);
234 return DateTime.FromFileTime (stat.LastAccessTime);
237 public static DateTime GetLastAccessTimeUtc (string path)
239 return GetLastAccessTime (path).ToUniversalTime ();
242 public static DateTime GetLastWriteTime (string path)
246 CheckPathExceptions (path);
248 if (!MonoIO.GetFileStat (path, out stat, out error))
249 throw new IOException (path);
250 return DateTime.FromFileTime (stat.LastWriteTime);
253 public static DateTime GetLastWriteTimeUtc (string path)
255 return GetLastWriteTime (path).ToUniversalTime ();
258 public static void Move (string src, string dest)
263 throw new ArgumentNullException ("src");
265 throw new ArgumentNullException ("dest");
266 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
267 throw new ArgumentException ("src");
268 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
269 throw new ArgumentException ("dest");
270 if (!MonoIO.Exists (src, out error))
271 throw new FileNotFoundException (src + " does not exist", src);
272 if (MonoIO.ExistsDirectory (dest, out error))
273 throw new IOException (dest + " is a directory");
274 if (MonoIO.Exists (dest, out error))
275 throw new IOException (dest + " already exists");
278 DirName = Path.GetDirectoryName(src);
279 if (DirName != String.Empty && !Directory.Exists (DirName))
280 throw new DirectoryNotFoundException("Source directory not found: " + DirName);
281 DirName = Path.GetDirectoryName(dest);
282 if (DirName != String.Empty && !Directory.Exists (DirName))
283 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
285 if (!MonoIO.MoveFile (src, dest, out error))
286 throw MonoIO.GetException (error);
289 public static FileStream Open (string path, FileMode mode)
291 return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
294 public static FileStream Open (string path, FileMode mode, FileAccess access)
296 return new FileStream (path, mode, access, FileShare.None);
299 public static FileStream Open (string path, FileMode mode, FileAccess access,
302 return new FileStream (path, mode, access, share);
305 public static FileStream OpenRead (string path)
307 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
310 public static StreamReader OpenText (string path)
312 return new StreamReader (path);
315 public static FileStream OpenWrite (string path)
317 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
320 public static void SetAttributes (string path,
321 FileAttributes attributes)
324 CheckPathExceptions (path);
326 if (!MonoIO.SetFileAttributes (path, attributes,
328 throw MonoIO.GetException (path, error);
332 public static void SetCreationTime (string path,
333 DateTime creation_time)
336 CheckPathExceptions (path);
337 if (!MonoIO.Exists (path, out error))
338 throw MonoIO.GetException (path, error);
340 if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
341 throw MonoIO.GetException (path, error);
345 public static void SetCreationTimeUtc (string path,
346 DateTime creation_time)
348 SetCreationTime (path, creation_time.ToLocalTime ());
351 public static void SetLastAccessTime (string path,DateTime last_access_time)
354 CheckPathExceptions (path);
355 if (!MonoIO.Exists (path, out error))
356 throw MonoIO.GetException (path, error);
358 if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
359 throw MonoIO.GetException (path, error);
363 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
365 SetLastAccessTime (path, last_access_time.ToLocalTime ());
368 public static void SetLastWriteTime (string path,
369 DateTime last_write_time)
372 CheckPathExceptions (path);
373 if (!MonoIO.Exists (path, out error))
374 throw MonoIO.GetException (path, error);
376 if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
377 throw MonoIO.GetException (path, error);
381 public static void SetLastWriteTimeUtc (string path,
382 DateTime last_write_time)
384 SetLastWriteTime (path, last_write_time.ToLocalTime ());
389 private static void CheckPathExceptions (string path)
392 throw new System.ArgumentNullException("Path is Null");
394 throw new System.ArgumentException("Path is Empty");
395 if (path.Trim().Length == 0)
396 throw new ArgumentException ("Only blank characters in path");
397 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
398 throw new ArgumentException ("Path contains invalid chars");