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 (Locale.GetText ("src is null"));
77 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
78 throw new ArgumentException (Locale.GetText ("dest is empty or contains invalid characters"));
80 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
82 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
83 throw new ArgumentException(Locale.GetText ("{0} is a directory", src));
87 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
88 throw new ArgumentException (Locale.GetText ("{0} is a directory", dest));
91 throw new IOException (Locale.GetText ("{0} already exists", dest));
94 string DirName = Path.GetDirectoryName(dest);
95 if (DirName != String.Empty && !Directory.Exists (DirName))
96 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}",DirName));
100 if (!MonoIO.CopyFile (src, dest, overwrite, out error)){
101 string p = Locale.GetText ("{0}\" or \"{1}", src, dest);
102 throw MonoIO.GetException (p, error);
106 public static FileStream Create (string path)
108 return Create (path, 8192);
111 public static FileStream Create (string path, int buffersize)
114 throw new ArgumentNullException ("path");
115 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
116 throw new ArgumentException (Locale.GetText ("path is invalid"));
118 string DirName = Path.GetDirectoryName(path);
119 if (DirName != String.Empty && !Directory.Exists (DirName))
120 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
122 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
123 throw new UnauthorizedAccessException (Locale.GetText ("{0} is read-only", path));
127 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
128 FileShare.None, buffersize);
131 public static StreamWriter CreateText(string path)
134 return new StreamWriter (path, false);
140 public static void Delete (string path)
143 throw new ArgumentNullException("path");
144 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
145 throw new ArgumentException("path");
146 if (Directory.Exists (path))
147 throw new UnauthorizedAccessException(Locale.GetText ("{0} is a directory", path));
149 string DirName = Path.GetDirectoryName(path);
150 if (DirName != String.Empty && !Directory.Exists (DirName))
151 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
155 if (!MonoIO.DeleteFile (path, out error)){
156 Exception e = MonoIO.GetException (path, error);
157 if (! (e is FileNotFoundException))
162 public static bool Exists (string path)
164 // For security reasons no exceptions are
165 // thrown, only false is returned if there is
166 // any problem with the path or permissions.
167 // Minimizes what information can be
168 // discovered by using this method.
169 if (null == path || String.Empty == path.Trim()
170 || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
177 exists = MonoIO.ExistsFile (path, out error);
178 if (error != MonoIOError.ERROR_SUCCESS &&
179 error != MonoIOError.ERROR_FILE_NOT_FOUND &&
180 error != MonoIOError.ERROR_PATH_NOT_FOUND &&
181 error != MonoIOError.ERROR_INVALID_NAME) {
182 throw MonoIO.GetException (path, error);
188 public static FileAttributes GetAttributes (string path)
191 throw new ArgumentNullException("path");
194 if (String.Empty == path.Trim()) {
195 throw new ArgumentException (Locale.GetText ("Path is empty"));
198 if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
199 throw new ArgumentException(Locale.GetText ("Path contains invalid chars"));
203 FileAttributes attrs;
205 attrs = MonoIO.GetFileAttributes (path, out error);
206 if (error != MonoIOError.ERROR_SUCCESS) {
207 throw MonoIO.GetException (path, error);
213 public static DateTime GetCreationTime (string path)
217 CheckPathExceptions (path);
219 if (!MonoIO.GetFileStat (path, out stat, out error))
220 throw new IOException (path);
221 return DateTime.FromFileTime (stat.CreationTime);
224 public static DateTime GetCreationTimeUtc (string path)
226 return GetCreationTime (path).ToUniversalTime ();
229 public static DateTime GetLastAccessTime (string path)
233 CheckPathExceptions (path);
235 if (!MonoIO.GetFileStat (path, out stat, out error))
236 throw new IOException (path);
237 return DateTime.FromFileTime (stat.LastAccessTime);
240 public static DateTime GetLastAccessTimeUtc (string path)
242 return GetLastAccessTime (path).ToUniversalTime ();
245 public static DateTime GetLastWriteTime (string path)
249 CheckPathExceptions (path);
251 if (!MonoIO.GetFileStat (path, out stat, out error))
252 throw new IOException (path);
253 return DateTime.FromFileTime (stat.LastWriteTime);
256 public static DateTime GetLastWriteTimeUtc (string path)
258 return GetLastWriteTime (path).ToUniversalTime ();
261 public static void Move (string src, string dest)
266 throw new ArgumentNullException ("src");
268 throw new ArgumentNullException ("dest");
269 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
270 throw new ArgumentException ("src");
271 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
272 throw new ArgumentException ("dest");
273 if (!MonoIO.Exists (src, out error))
274 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
275 if (MonoIO.ExistsDirectory (dest, out error))
276 throw new IOException (Locale.GetText ("{0} is a directory", dest));
278 // Don't check for this error here to allow the runtime to check if src and dest
279 // are equal. Comparing src and dest is not enough.
280 //if (MonoIO.Exists (dest, out error))
281 // throw new IOException (Locale.GetText ("{0} already exists", dest));
284 DirName = Path.GetDirectoryName(src);
285 if (DirName != String.Empty && !Directory.Exists (DirName))
286 throw new DirectoryNotFoundException(Locale.GetText ("Source directory not found: {0}", DirName));
287 DirName = Path.GetDirectoryName(dest);
288 if (DirName != String.Empty && !Directory.Exists (DirName))
289 throw new DirectoryNotFoundException(Locale.GetText ("Destination directory not found: {0}", DirName));
291 if (!MonoIO.MoveFile (src, dest, out error)) {
292 if (error == MonoIOError.ERROR_ALREADY_EXISTS)
293 throw MonoIO.GetException (dest, error);
294 throw MonoIO.GetException (error);
298 public static FileStream Open (string path, FileMode mode)
300 return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
303 public static FileStream Open (string path, FileMode mode, FileAccess access)
305 return new FileStream (path, mode, access, FileShare.None);
308 public static FileStream Open (string path, FileMode mode, FileAccess access,
311 return new FileStream (path, mode, access, share);
314 public static FileStream OpenRead (string path)
316 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
319 public static StreamReader OpenText (string path)
321 return new StreamReader (path);
324 public static FileStream OpenWrite (string path)
326 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
329 public static void SetAttributes (string path,
330 FileAttributes attributes)
333 CheckPathExceptions (path);
335 if (!MonoIO.SetFileAttributes (path, attributes,
337 throw MonoIO.GetException (path, error);
341 public static void SetCreationTime (string path,
342 DateTime creation_time)
345 CheckPathExceptions (path);
346 if (!MonoIO.Exists (path, out error))
347 throw MonoIO.GetException (path, error);
349 if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
350 throw MonoIO.GetException (path, error);
354 public static void SetCreationTimeUtc (string path,
355 DateTime creation_time)
357 SetCreationTime (path, creation_time.ToLocalTime ());
360 public static void SetLastAccessTime (string path,DateTime last_access_time)
363 CheckPathExceptions (path);
364 if (!MonoIO.Exists (path, out error))
365 throw MonoIO.GetException (path, error);
367 if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
368 throw MonoIO.GetException (path, error);
372 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
374 SetLastAccessTime (path, last_access_time.ToLocalTime ());
377 public static void SetLastWriteTime (string path,
378 DateTime last_write_time)
381 CheckPathExceptions (path);
382 if (!MonoIO.Exists (path, out error))
383 throw MonoIO.GetException (path, error);
385 if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
386 throw MonoIO.GetException (path, error);
390 public static void SetLastWriteTimeUtc (string path,
391 DateTime last_write_time)
393 SetLastWriteTime (path, last_write_time.ToLocalTime ());
398 private static void CheckPathExceptions (string path)
401 throw new System.ArgumentNullException("path");
403 throw new System.ArgumentException(Locale.GetText ("Path is empty"));
404 if (path.Trim().Length == 0)
405 throw new ArgumentException (Locale.GetText ("Path is empty"));
406 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
407 throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));