2 // FileTest.cs: Test cases for System.IO.File
5 // Duncan Mak (duncan@ximian.com)
6 // Ville Palo (vi64pa@kolumbus.fi)
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
10 // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
12 using NUnit.Framework;
15 using System.Globalization;
16 using System.Threading;
18 namespace MonoTests.System.IO
21 public class FileTest : Assertion
23 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
28 if (Directory.Exists (TempFolder))
29 Directory.Delete (TempFolder, true);
30 Directory.CreateDirectory (TempFolder);
32 Thread.CurrentThread.CurrentCulture = new CultureInfo ("EN-us");
36 public void TearDown ()
38 if (Directory.Exists (TempFolder))
39 Directory.Delete (TempFolder, true);
43 public void TestExists ()
47 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
49 Assert ("null filename should not exist", !File.Exists (null));
51 Assert ("empty filename should not exist", !File.Exists (""));
53 Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
56 s = File.Create (path);
58 Assert ("File " + path + " should exists", File.Exists (path));
60 Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"));
61 } catch (Exception e) {
62 Fail ("Unexpected exception at i = " + i + ". e=" + e);
71 public void Exists_InvalidFileName ()
73 Assert ("><|", !File.Exists ("><|"));
74 Assert ("?*", !File.Exists ("?*"));
78 public void Exists_InvalidDirectory ()
80 Assert ("InvalidDirectory", !File.Exists (Path.Combine ("does not exist", "file.txt")));
84 [ExpectedException(typeof (ArgumentNullException))]
85 public void CtorArgumentNullException1 ()
87 FileStream stream = File.Create (null);
91 [ExpectedException(typeof (ArgumentException))]
92 public void CtorArgumentException1 ()
94 FileStream stream = File.Create ("");
98 [ExpectedException(typeof (ArgumentException))]
99 public void CtorArgumentException2 ()
101 FileStream stream = File.Create (" ");
105 [ExpectedException(typeof (DirectoryNotFoundException))]
106 public void CtorDirectoryNotFoundException ()
108 FileStream stream = null;
109 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
112 stream = File.Create (path);
121 public void TestCreate ()
123 FileStream stream = null;
125 /* positive test: create resources/foo */
127 path = TempFolder + Path.DirectorySeparatorChar + "foo";
128 stream = File.Create (path);
129 Assert ("File should exist", File.Exists (path));
131 } catch (Exception e) {
132 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
142 /* positive test: repeat test above again to test for overwriting file */
144 path = TempFolder + Path.DirectorySeparatorChar + "foo";
145 stream = File.Create (path);
146 Assert ("File should exist", File.Exists (path));
148 } catch (Exception e) {
149 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
158 [ExpectedException(typeof(ArgumentNullException))]
159 public void CopyArgumentNullException1 ()
161 File.Copy (null, "b");
165 [ExpectedException(typeof(ArgumentNullException))]
166 public void CopyArgumentNullException2 ()
168 File.Copy ("a", null);
172 [ExpectedException(typeof(ArgumentException))]
173 public void CopyArgumentException1 ()
179 [ExpectedException(typeof(ArgumentException))]
180 public void CopyArgumentException2 ()
186 [ExpectedException(typeof(ArgumentException))]
187 public void CopyArgumentException3 ()
189 File.Copy (" ", "b");
193 [ExpectedException(typeof(ArgumentException))]
194 public void CopyArgumentException4 ()
196 File.Copy ("a", " ");
200 [ExpectedException(typeof(FileNotFoundException))]
201 public void CopyFileNotFoundException ()
203 File.Copy ("doesnotexist", "b");
206 [ExpectedException(typeof(IOException))]
207 public void CopyIOException ()
209 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
210 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
212 File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
213 File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
214 File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
216 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
217 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
222 public void TestCopy ()
224 string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
225 string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
226 /* positive test: copy resources/AFile.txt to resources/bar */
232 File.Create (path2).Close ();
233 File.Copy (path2, path1);
234 Assert ("File AFile.txt should still exist", File.Exists (path2));
235 Assert ("File bar should exist after File.Copy", File.Exists (path1));
236 } catch (Exception e) {
237 Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
240 /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
242 Assert ("File bar should exist before File.Copy", File.Exists (path1));
243 File.Copy (path2, path1, true);
244 Assert ("File AFile.txt should still exist", File.Exists (path2));
245 Assert ("File bar should exist after File.Copy", File.Exists (path1));
246 } catch (Exception e) {
247 Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
256 [ExpectedException (typeof (ArgumentNullException))]
257 public void DeleteArgumentNullException ()
263 [ExpectedException (typeof (ArgumentException))]
264 public void DeleteArgumentException1 ()
270 [ExpectedException (typeof (ArgumentException))]
271 public void DeleteArgumentException2 ()
277 [ExpectedException (typeof (DirectoryNotFoundException))]
278 public void DeleteDirectoryNotFoundException ()
280 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
281 if (Directory.Exists (path))
282 Directory.Delete (path, true);
288 public void TestDelete ()
290 string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
291 DeleteFile (foopath);
293 File.Create (foopath).Close ();
296 File.Delete (foopath);
297 } catch (Exception e) {
298 Fail ("Unable to delete " + foopath + " e=" + e.ToString());
300 Assert ("File " + foopath + " should not exist after File.Delete", !File.Exists (foopath));
302 DeleteFile (foopath);
307 [ExpectedException(typeof (IOException))]
308 [Category("NotWorking")]
309 public void DeleteOpenStreamException ()
311 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
313 FileStream stream = null;
315 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
325 [ExpectedException(typeof (ArgumentNullException))]
326 public void MoveException1 ()
328 File.Move (null, "b");
332 [ExpectedException(typeof (ArgumentNullException))]
333 public void MoveException2 ()
335 File.Move ("a", null);
339 [ExpectedException(typeof (ArgumentException))]
340 public void MoveException3 ()
346 [ExpectedException(typeof (ArgumentException))]
347 public void MoveException4 ()
353 [ExpectedException(typeof (ArgumentException))]
354 public void MoveException5 ()
356 File.Move (" ", "b");
360 [ExpectedException(typeof (ArgumentException))]
361 public void MoveException6 ()
363 File.Move ("a", " ");
367 [ExpectedException(typeof (FileNotFoundException))]
368 public void MoveException7 ()
370 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist");
371 File.Move (TempFolder + Path.DirectorySeparatorChar + "doesnotexist", "b");
375 [ExpectedException(typeof (DirectoryNotFoundException))]
376 public void MoveException8 ()
378 string path = TempFolder + Path.DirectorySeparatorChar + "foo";
381 File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
382 File.Copy(TempFolder + Path.DirectorySeparatorChar + "AFile.txt", path, true);
383 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
384 File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
386 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
392 [ExpectedException(typeof (IOException))]
393 public void MoveException9 ()
395 File.Create (TempFolder + Path.DirectorySeparatorChar + "foo").Close ();
397 File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder);
399 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "foo");
404 public void TestMove ()
406 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
407 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
408 if (!File.Exists (bar)) {
409 FileStream f = File.Create(bar);
413 Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should exist", File.Exists (bar));
414 File.Move (bar, baz);
415 Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists (bar));
416 Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "baz should exist", File.Exists (baz));
418 // Test moving of directories
419 string dir = Path.Combine (TempFolder, "dir");
420 string dir2 = Path.Combine (TempFolder, "dir2");
421 string dir_foo = Path.Combine (dir, "foo");
422 string dir2_foo = Path.Combine (dir2, "foo");
424 if (Directory.Exists (dir))
425 Directory.Delete (dir, true);
427 Directory.CreateDirectory (dir);
428 Directory.CreateDirectory (dir2);
429 File.Create (dir_foo).Close ();
430 File.Move (dir_foo, dir2_foo);
431 Assert (File.Exists (dir2_foo));
433 Directory.Delete (dir, true);
434 Directory.Delete (dir2, true);
435 DeleteFile (dir_foo);
436 DeleteFile (dir2_foo);
440 public void TestOpen ()
443 FileStream stream = null;
445 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
446 if (!File.Exists (path))
447 stream = File.Create (path);
449 stream = File.Open (path, FileMode.Open);
451 } catch (Exception e) {
452 Fail ("Unable to open " + TempFolder + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
461 /* Exception tests */
463 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
464 stream = File.Open (path, FileMode.Open);
465 Fail ("File 'filedoesnotexist' should not exist");
466 } catch (FileNotFoundException) {
467 // do nothing, this is what we expect
468 } catch (Exception e) {
469 Fail ("Unexpect exception caught: e=" + e.ToString());
480 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
481 if (!File.Exists (path))
482 File.Create (path).Close ();
483 FileStream stream = null;
486 stream = File.Open (path, FileMode.Open);
488 Assertion.AssertEquals ("test#01", true, stream.CanRead);
489 Assertion.AssertEquals ("test#02", true, stream.CanSeek);
490 Assertion.AssertEquals ("test#03", true, stream.CanWrite);
493 stream = File.Open (path, FileMode.Open, FileAccess.Write);
494 Assertion.AssertEquals ("test#04", false, stream.CanRead);
495 Assertion.AssertEquals ("test#05", true, stream.CanSeek);
496 Assertion.AssertEquals ("test#06", true, stream.CanWrite);
499 stream = File.Open (path, FileMode.Open, FileAccess.Read);
500 Assertion.AssertEquals ("test#04", true, stream.CanRead);
501 Assertion.AssertEquals ("test#05", true, stream.CanSeek);
502 Assertion.AssertEquals ("test#06", false, stream.CanWrite);
513 [ExpectedException(typeof(ArgumentException))]
514 public void OpenException1 ()
516 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
517 FileStream stream = null;
518 // CreateNew + Read throws an exceptoin
520 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
529 [ExpectedException(typeof(ArgumentException))]
530 public void OpenException2 ()
532 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
534 // Append + Read throws an exceptoin
535 if (!File.Exists (path))
536 File.Create (path).Close ();
538 s = File.Open (path, FileMode.Append, FileAccess.Read);
547 public void OpenRead ()
549 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
550 if (!File.Exists (path))
551 File.Create (path).Close ();
552 FileStream stream = null;
555 stream = File.OpenRead (path);
557 Assertion.AssertEquals ("test#01", true, stream.CanRead);
558 Assertion.AssertEquals ("test#02", true, stream.CanSeek);
559 Assertion.AssertEquals ("test#03", false, stream.CanWrite);
569 public void OpenWrite ()
571 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
572 if (!File.Exists (path))
573 File.Create (path).Close ();
574 FileStream stream = null;
577 stream = File.OpenWrite (path);
578 Assertion.AssertEquals ("test#01", false, stream.CanRead);
579 Assertion.AssertEquals ("test#02", true, stream.CanSeek);
580 Assertion.AssertEquals ("test#03", true, stream.CanWrite);
590 public void TestGetCreationTime ()
592 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
596 File.Create (path).Close();
597 DateTime time = File.GetCreationTime (path);
598 Assert ("GetCreationTime incorrect", (DateTime.Now - time).TotalSeconds < 10);
604 // Setting the creation time on Unix is not possible
606 public void CreationTime ()
608 int platform = (int) Environment.OSVersion.Platform;
609 if ((platform == 4) || (platform == 128))
612 string path = Path.GetTempFileName ();
615 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
616 DateTime time = File.GetCreationTime (path);
617 Assertion.AssertEquals ("test#01", 2002, time.Year);
618 Assertion.AssertEquals ("test#02", 4, time.Month);
619 Assertion.AssertEquals ("test#03", 6, time.Day);
620 Assertion.AssertEquals ("test#04", 4, time.Hour);
621 Assertion.AssertEquals ("test#05", 4, time.Second);
623 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
624 Assertion.AssertEquals ("test#06", 2002, time.Year);
625 Assertion.AssertEquals ("test#07", 4, time.Month);
626 Assertion.AssertEquals ("test#08", 6, time.Day);
627 Assertion.AssertEquals ("test#09", 4, time.Hour);
628 Assertion.AssertEquals ("test#10", 4, time.Second);
630 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
631 time = File.GetCreationTimeUtc (path);
632 Assertion.AssertEquals ("test#11", 2002, time.Year);
633 Assertion.AssertEquals ("test#12", 4, time.Month);
634 Assertion.AssertEquals ("test#13", 6, time.Day);
635 Assertion.AssertEquals ("test#14", 4, time.Hour);
636 Assertion.AssertEquals ("test#15", 4, time.Second);
638 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
639 Assertion.AssertEquals ("test#16", 2002, time.Year);
640 Assertion.AssertEquals ("test#17", 4, time.Month);
641 Assertion.AssertEquals ("test#18", 6, time.Day);
642 Assertion.AssertEquals ("test#19", 4, time.Hour);
643 Assertion.AssertEquals ("test#20", 4, time.Second);
650 public void LastAccessTime ()
652 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
653 if (File.Exists (path))
655 FileStream stream = null;
657 stream = File.Create (path);
660 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
661 DateTime time = File.GetLastAccessTime (path);
662 Assertion.AssertEquals ("test#01", 2002, time.Year);
663 Assertion.AssertEquals ("test#02", 4, time.Month);
664 Assertion.AssertEquals ("test#03", 6, time.Day);
665 Assertion.AssertEquals ("test#04", 4, time.Hour);
666 Assertion.AssertEquals ("test#05", 4, time.Second);
668 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
669 Assertion.AssertEquals ("test#06", 2002, time.Year);
670 Assertion.AssertEquals ("test#07", 4, time.Month);
671 Assertion.AssertEquals ("test#08", 6, time.Day);
672 Assertion.AssertEquals ("test#09", 4, time.Hour);
673 Assertion.AssertEquals ("test#10", 4, time.Second);
675 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
676 time = File.GetLastAccessTimeUtc (path);
677 Assertion.AssertEquals ("test#11", 2002, time.Year);
678 Assertion.AssertEquals ("test#12", 4, time.Month);
679 Assertion.AssertEquals ("test#13", 6, time.Day);
680 Assertion.AssertEquals ("test#14", 4, time.Hour);
681 Assertion.AssertEquals ("test#15", 4, time.Second);
683 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
684 Assertion.AssertEquals ("test#16", 2002, time.Year);
685 Assertion.AssertEquals ("test#17", 4, time.Month);
686 Assertion.AssertEquals ("test#18", 6, time.Day);
687 Assertion.AssertEquals ("test#19", 4, time.Hour);
688 Assertion.AssertEquals ("test#20", 4, time.Second);
697 public void LastWriteTime ()
699 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
700 if (File.Exists (path))
702 FileStream stream = null;
704 stream = File.Create (path);
707 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
708 DateTime time = File.GetLastWriteTime (path);
709 Assertion.AssertEquals ("test#01", 2002, time.Year);
710 Assertion.AssertEquals ("test#02", 4, time.Month);
711 Assertion.AssertEquals ("test#03", 6, time.Day);
712 Assertion.AssertEquals ("test#04", 4, time.Hour);
713 Assertion.AssertEquals ("test#05", 4, time.Second);
715 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
716 Assertion.AssertEquals ("test#06", 2002, time.Year);
717 Assertion.AssertEquals ("test#07", 4, time.Month);
718 Assertion.AssertEquals ("test#08", 6, time.Day);
719 Assertion.AssertEquals ("test#09", 4, time.Hour);
720 Assertion.AssertEquals ("test#10", 4, time.Second);
722 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
723 time = File.GetLastWriteTimeUtc (path);
724 Assertion.AssertEquals ("test#11", 2002, time.Year);
725 Assertion.AssertEquals ("test#12", 4, time.Month);
726 Assertion.AssertEquals ("test#13", 6, time.Day);
727 Assertion.AssertEquals ("test#14", 4, time.Hour);
728 Assertion.AssertEquals ("test#15", 4, time.Second);
730 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
731 Assertion.AssertEquals ("test#16", 2002, time.Year);
732 Assertion.AssertEquals ("test#17", 4, time.Month);
733 Assertion.AssertEquals ("test#18", 6, time.Day);
734 Assertion.AssertEquals ("test#19", 4, time.Hour);
735 Assertion.AssertEquals ("test#20", 4, time.Second);
744 [ExpectedException(typeof(ArgumentNullException))]
745 public void GetCreationTimeException1 ()
747 File.GetCreationTime (null as string);
751 [ExpectedException(typeof(ArgumentException))]
752 public void GetCreationTimeException2 ()
754 File.GetCreationTime ("");
759 [ExpectedException(typeof(IOException))]
761 public void GetCreationTime_NonExistingPath ()
763 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
765 DateTime time = File.GetCreationTime (path);
768 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
769 Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
770 Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
771 Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
772 Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
773 Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
774 Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
779 [ExpectedException(typeof(ArgumentException))]
780 public void GetCreationTimeException4 ()
782 File.GetCreationTime (" ");
786 [ExpectedException(typeof(ArgumentException))]
787 public void GetCreationTimeException5 ()
789 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
793 [ExpectedException(typeof(ArgumentNullException))]
794 public void GetCreationTimeUtcException1 ()
796 File.GetCreationTimeUtc (null as string);
800 [ExpectedException(typeof(ArgumentException))]
801 public void GetCreationTimeUtcException2 ()
803 File.GetCreationTimeUtc ("");
808 [ExpectedException (typeof (IOException))]
810 public void GetCreationTimeUtc_NonExistingPath ()
812 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
814 DateTime time = File.GetCreationTimeUtc (path);
817 Assertion.AssertEquals ("#1", 1601, time.Year);
818 Assertion.AssertEquals ("#2", 1, time.Month);
819 Assertion.AssertEquals ("#3", 1, time.Day);
820 Assertion.AssertEquals ("#4", 0, time.Hour);
821 Assertion.AssertEquals ("#5", 0, time.Second);
822 Assertion.AssertEquals ("#6", 0, time.Millisecond);
827 [ExpectedException(typeof(ArgumentException))]
828 public void GetCreationTimeUtcException4 ()
830 File.GetCreationTimeUtc (" ");
834 [ExpectedException(typeof(ArgumentException))]
835 public void GetCreationTimeUtcException5 ()
837 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
841 [ExpectedException(typeof(ArgumentNullException))]
842 public void GetLastAccessTimeException1 ()
844 File.GetLastAccessTime (null as string);
848 [ExpectedException(typeof(ArgumentException))]
849 public void GetLastAccessTimeException2 ()
851 File.GetLastAccessTime ("");
856 [ExpectedException (typeof (IOException))]
858 public void GetLastAccessTime_NonExistingPath ()
860 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
862 DateTime time = File.GetLastAccessTime (path);
865 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
866 Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
867 Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
868 Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
869 Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
870 Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
871 Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
876 [ExpectedException(typeof(ArgumentException))]
877 public void GetLastAccessTimeException4 ()
879 File.GetLastAccessTime (" ");
883 [ExpectedException(typeof(ArgumentException))]
884 public void GetLastAccessTimeException5 ()
886 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
890 [ExpectedException(typeof(ArgumentNullException))]
891 public void GetLastAccessTimeUtcException1 ()
893 File.GetLastAccessTimeUtc (null as string);
897 [ExpectedException(typeof(ArgumentException))]
898 public void GetLastAccessTimeUtcException2 ()
900 File.GetLastAccessTimeUtc ("");
905 [ExpectedException (typeof (IOException))]
907 public void GetLastAccessTimeUtc_NonExistingPath ()
909 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
911 DateTime time = File.GetLastAccessTimeUtc (path);
914 Assertion.AssertEquals ("#1", 1601, time.Year);
915 Assertion.AssertEquals ("#2", 1, time.Month);
916 Assertion.AssertEquals ("#3", 1, time.Day);
917 Assertion.AssertEquals ("#4", 0, time.Hour);
918 Assertion.AssertEquals ("#5", 0, time.Second);
919 Assertion.AssertEquals ("#6", 0, time.Millisecond);
924 [ExpectedException(typeof(ArgumentException))]
925 public void GetLastAccessTimeUtcException4 ()
927 File.GetLastAccessTimeUtc (" ");
931 [ExpectedException(typeof(ArgumentException))]
932 public void GetLastAccessTimeUtcException5 ()
934 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
938 [ExpectedException(typeof(ArgumentNullException))]
939 public void GetLastWriteTimeException1 ()
941 File.GetLastWriteTime (null as string);
945 [ExpectedException(typeof(ArgumentException))]
946 public void GetLastWriteTimeException2 ()
948 File.GetLastWriteTime ("");
953 [ExpectedException (typeof (IOException))]
955 public void GetLastWriteTime_NonExistingPath ()
957 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
959 DateTime time = File.GetLastWriteTime (path);
962 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
963 Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
964 Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
965 Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
966 Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
967 Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
968 Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
973 [ExpectedException(typeof(ArgumentException))]
974 public void GetLastWriteTimeException4 ()
976 File.GetLastWriteTime (" ");
980 [ExpectedException(typeof(ArgumentException))]
981 public void GetLastWriteTimeException5 ()
983 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
987 [ExpectedException(typeof(ArgumentNullException))]
988 public void GetLastWriteTimeUtcException1 ()
990 File.GetLastWriteTimeUtc (null as string);
994 [ExpectedException(typeof(ArgumentException))]
995 public void GetLastWriteTimeUtcException2 ()
997 File.GetLastWriteTimeUtc ("");
1002 [ExpectedException (typeof (IOException))]
1004 public void GetLastWriteTimeUtc_NonExistingPath ()
1006 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1008 DateTime time = File.GetLastWriteTimeUtc (path);
1011 Assertion.AssertEquals ("#1", 1601, time.Year);
1012 Assertion.AssertEquals ("#2", 1, time.Month);
1013 Assertion.AssertEquals ("#3", 1, time.Day);
1014 Assertion.AssertEquals ("#4", 0, time.Hour);
1015 Assertion.AssertEquals ("#5", 0, time.Second);
1016 Assertion.AssertEquals ("#6", 0, time.Millisecond);
1021 [ExpectedException(typeof(ArgumentException))]
1022 public void GetLastWriteTimeUtcException4 ()
1024 File.GetLastWriteTimeUtc (" ");
1028 [ExpectedException(typeof(ArgumentException))]
1029 public void GetLastWriteTimeUtcException5 ()
1031 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1035 public void FileStreamClose ()
1037 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1038 FileStream stream = null;
1040 stream = File.Create (path);
1050 // SetCreationTime and SetCreationTimeUtc exceptions
1053 [ExpectedException(typeof (ArgumentNullException))]
1054 public void SetCreationTimeArgumentNullException1 ()
1056 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1060 [ExpectedException(typeof (ArgumentException))]
1061 public void SetCreationTimeArgumenException1 ()
1063 File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1067 [ExpectedException(typeof (ArgumentException))]
1068 public void SetCreationTimeArgumenException2 ()
1070 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1074 // On Unix there are no invalid path chars.
1075 public void SetCreationTimeArgumenException3 ()
1077 if (Path.InvalidPathChars.Length > 1) {
1080 File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1081 } catch (ArgumentException) {
1085 Assertion.Assert ("#01", pass);
1090 [ExpectedException(typeof (FileNotFoundException))]
1091 public void SetCreationTimeFileNotFoundException1 ()
1093 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1096 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1100 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1101 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1103 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1104 // FileStream stream = null;
1105 // DeleteFile (path);
1107 // stream = File.Create (path);
1109 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1111 // if (stream != null)
1113 // DeleteFile (path);
1118 [ExpectedException(typeof (IOException))]
1119 public void SetCreationTimeIOException1 ()
1121 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1123 FileStream stream = null;
1125 stream = File.Create (path);
1126 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1135 [ExpectedException(typeof (ArgumentNullException))]
1136 public void SetCreationTimeUtcArgumentNullException1 ()
1138 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1142 [ExpectedException(typeof (ArgumentException))]
1143 public void SetCreationTimeUtcArgumenException1 ()
1145 File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1149 [ExpectedException(typeof (ArgumentException))]
1150 public void SetCreationTimeUtcArgumenException2 ()
1152 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1156 // On Unix there are no invalid path chars.
1157 public void SetCreationTimeUtcArgumentException3 ()
1159 if (Path.InvalidPathChars.Length > 1) {
1162 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1163 } catch (ArgumentException) {
1167 Assertion.Assert ("#01", pass);
1172 [ExpectedException(typeof (FileNotFoundException))]
1173 public void SetCreationTimeUtcFileNotFoundException1 ()
1175 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1178 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1182 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1183 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1185 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1186 // DeleteFile (path);
1187 // FileStream stream = null;
1189 // stream = File.Create (path);
1191 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1193 // if (stream != null)
1195 // DeleteFile (path);
1200 [ExpectedException(typeof (IOException))]
1201 public void SetCreationTimeUtcIOException1 ()
1203 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1205 FileStream stream = null;
1207 stream = File.Create (path);
1208 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1216 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1219 [ExpectedException(typeof (ArgumentNullException))]
1220 public void SetLastAccessTimeArgumentNullException1 ()
1222 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1226 [ExpectedException(typeof (ArgumentException))]
1227 public void SetLastAccessTimeArgumenException1 ()
1229 File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1233 [ExpectedException(typeof (ArgumentException))]
1234 public void SetLastAccessTimeArgumenException2 ()
1236 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1240 // On Unix there are no invalid path chars.
1241 public void SetLastAccessTimeArgumenException3 ()
1243 if (Path.InvalidPathChars.Length > 1) {
1246 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1247 } catch (ArgumentException) {
1251 Assertion.Assert ("#01", pass);
1256 [ExpectedException(typeof (FileNotFoundException))]
1257 public void SetLastAccessTimeFileNotFoundException1 ()
1259 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
1262 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1266 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1267 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
1269 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
1270 // DeleteFile (path);
1271 // FileStream stream = null;
1273 // stream = File.Create (path);
1275 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1277 // if (stream != null)
1279 // DeleteFile (path);
1284 [ExpectedException(typeof (IOException))]
1285 public void SetLastAccessTimeIOException1 ()
1287 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
1289 FileStream stream = null;
1291 stream = File.Create (path);
1292 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1301 [ExpectedException(typeof (ArgumentNullException))]
1302 public void SetLastAccessTimeUtcArgumentNullException1 ()
1304 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1308 [ExpectedException(typeof (ArgumentException))]
1309 public void SetCLastAccessTimeUtcArgumenException1 ()
1311 File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1315 [ExpectedException(typeof (ArgumentException))]
1316 public void SetLastAccessTimeUtcArgumenException2 ()
1318 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1322 // On Unix there are no invalid path chars.
1323 public void SetLastAccessTimeUtcArgumenException3 ()
1325 if (Path.InvalidPathChars.Length > 1) {
1328 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1329 } catch (ArgumentException) {
1333 Assertion.Assert ("#01", pass);
1338 [ExpectedException(typeof (FileNotFoundException))]
1339 public void SetLastAccessTimeUtcFileNotFoundException1 ()
1341 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
1344 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1348 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1349 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
1351 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
1352 // DeleteFile (path);
1353 // FileStream stream = null;
1355 // stream = File.Create (path);
1357 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1359 // if (stream != null)
1361 // DeleteFile (path);
1366 [ExpectedException(typeof (IOException))]
1367 public void SetLastAccessTimeUtcIOException1 ()
1369 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
1371 FileStream stream = null;
1373 stream = File.Create (path);
1374 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1382 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
1385 [ExpectedException(typeof (ArgumentNullException))]
1386 public void SetLastWriteTimeArgumentNullException1 ()
1388 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1392 [ExpectedException(typeof (ArgumentException))]
1393 public void SetLastWriteTimeArgumenException1 ()
1395 File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1399 [ExpectedException(typeof (ArgumentException))]
1400 public void SetLastWriteTimeArgumenException2 ()
1402 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1406 // On Unix there are no invalid path chars.
1407 public void SetLastWriteTimeArgumenException3 ()
1409 if (Path.InvalidPathChars.Length > 1) {
1412 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1413 } catch (ArgumentException) {
1417 Assertion.Assert ("#01", pass);
1422 [ExpectedException(typeof (FileNotFoundException))]
1423 public void SetLastWriteTimeFileNotFoundException1 ()
1425 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
1428 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1432 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1433 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
1435 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
1436 // DeleteFile (path);
1437 // FileStream stream = null;
1439 // stream = File.Create (path);
1441 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1443 // if (stream != null)
1445 // DeleteFile (path);
1450 [ExpectedException(typeof (IOException))]
1451 public void SetLastWriteTimeIOException1 ()
1453 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
1455 FileStream stream = null;
1457 stream = File.Create (path);
1458 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1467 [ExpectedException(typeof (ArgumentNullException))]
1468 public void SetLastWriteTimeUtcArgumentNullException1 ()
1470 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1474 [ExpectedException(typeof (ArgumentException))]
1475 public void SetCLastWriteTimeUtcArgumenException1 ()
1477 File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1481 [ExpectedException(typeof (ArgumentException))]
1482 public void SetLastWriteTimeUtcArgumenException2 ()
1484 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1488 // On Unix there are no invalid path chars.
1489 public void SetLastWriteTimeUtcArgumenException3 ()
1491 if (Path.InvalidPathChars.Length > 1) {
1494 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1495 } catch (ArgumentException) {
1499 Assertion.Assert ("#01", pass);
1504 [ExpectedException(typeof (FileNotFoundException))]
1505 public void SetLastWriteTimeUtcFileNotFoundException1 ()
1507 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
1510 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1514 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1515 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
1517 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
1518 // DeleteFile (path);
1519 // FileStream stream = null;
1521 // stream = File.Create (path);
1523 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1525 // if (stream != null)
1527 // DeleteFile (path);
1532 [ExpectedException(typeof (IOException))]
1533 public void SetLastWriteTimeUtcIOException1 ()
1535 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
1537 FileStream stream = null;
1539 stream = File.Create (path);
1540 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1549 public void OpenAppend ()
1551 string fn = Path.GetTempFileName ();
1552 using (FileStream s = File.Open (fn, FileMode.Append))
1559 void TestRWAT (string s)
1561 string f = Path.GetTempFileName ();
1563 File.WriteAllText (f, s);
1564 string r = File.ReadAllText (f);
1565 AssertEquals (r, s);
1571 public void ReadWriteAllText ()
1573 // The MSDN docs said something about
1574 // not including a final new line. it looks
1575 // like that was not true. I'm not sure what
1576 // that was talking about
1586 TestRWAT ("a\r\na");
1590 TestRWAT ("\r\n\r\n");
1594 private void DeleteFile (string path)
1596 if (File.Exists (path))