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.
41 using System.Collections.Generic;
63 public static void AppendAllText (string path, string contents)
65 using (TextWriter w = new StreamWriter (path, true)) {
70 public static void AppendAllText (string path, string contents, Encoding encoding)
72 using (TextWriter w = new StreamWriter (path, true, encoding)) {
78 public static StreamWriter AppendText (string path)
80 return new StreamWriter (path, true);
83 [MonoTODO("Security Permision Checks")]
84 public static void Copy (string sourceFilename, string destFilename)
86 Copy (sourceFilename, destFilename, false);
89 public static void Copy (string src, string dest, bool overwrite)
92 throw new ArgumentNullException ("src");
94 throw new ArgumentNullException ("dest");
95 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
96 throw new ArgumentException (Locale.GetText ("src is null"));
97 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
98 throw new ArgumentException (Locale.GetText ("dest is empty or contains invalid characters"));
100 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
102 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
103 throw new ArgumentException(Locale.GetText ("{0} is a directory", src));
107 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
108 throw new ArgumentException (Locale.GetText ("{0} is a directory", dest));
111 throw new IOException (Locale.GetText ("{0} already exists", dest));
114 string DirName = Path.GetDirectoryName(dest);
115 if (DirName != String.Empty && !Directory.Exists (DirName))
116 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}",DirName));
120 if (!MonoIO.CopyFile (src, dest, overwrite, out error)){
121 string p = Locale.GetText ("{0}\" or \"{1}", src, dest);
122 throw MonoIO.GetException (p, error);
126 public static FileStream Create (string path)
128 return Create (path, 8192);
131 public static FileStream Create (string path, int buffersize)
134 throw new ArgumentNullException ("path");
135 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
136 throw new ArgumentException (Locale.GetText ("path is invalid"));
138 string DirName = Path.GetDirectoryName(path);
139 if (DirName != String.Empty && !Directory.Exists (DirName))
140 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
142 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
143 throw new UnauthorizedAccessException (Locale.GetText ("{0} is read-only", path));
147 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
148 FileShare.None, buffersize);
151 public static StreamWriter CreateText(string path)
154 return new StreamWriter (path, false);
160 public static void Delete (string path)
163 throw new ArgumentNullException("path");
164 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
165 throw new ArgumentException("path");
166 if (Directory.Exists (path))
167 throw new UnauthorizedAccessException(Locale.GetText ("{0} is a directory", path));
169 string DirName = Path.GetDirectoryName(path);
170 if (DirName != String.Empty && !Directory.Exists (DirName))
171 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
175 if (!MonoIO.DeleteFile (path, out error)){
176 if (error != MonoIOError.ERROR_FILE_NOT_FOUND)
177 throw MonoIO.GetException (path, error);
181 public static bool Exists (string path)
183 // For security reasons no exceptions are
184 // thrown, only false is returned if there is
185 // any problem with the path or permissions.
186 // Minimizes what information can be
187 // discovered by using this method.
188 if (null == path || String.Empty == path.Trim()
189 || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
194 return MonoIO.ExistsFile (path, out error);
197 public static FileAttributes GetAttributes (string path)
200 throw new ArgumentNullException("path");
203 if (String.Empty == path.Trim()) {
204 throw new ArgumentException (Locale.GetText ("Path is empty"));
207 if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
208 throw new ArgumentException(Locale.GetText ("Path contains invalid chars"));
212 FileAttributes attrs;
214 attrs = MonoIO.GetFileAttributes (path, out error);
215 if (error != MonoIOError.ERROR_SUCCESS) {
216 throw MonoIO.GetException (path, error);
222 public static DateTime GetCreationTime (string path)
226 CheckPathExceptions (path);
228 if (!MonoIO.GetFileStat (path, out stat, out error)) {
230 if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
231 return _defaultLocalFileTime;
233 throw new IOException (path);
235 throw new IOException (path);
238 return DateTime.FromFileTime (stat.CreationTime);
241 public static DateTime GetCreationTimeUtc (string path)
243 return GetCreationTime (path).ToUniversalTime ();
246 public static DateTime GetLastAccessTime (string path)
250 CheckPathExceptions (path);
252 if (!MonoIO.GetFileStat (path, out stat, out error)) {
254 if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
255 return _defaultLocalFileTime;
257 throw new IOException (path);
259 throw new IOException (path);
262 return DateTime.FromFileTime (stat.LastAccessTime);
265 public static DateTime GetLastAccessTimeUtc (string path)
267 return GetLastAccessTime (path).ToUniversalTime ();
270 public static DateTime GetLastWriteTime (string path)
274 CheckPathExceptions (path);
276 if (!MonoIO.GetFileStat (path, out stat, out error)) {
278 if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND)
279 return _defaultLocalFileTime;
281 throw new IOException (path);
283 throw new IOException (path);
286 return DateTime.FromFileTime (stat.LastWriteTime);
289 public static DateTime GetLastWriteTimeUtc (string path)
291 return GetLastWriteTime (path).ToUniversalTime ();
294 public static void Move (string src, string dest)
299 throw new ArgumentNullException ("src");
301 throw new ArgumentNullException ("dest");
302 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
303 throw new ArgumentException ("src");
304 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
305 throw new ArgumentException ("dest");
306 if (!MonoIO.Exists (src, out error))
307 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
308 if (MonoIO.ExistsDirectory (dest, out error))
309 throw new IOException (Locale.GetText ("{0} is a directory", dest));
311 // Don't check for this error here to allow the runtime to check if src and dest
312 // are equal. Comparing src and dest is not enough.
313 //if (MonoIO.Exists (dest, out error))
314 // throw new IOException (Locale.GetText ("{0} already exists", dest));
317 DirName = Path.GetDirectoryName(src);
318 if (DirName != String.Empty && !Directory.Exists (DirName))
319 throw new DirectoryNotFoundException(Locale.GetText ("Source directory not found: {0}", DirName));
320 DirName = Path.GetDirectoryName(dest);
321 if (DirName != String.Empty && !Directory.Exists (DirName))
322 throw new DirectoryNotFoundException(Locale.GetText ("Destination directory not found: {0}", DirName));
324 if (!MonoIO.MoveFile (src, dest, out error)) {
325 if (error == MonoIOError.ERROR_ALREADY_EXISTS)
326 throw MonoIO.GetException (dest, error);
327 throw MonoIO.GetException (error);
331 public static FileStream Open (string path, FileMode mode)
333 return new FileStream (path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None);
336 public static FileStream Open (string path, FileMode mode, FileAccess access)
338 return new FileStream (path, mode, access, FileShare.None);
341 public static FileStream Open (string path, FileMode mode, FileAccess access,
344 return new FileStream (path, mode, access, share);
347 public static FileStream OpenRead (string path)
349 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
352 public static StreamReader OpenText (string path)
354 return new StreamReader (path);
357 public static FileStream OpenWrite (string path)
359 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
362 public static void SetAttributes (string path,
363 FileAttributes attributes)
366 CheckPathExceptions (path);
368 if (!MonoIO.SetFileAttributes (path, attributes,
370 throw MonoIO.GetException (path, error);
374 public static void SetCreationTime (string path,
375 DateTime creation_time)
378 CheckPathExceptions (path);
379 if (!MonoIO.Exists (path, out error))
380 throw MonoIO.GetException (path, error);
382 if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
383 throw MonoIO.GetException (path, error);
387 public static void SetCreationTimeUtc (string path,
388 DateTime creation_time)
390 SetCreationTime (path, creation_time.ToLocalTime ());
393 public static void SetLastAccessTime (string path,DateTime last_access_time)
396 CheckPathExceptions (path);
397 if (!MonoIO.Exists (path, out error))
398 throw MonoIO.GetException (path, error);
400 if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
401 throw MonoIO.GetException (path, error);
405 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
407 SetLastAccessTime (path, last_access_time.ToLocalTime ());
410 public static void SetLastWriteTime (string path,
411 DateTime last_write_time)
414 CheckPathExceptions (path);
415 if (!MonoIO.Exists (path, out error))
416 throw MonoIO.GetException (path, error);
418 if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
419 throw MonoIO.GetException (path, error);
423 public static void SetLastWriteTimeUtc (string path,
424 DateTime last_write_time)
426 SetLastWriteTime (path, last_write_time.ToLocalTime ());
431 private static void CheckPathExceptions (string path)
434 throw new System.ArgumentNullException("path");
436 throw new System.ArgumentException(Locale.GetText ("Path is empty"));
437 if (path.Trim().Length == 0)
438 throw new ArgumentException (Locale.GetText ("Path is empty"));
439 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
440 throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
447 _defaultLocalFileTime = new DateTime (1601, 1, 1);
448 _defaultLocalFileTime = _defaultLocalFileTime.ToLocalTime ();
452 // The documentation for this method is most likely wrong, it
453 // talks about doing a "binary read", but the remarks say
454 // that this "detects the encoding".
456 // This can not detect and do anything useful with the encoding
457 // since the result is a byte [] not a char [].
459 public static byte [] ReadAllBytes (string path)
461 using (FileStream s = Open (path, FileMode.Open, FileAccess.Read, FileShare.Read)){
462 long size = s.Length;
465 // Is this worth supporting?
467 if (size > Int32.MaxValue)
468 throw new ArgumentException ("Reading more than 4gigs with this call is not supported");
470 byte [] result = new byte [s.Length];
472 s.Read (result, 0, (int) size);
479 public static string [] ReadAllLines (string path)
481 using (StreamReader reader = File.OpenText (path)) {
482 return ReadAllLines (reader);
486 public static string [] ReadAllLines (string path, Encoding encoding)
488 using (StreamReader reader = new StreamReader (path, encoding)) {
489 return ReadAllLines (reader);
493 static string [] ReadAllLines (StreamReader reader)
495 List<string> list = new List<string> ();
496 while (!reader.EndOfStream)
497 list.Add (reader.ReadLine ());
498 return list.ToArray ();
502 public static string ReadAllText (string path)
504 return ReadAllText (path, Encoding.UTF8Unmarked);
507 public static string ReadAllText (string path, Encoding enc)
509 using (StreamReader sr = new StreamReader (path, enc)) {
510 return sr.ReadToEnd ();
515 public static void WriteAllBytes (string path, byte [] data)
517 using (Stream stream = File.Create (path)) {
518 stream.Write (data, 0, data.Length);
522 public static void WriteAllLines (string path, string [] lines)
524 using (StreamWriter writer = new StreamWriter (path)) {
525 WriteAllLines (writer, lines);
529 public static void WriteAllLines (string path, string [] lines, Encoding encoding)
531 using (StreamWriter writer = new StreamWriter (path, false, encoding)) {
532 WriteAllLines (writer, lines);
536 static void WriteAllLines (StreamWriter writer, string [] lines)
538 foreach (string line in lines)
539 writer.WriteLine (line);
543 public static void WriteAllText (string path, string contents)
545 WriteAllText (path, contents, Encoding.UTF8Unmarked);
548 public static void WriteAllText (string path, string contents, Encoding enc)
550 using (StreamWriter sw = new StreamWriter (path, false, enc)) {
555 private static readonly DateTime _defaultLocalFileTime;