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);
591 [Category("NotWorking")]
593 public void TestGetCreationTime ()
595 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
599 File.Create (path).Close();
600 DateTime time = File.GetCreationTime (path);
601 Assert ("GetCreationTime incorrect", (DateTime.Now - time).TotalSeconds < 10);
607 // Setting the creation time on Unix is not possible
610 [Category("NotWorking")]
612 public void CreationTime ()
614 int platform = (int) Environment.OSVersion.Platform;
615 if ((platform == 4) || (platform == 128))
618 string path = Path.GetTempFileName ();
621 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
622 DateTime time = File.GetCreationTime (path);
623 Assertion.AssertEquals ("test#01", 2002, time.Year);
624 Assertion.AssertEquals ("test#02", 4, time.Month);
625 Assertion.AssertEquals ("test#03", 6, time.Day);
626 Assertion.AssertEquals ("test#04", 4, time.Hour);
627 Assertion.AssertEquals ("test#05", 4, time.Second);
629 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
630 Assertion.AssertEquals ("test#06", 2002, time.Year);
631 Assertion.AssertEquals ("test#07", 4, time.Month);
632 Assertion.AssertEquals ("test#08", 6, time.Day);
633 Assertion.AssertEquals ("test#09", 4, time.Hour);
634 Assertion.AssertEquals ("test#10", 4, time.Second);
636 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
637 time = File.GetCreationTimeUtc (path);
638 Assertion.AssertEquals ("test#11", 2002, time.Year);
639 Assertion.AssertEquals ("test#12", 4, time.Month);
640 Assertion.AssertEquals ("test#13", 6, time.Day);
641 Assertion.AssertEquals ("test#14", 4, time.Hour);
642 Assertion.AssertEquals ("test#15", 4, time.Second);
644 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
645 Assertion.AssertEquals ("test#16", 2002, time.Year);
646 Assertion.AssertEquals ("test#17", 4, time.Month);
647 Assertion.AssertEquals ("test#18", 6, time.Day);
648 Assertion.AssertEquals ("test#19", 4, time.Hour);
649 Assertion.AssertEquals ("test#20", 4, time.Second);
657 [Category("NotWorking")]
659 public void LastAccessTime ()
661 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
662 if (File.Exists (path))
664 FileStream stream = null;
666 stream = File.Create (path);
669 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
670 DateTime time = File.GetLastAccessTime (path);
671 Assertion.AssertEquals ("test#01", 2002, time.Year);
672 Assertion.AssertEquals ("test#02", 4, time.Month);
673 Assertion.AssertEquals ("test#03", 6, time.Day);
674 Assertion.AssertEquals ("test#04", 4, time.Hour);
675 Assertion.AssertEquals ("test#05", 4, time.Second);
677 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
678 Assertion.AssertEquals ("test#06", 2002, time.Year);
679 Assertion.AssertEquals ("test#07", 4, time.Month);
680 Assertion.AssertEquals ("test#08", 6, time.Day);
681 Assertion.AssertEquals ("test#09", 4, time.Hour);
682 Assertion.AssertEquals ("test#10", 4, time.Second);
684 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
685 time = File.GetLastAccessTimeUtc (path);
686 Assertion.AssertEquals ("test#11", 2002, time.Year);
687 Assertion.AssertEquals ("test#12", 4, time.Month);
688 Assertion.AssertEquals ("test#13", 6, time.Day);
689 Assertion.AssertEquals ("test#14", 4, time.Hour);
690 Assertion.AssertEquals ("test#15", 4, time.Second);
692 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
693 Assertion.AssertEquals ("test#16", 2002, time.Year);
694 Assertion.AssertEquals ("test#17", 4, time.Month);
695 Assertion.AssertEquals ("test#18", 6, time.Day);
696 Assertion.AssertEquals ("test#19", 4, time.Hour);
697 Assertion.AssertEquals ("test#20", 4, time.Second);
706 public void LastWriteTime ()
708 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
709 if (File.Exists (path))
711 FileStream stream = null;
713 stream = File.Create (path);
716 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
717 DateTime time = File.GetLastWriteTime (path);
718 Assertion.AssertEquals ("test#01", 2002, time.Year);
719 Assertion.AssertEquals ("test#02", 4, time.Month);
720 Assertion.AssertEquals ("test#03", 6, time.Day);
721 Assertion.AssertEquals ("test#04", 4, time.Hour);
722 Assertion.AssertEquals ("test#05", 4, time.Second);
724 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
725 Assertion.AssertEquals ("test#06", 2002, time.Year);
726 Assertion.AssertEquals ("test#07", 4, time.Month);
727 Assertion.AssertEquals ("test#08", 6, time.Day);
728 Assertion.AssertEquals ("test#09", 4, time.Hour);
729 Assertion.AssertEquals ("test#10", 4, time.Second);
731 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
732 time = File.GetLastWriteTimeUtc (path);
733 Assertion.AssertEquals ("test#11", 2002, time.Year);
734 Assertion.AssertEquals ("test#12", 4, time.Month);
735 Assertion.AssertEquals ("test#13", 6, time.Day);
736 Assertion.AssertEquals ("test#14", 4, time.Hour);
737 Assertion.AssertEquals ("test#15", 4, time.Second);
739 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
740 Assertion.AssertEquals ("test#16", 2002, time.Year);
741 Assertion.AssertEquals ("test#17", 4, time.Month);
742 Assertion.AssertEquals ("test#18", 6, time.Day);
743 Assertion.AssertEquals ("test#19", 4, time.Hour);
744 Assertion.AssertEquals ("test#20", 4, time.Second);
753 [ExpectedException(typeof(ArgumentNullException))]
755 [Category("NotWorking")]
757 public void GetCreationTimeException1 ()
759 File.GetCreationTime (null as string);
763 [ExpectedException(typeof(ArgumentException))]
765 [Category("NotWorking")]
767 public void GetCreationTimeException2 ()
769 File.GetCreationTime ("");
774 [ExpectedException(typeof(IOException))]
777 [Category("NotWorking")]
779 public void GetCreationTime_NonExistingPath ()
781 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
783 DateTime time = File.GetCreationTime (path);
786 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
787 Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
788 Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
789 Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
790 Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
791 Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
792 Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
797 [ExpectedException(typeof(ArgumentException))]
799 [Category("NotWorking")]
801 public void GetCreationTimeException4 ()
803 File.GetCreationTime (" ");
807 [ExpectedException(typeof(ArgumentException))]
809 [Category("NotWorking")]
811 public void GetCreationTimeException5 ()
813 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
817 [ExpectedException(typeof(ArgumentNullException))]
819 [Category("NotWorking")]
821 public void GetCreationTimeUtcException1 ()
823 File.GetCreationTimeUtc (null as string);
827 [ExpectedException(typeof(ArgumentException))]
829 [Category("NotWorking")]
831 public void GetCreationTimeUtcException2 ()
833 File.GetCreationTimeUtc ("");
838 [ExpectedException (typeof (IOException))]
841 [Category("NotWorking")]
843 public void GetCreationTimeUtc_NonExistingPath ()
845 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
847 DateTime time = File.GetCreationTimeUtc (path);
850 Assertion.AssertEquals ("#1", 1601, time.Year);
851 Assertion.AssertEquals ("#2", 1, time.Month);
852 Assertion.AssertEquals ("#3", 1, time.Day);
853 Assertion.AssertEquals ("#4", 0, time.Hour);
854 Assertion.AssertEquals ("#5", 0, time.Second);
855 Assertion.AssertEquals ("#6", 0, time.Millisecond);
860 [ExpectedException(typeof(ArgumentException))]
862 [Category("NotWorking")]
864 public void GetCreationTimeUtcException4 ()
866 File.GetCreationTimeUtc (" ");
870 [ExpectedException(typeof(ArgumentException))]
872 [Category("NotWorking")]
874 public void GetCreationTimeUtcException5 ()
876 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
880 [ExpectedException(typeof(ArgumentNullException))]
882 [Category("NotWorking")]
884 public void GetLastAccessTimeException1 ()
886 File.GetLastAccessTime (null as string);
890 [ExpectedException(typeof(ArgumentException))]
892 [Category("NotWorking")]
894 public void GetLastAccessTimeException2 ()
896 File.GetLastAccessTime ("");
901 [ExpectedException (typeof (IOException))]
904 [Category("NotWorking")]
906 public void GetLastAccessTime_NonExistingPath ()
908 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
910 DateTime time = File.GetLastAccessTime (path);
913 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
914 Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
915 Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
916 Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
917 Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
918 Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
919 Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
924 [ExpectedException(typeof(ArgumentException))]
926 [Category("NotWorking")]
928 public void GetLastAccessTimeException4 ()
930 File.GetLastAccessTime (" ");
934 [ExpectedException(typeof(ArgumentException))]
936 [Category("NotWorking")]
938 public void GetLastAccessTimeException5 ()
940 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
944 [ExpectedException(typeof(ArgumentNullException))]
946 [Category("NotWorking")]
948 public void GetLastAccessTimeUtcException1 ()
950 File.GetLastAccessTimeUtc (null as string);
954 [ExpectedException(typeof(ArgumentException))]
956 [Category("NotWorking")]
958 public void GetLastAccessTimeUtcException2 ()
960 File.GetLastAccessTimeUtc ("");
965 [ExpectedException (typeof (IOException))]
968 [Category("NotWorking")]
970 public void GetLastAccessTimeUtc_NonExistingPath ()
972 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
974 DateTime time = File.GetLastAccessTimeUtc (path);
977 Assertion.AssertEquals ("#1", 1601, time.Year);
978 Assertion.AssertEquals ("#2", 1, time.Month);
979 Assertion.AssertEquals ("#3", 1, time.Day);
980 Assertion.AssertEquals ("#4", 0, time.Hour);
981 Assertion.AssertEquals ("#5", 0, time.Second);
982 Assertion.AssertEquals ("#6", 0, time.Millisecond);
987 [ExpectedException(typeof(ArgumentException))]
989 [Category("NotWorking")]
991 public void GetLastAccessTimeUtcException4 ()
993 File.GetLastAccessTimeUtc (" ");
997 [ExpectedException(typeof(ArgumentException))]
999 [Category("NotWorking")]
1001 public void GetLastAccessTimeUtcException5 ()
1003 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1007 [ExpectedException(typeof(ArgumentNullException))]
1008 public void GetLastWriteTimeException1 ()
1010 File.GetLastWriteTime (null as string);
1014 [ExpectedException(typeof(ArgumentException))]
1015 public void GetLastWriteTimeException2 ()
1017 File.GetLastWriteTime ("");
1022 [ExpectedException (typeof (IOException))]
1024 public void GetLastWriteTime_NonExistingPath ()
1026 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1028 DateTime time = File.GetLastWriteTime (path);
1031 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1032 Assertion.AssertEquals ("#1", expectedTime.Year, time.Year);
1033 Assertion.AssertEquals ("#2", expectedTime.Month, time.Month);
1034 Assertion.AssertEquals ("#3", expectedTime.Day, time.Day);
1035 Assertion.AssertEquals ("#4", expectedTime.Hour, time.Hour);
1036 Assertion.AssertEquals ("#5", expectedTime.Second, time.Second);
1037 Assertion.AssertEquals ("#6", expectedTime.Millisecond, time.Millisecond);
1042 [ExpectedException(typeof(ArgumentException))]
1043 public void GetLastWriteTimeException4 ()
1045 File.GetLastWriteTime (" ");
1049 [ExpectedException(typeof(ArgumentException))]
1050 public void GetLastWriteTimeException5 ()
1052 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1056 [ExpectedException(typeof(ArgumentNullException))]
1057 public void GetLastWriteTimeUtcException1 ()
1059 File.GetLastWriteTimeUtc (null as string);
1063 [ExpectedException(typeof(ArgumentException))]
1064 public void GetLastWriteTimeUtcException2 ()
1066 File.GetLastWriteTimeUtc ("");
1071 [ExpectedException (typeof (IOException))]
1073 public void GetLastWriteTimeUtc_NonExistingPath ()
1075 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1077 DateTime time = File.GetLastWriteTimeUtc (path);
1080 Assertion.AssertEquals ("#1", 1601, time.Year);
1081 Assertion.AssertEquals ("#2", 1, time.Month);
1082 Assertion.AssertEquals ("#3", 1, time.Day);
1083 Assertion.AssertEquals ("#4", 0, time.Hour);
1084 Assertion.AssertEquals ("#5", 0, time.Second);
1085 Assertion.AssertEquals ("#6", 0, time.Millisecond);
1090 [ExpectedException(typeof(ArgumentException))]
1091 public void GetLastWriteTimeUtcException4 ()
1093 File.GetLastWriteTimeUtc (" ");
1097 [ExpectedException(typeof(ArgumentException))]
1098 public void GetLastWriteTimeUtcException5 ()
1100 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1104 public void FileStreamClose ()
1106 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1107 FileStream stream = null;
1109 stream = File.Create (path);
1119 // SetCreationTime and SetCreationTimeUtc exceptions
1122 [ExpectedException(typeof (ArgumentNullException))]
1124 [Category("NotWorking")]
1126 public void SetCreationTimeArgumentNullException1 ()
1128 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1132 [ExpectedException(typeof (ArgumentException))]
1134 [Category("NotWorking")]
1136 public void SetCreationTimeArgumenException1 ()
1138 File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1142 [ExpectedException(typeof (ArgumentException))]
1144 [Category("NotWorking")]
1146 public void SetCreationTimeArgumenException2 ()
1148 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1152 // On Unix there are no invalid path chars.
1154 [Category("NotWorking")]
1156 public void SetCreationTimeArgumenException3 ()
1158 if (Path.InvalidPathChars.Length > 1) {
1161 File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1162 } catch (ArgumentException) {
1166 Assertion.Assert ("#01", pass);
1171 [ExpectedException(typeof (FileNotFoundException))]
1173 [Category("NotWorking")]
1175 public void SetCreationTimeFileNotFoundException1 ()
1177 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1180 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1184 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1185 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1187 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1188 // FileStream stream = null;
1189 // DeleteFile (path);
1191 // stream = File.Create (path);
1193 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1195 // if (stream != null)
1197 // DeleteFile (path);
1202 [ExpectedException(typeof (IOException))]
1204 [Category("NotWorking")]
1206 public void SetCreationTimeIOException1 ()
1208 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1210 FileStream stream = null;
1212 stream = File.Create (path);
1213 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1222 [ExpectedException(typeof (ArgumentNullException))]
1224 [Category("NotWorking")]
1226 public void SetCreationTimeUtcArgumentNullException1 ()
1228 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1232 [ExpectedException(typeof (ArgumentException))]
1234 [Category("NotWorking")]
1236 public void SetCreationTimeUtcArgumenException1 ()
1238 File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1242 [ExpectedException(typeof (ArgumentException))]
1244 [Category("NotWorking")]
1246 public void SetCreationTimeUtcArgumenException2 ()
1248 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1252 // On Unix there are no invalid path chars.
1254 [Category("NotWorking")]
1256 public void SetCreationTimeUtcArgumentException3 ()
1258 if (Path.InvalidPathChars.Length > 1) {
1261 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1262 } catch (ArgumentException) {
1266 Assertion.Assert ("#01", pass);
1271 [ExpectedException(typeof (FileNotFoundException))]
1273 [Category("NotWorking")]
1275 public void SetCreationTimeUtcFileNotFoundException1 ()
1277 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1280 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1284 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1285 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1287 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1288 // DeleteFile (path);
1289 // FileStream stream = null;
1291 // stream = File.Create (path);
1293 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1295 // if (stream != null)
1297 // DeleteFile (path);
1302 [ExpectedException(typeof (IOException))]
1304 [Category("NotWorking")]
1306 public void SetCreationTimeUtcIOException1 ()
1308 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1310 FileStream stream = null;
1312 stream = File.Create (path);
1313 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1321 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1324 [ExpectedException(typeof (ArgumentNullException))]
1326 [Category("NotWorking")]
1328 public void SetLastAccessTimeArgumentNullException1 ()
1330 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1334 [ExpectedException(typeof (ArgumentException))]
1336 [Category("NotWorking")]
1338 public void SetLastAccessTimeArgumenException1 ()
1340 File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1344 [ExpectedException(typeof (ArgumentException))]
1346 [Category("NotWorking")]
1348 public void SetLastAccessTimeArgumenException2 ()
1350 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1354 // On Unix there are no invalid path chars.
1356 [Category("NotWorking")]
1358 public void SetLastAccessTimeArgumenException3 ()
1360 if (Path.InvalidPathChars.Length > 1) {
1363 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1364 } catch (ArgumentException) {
1368 Assertion.Assert ("#01", pass);
1373 [ExpectedException(typeof (FileNotFoundException))]
1375 [Category("NotWorking")]
1377 public void SetLastAccessTimeFileNotFoundException1 ()
1379 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
1382 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1386 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1387 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
1389 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
1390 // DeleteFile (path);
1391 // FileStream stream = null;
1393 // stream = File.Create (path);
1395 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1397 // if (stream != null)
1399 // DeleteFile (path);
1404 [ExpectedException(typeof (IOException))]
1406 [Category("NotWorking")]
1408 public void SetLastAccessTimeIOException1 ()
1410 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
1412 FileStream stream = null;
1414 stream = File.Create (path);
1415 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1424 [ExpectedException(typeof (ArgumentNullException))]
1426 [Category("NotWorking")]
1428 public void SetLastAccessTimeUtcArgumentNullException1 ()
1430 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1434 [ExpectedException(typeof (ArgumentException))]
1436 [Category("NotWorking")]
1438 public void SetCLastAccessTimeUtcArgumenException1 ()
1440 File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1444 [ExpectedException(typeof (ArgumentException))]
1446 [Category("NotWorking")]
1448 public void SetLastAccessTimeUtcArgumenException2 ()
1450 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1454 // On Unix there are no invalid path chars.
1456 [Category("NotWorking")]
1458 public void SetLastAccessTimeUtcArgumenException3 ()
1460 if (Path.InvalidPathChars.Length > 1) {
1463 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1464 } catch (ArgumentException) {
1468 Assertion.Assert ("#01", pass);
1473 [ExpectedException(typeof (FileNotFoundException))]
1475 [Category("NotWorking")]
1477 public void SetLastAccessTimeUtcFileNotFoundException1 ()
1479 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
1482 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1486 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1487 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
1489 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
1490 // DeleteFile (path);
1491 // FileStream stream = null;
1493 // stream = File.Create (path);
1495 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1497 // if (stream != null)
1499 // DeleteFile (path);
1504 [ExpectedException(typeof (IOException))]
1506 [Category("NotWorking")]
1508 public void SetLastAccessTimeUtcIOException1 ()
1510 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
1512 FileStream stream = null;
1514 stream = File.Create (path);
1515 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1523 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
1526 [ExpectedException(typeof (ArgumentNullException))]
1527 public void SetLastWriteTimeArgumentNullException1 ()
1529 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1533 [ExpectedException(typeof (ArgumentException))]
1534 public void SetLastWriteTimeArgumenException1 ()
1536 File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1540 [ExpectedException(typeof (ArgumentException))]
1541 public void SetLastWriteTimeArgumenException2 ()
1543 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1547 // On Unix there are no invalid path chars.
1548 public void SetLastWriteTimeArgumenException3 ()
1550 if (Path.InvalidPathChars.Length > 1) {
1553 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1554 } catch (ArgumentException) {
1558 Assertion.Assert ("#01", pass);
1563 [ExpectedException(typeof (FileNotFoundException))]
1564 public void SetLastWriteTimeFileNotFoundException1 ()
1566 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
1569 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1573 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1574 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
1576 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
1577 // DeleteFile (path);
1578 // FileStream stream = null;
1580 // stream = File.Create (path);
1582 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1584 // if (stream != null)
1586 // DeleteFile (path);
1591 [ExpectedException(typeof (IOException))]
1593 [Category("NotWorking")]
1595 public void SetLastWriteTimeIOException1 ()
1597 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
1599 FileStream stream = null;
1601 stream = File.Create (path);
1602 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1611 [ExpectedException(typeof (ArgumentNullException))]
1612 public void SetLastWriteTimeUtcArgumentNullException1 ()
1614 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1618 [ExpectedException(typeof (ArgumentException))]
1619 public void SetCLastWriteTimeUtcArgumenException1 ()
1621 File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1625 [ExpectedException(typeof (ArgumentException))]
1626 public void SetLastWriteTimeUtcArgumenException2 ()
1628 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1632 // On Unix there are no invalid path chars.
1633 public void SetLastWriteTimeUtcArgumenException3 ()
1635 if (Path.InvalidPathChars.Length > 1) {
1638 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1639 } catch (ArgumentException) {
1643 Assertion.Assert ("#01", pass);
1648 [ExpectedException(typeof (FileNotFoundException))]
1649 public void SetLastWriteTimeUtcFileNotFoundException1 ()
1651 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
1654 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1658 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1659 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
1661 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
1662 // DeleteFile (path);
1663 // FileStream stream = null;
1665 // stream = File.Create (path);
1667 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1669 // if (stream != null)
1671 // DeleteFile (path);
1676 [ExpectedException(typeof (IOException))]
1678 [Category("NotWorking")]
1680 public void SetLastWriteTimeUtcIOException1 ()
1682 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
1684 FileStream stream = null;
1686 stream = File.Create (path);
1687 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1696 public void OpenAppend ()
1698 string fn = Path.GetTempFileName ();
1699 using (FileStream s = File.Open (fn, FileMode.Append))
1706 void TestRWAT (string s)
1708 string f = Path.GetTempFileName ();
1710 File.WriteAllText (f, s);
1711 string r = File.ReadAllText (f);
1712 AssertEquals (r, s);
1718 public void ReadWriteAllText ()
1720 // The MSDN docs said something about
1721 // not including a final new line. it looks
1722 // like that was not true. I'm not sure what
1723 // that was talking about
1733 TestRWAT ("a\r\na");
1737 TestRWAT ("\r\n\r\n");
1741 private void DeleteFile (string path)
1743 if (File.Exists (path))