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);
605 [ExpectedException(typeof(IOException))]
606 public void TestGetCreationTimeException ()
608 // Test nonexistent files
609 string path2 = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
611 // should throw an exception
612 File.GetCreationTime (path2);
617 // Setting the creation time on Unix is not possible
619 public void CreationTime ()
621 int platform = (int) Environment.OSVersion.Platform;
622 if ((platform == 4) || (platform == 128))
625 string path = Path.GetTempFileName ();
628 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
629 DateTime time = File.GetCreationTime (path);
630 Assertion.AssertEquals ("test#01", 2002, time.Year);
631 Assertion.AssertEquals ("test#02", 4, time.Month);
632 Assertion.AssertEquals ("test#03", 6, time.Day);
633 Assertion.AssertEquals ("test#04", 4, time.Hour);
634 Assertion.AssertEquals ("test#05", 4, time.Second);
636 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
637 Assertion.AssertEquals ("test#06", 2002, time.Year);
638 Assertion.AssertEquals ("test#07", 4, time.Month);
639 Assertion.AssertEquals ("test#08", 6, time.Day);
640 Assertion.AssertEquals ("test#09", 4, time.Hour);
641 Assertion.AssertEquals ("test#10", 4, time.Second);
643 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
644 time = File.GetCreationTimeUtc (path);
645 Assertion.AssertEquals ("test#11", 2002, time.Year);
646 Assertion.AssertEquals ("test#12", 4, time.Month);
647 Assertion.AssertEquals ("test#13", 6, time.Day);
648 Assertion.AssertEquals ("test#14", 4, time.Hour);
649 Assertion.AssertEquals ("test#15", 4, time.Second);
651 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
652 Assertion.AssertEquals ("test#16", 2002, time.Year);
653 Assertion.AssertEquals ("test#17", 4, time.Month);
654 Assertion.AssertEquals ("test#18", 6, time.Day);
655 Assertion.AssertEquals ("test#19", 4, time.Hour);
656 Assertion.AssertEquals ("test#20", 4, time.Second);
663 public void LastAccessTime ()
665 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
666 if (File.Exists (path))
668 FileStream stream = null;
670 stream = File.Create (path);
673 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
674 DateTime time = File.GetLastAccessTime (path);
675 Assertion.AssertEquals ("test#01", 2002, time.Year);
676 Assertion.AssertEquals ("test#02", 4, time.Month);
677 Assertion.AssertEquals ("test#03", 6, time.Day);
678 Assertion.AssertEquals ("test#04", 4, time.Hour);
679 Assertion.AssertEquals ("test#05", 4, time.Second);
681 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
682 Assertion.AssertEquals ("test#06", 2002, time.Year);
683 Assertion.AssertEquals ("test#07", 4, time.Month);
684 Assertion.AssertEquals ("test#08", 6, time.Day);
685 Assertion.AssertEquals ("test#09", 4, time.Hour);
686 Assertion.AssertEquals ("test#10", 4, time.Second);
688 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
689 time = File.GetLastAccessTimeUtc (path);
690 Assertion.AssertEquals ("test#11", 2002, time.Year);
691 Assertion.AssertEquals ("test#12", 4, time.Month);
692 Assertion.AssertEquals ("test#13", 6, time.Day);
693 Assertion.AssertEquals ("test#14", 4, time.Hour);
694 Assertion.AssertEquals ("test#15", 4, time.Second);
696 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
697 Assertion.AssertEquals ("test#16", 2002, time.Year);
698 Assertion.AssertEquals ("test#17", 4, time.Month);
699 Assertion.AssertEquals ("test#18", 6, time.Day);
700 Assertion.AssertEquals ("test#19", 4, time.Hour);
701 Assertion.AssertEquals ("test#20", 4, time.Second);
710 public void LastWriteTime ()
712 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
713 if (File.Exists (path))
715 FileStream stream = null;
717 stream = File.Create (path);
720 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
721 DateTime time = File.GetLastWriteTime (path);
722 Assertion.AssertEquals ("test#01", 2002, time.Year);
723 Assertion.AssertEquals ("test#02", 4, time.Month);
724 Assertion.AssertEquals ("test#03", 6, time.Day);
725 Assertion.AssertEquals ("test#04", 4, time.Hour);
726 Assertion.AssertEquals ("test#05", 4, time.Second);
728 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
729 Assertion.AssertEquals ("test#06", 2002, time.Year);
730 Assertion.AssertEquals ("test#07", 4, time.Month);
731 Assertion.AssertEquals ("test#08", 6, time.Day);
732 Assertion.AssertEquals ("test#09", 4, time.Hour);
733 Assertion.AssertEquals ("test#10", 4, time.Second);
735 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
736 time = File.GetLastWriteTimeUtc (path);
737 Assertion.AssertEquals ("test#11", 2002, time.Year);
738 Assertion.AssertEquals ("test#12", 4, time.Month);
739 Assertion.AssertEquals ("test#13", 6, time.Day);
740 Assertion.AssertEquals ("test#14", 4, time.Hour);
741 Assertion.AssertEquals ("test#15", 4, time.Second);
743 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
744 Assertion.AssertEquals ("test#16", 2002, time.Year);
745 Assertion.AssertEquals ("test#17", 4, time.Month);
746 Assertion.AssertEquals ("test#18", 6, time.Day);
747 Assertion.AssertEquals ("test#19", 4, time.Hour);
748 Assertion.AssertEquals ("test#20", 4, time.Second);
757 [ExpectedException(typeof(ArgumentNullException))]
758 public void GetCreationTimeException1 ()
760 File.GetCreationTime (null as string);
764 [ExpectedException(typeof(ArgumentException))]
765 public void GetCreationTimeException2 ()
767 File.GetCreationTime ("");
771 [ExpectedException(typeof(IOException))]
772 public void GetCreationTimeException3 ()
774 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
776 File.GetCreationTime (path);
780 [ExpectedException(typeof(ArgumentException))]
781 public void GetCreationTimeException4 ()
783 File.GetCreationTime (" ");
787 [ExpectedException(typeof(ArgumentException))]
788 public void GetCreationTimeException5 ()
790 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
794 [ExpectedException(typeof(ArgumentNullException))]
795 public void GetCreationTimeUtcException1 ()
797 File.GetCreationTimeUtc (null as string);
801 [ExpectedException(typeof(ArgumentException))]
802 public void GetCreationTimeUtcException2 ()
804 File.GetCreationTimeUtc ("");
808 [ExpectedException(typeof(IOException))]
809 public void GetCreationTimeUtcException3 ()
811 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
813 File.GetCreationTimeUtc (path);
817 [ExpectedException(typeof(ArgumentException))]
818 public void GetCreationTimeUtcException4 ()
820 File.GetCreationTimeUtc (" ");
824 [ExpectedException(typeof(ArgumentException))]
825 public void GetCreationTimeUtcException5 ()
827 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
831 [ExpectedException(typeof(ArgumentNullException))]
832 public void GetLastAccessTimeException1 ()
834 File.GetLastAccessTime (null as string);
838 [ExpectedException(typeof(ArgumentException))]
839 public void GetLastAccessTimeException2 ()
841 File.GetLastAccessTime ("");
845 [ExpectedException(typeof(IOException))]
846 public void GetLastAccessTimeException3 ()
848 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
850 File.GetLastAccessTime (path);
854 [ExpectedException(typeof(ArgumentException))]
855 public void GetLastAccessTimeException4 ()
857 File.GetLastAccessTime (" ");
861 [ExpectedException(typeof(ArgumentException))]
862 public void GetLastAccessTimeException5 ()
864 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
868 [ExpectedException(typeof(ArgumentNullException))]
869 public void GetLastAccessTimeUtcException1 ()
871 File.GetLastAccessTimeUtc (null as string);
875 [ExpectedException(typeof(ArgumentException))]
876 public void GetLastAccessTimeUtcException2 ()
878 File.GetLastAccessTimeUtc ("");
882 [ExpectedException(typeof(IOException))]
883 public void GetLastAccessTimeUtcException3 ()
885 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
887 File.GetLastAccessTimeUtc (path);
891 [ExpectedException(typeof(ArgumentException))]
892 public void GetLastAccessTimeUtcException4 ()
894 File.GetLastAccessTimeUtc (" ");
898 [ExpectedException(typeof(ArgumentException))]
899 public void GetLastAccessTimeUtcException5 ()
901 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
905 [ExpectedException(typeof(ArgumentNullException))]
906 public void GetLastWriteTimeException1 ()
908 File.GetLastWriteTime (null as string);
912 [ExpectedException(typeof(ArgumentException))]
913 public void GetLastWriteTimeException2 ()
915 File.GetLastWriteTime ("");
919 [ExpectedException(typeof(IOException))]
920 public void GetLastWriteTimeException3 ()
922 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
924 File.GetLastWriteTime (path);
928 [ExpectedException(typeof(ArgumentException))]
929 public void GetLastWriteTimeException4 ()
931 File.GetLastWriteTime (" ");
935 [ExpectedException(typeof(ArgumentException))]
936 public void GetLastWriteTimeException5 ()
938 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
942 [ExpectedException(typeof(ArgumentNullException))]
943 public void GetLastWriteTimeUtcException1 ()
945 File.GetLastWriteTimeUtc (null as string);
949 [ExpectedException(typeof(ArgumentException))]
950 public void GetLastWriteTimeUtcException2 ()
952 File.GetLastAccessTimeUtc ("");
956 [ExpectedException(typeof(IOException))]
957 public void GetLastWriteTimeUtcException3 ()
959 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
961 File.GetLastAccessTimeUtc (path);
965 [ExpectedException(typeof(ArgumentException))]
966 public void GetLastWriteTimeUtcException4 ()
968 File.GetLastAccessTimeUtc (" ");
972 [ExpectedException(typeof(ArgumentException))]
973 public void GetLastWriteTimeUtcException5 ()
975 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
979 public void FileStreamClose ()
981 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
982 FileStream stream = null;
984 stream = File.Create (path);
994 // SetCreationTime and SetCreationTimeUtc exceptions
997 [ExpectedException(typeof (ArgumentNullException))]
998 public void SetCreationTimeArgumentNullException1 ()
1000 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1004 [ExpectedException(typeof (ArgumentException))]
1005 public void SetCreationTimeArgumenException1 ()
1007 File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1011 [ExpectedException(typeof (ArgumentException))]
1012 public void SetCreationTimeArgumenException2 ()
1014 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1018 // On Unix there are no invalid path chars.
1019 public void SetCreationTimeArgumenException3 ()
1021 if (Path.InvalidPathChars.Length > 1) {
1024 File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1025 } catch (ArgumentException) {
1029 Assertion.Assert ("#01", pass);
1034 [ExpectedException(typeof (FileNotFoundException))]
1035 public void SetCreationTimeFileNotFoundException1 ()
1037 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1040 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1044 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1045 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1047 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1048 // FileStream stream = null;
1049 // DeleteFile (path);
1051 // stream = File.Create (path);
1053 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1055 // if (stream != null)
1057 // DeleteFile (path);
1062 [ExpectedException(typeof (IOException))]
1063 public void SetCreationTimeIOException1 ()
1065 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1067 FileStream stream = null;
1069 stream = File.Create (path);
1070 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1079 [ExpectedException(typeof (ArgumentNullException))]
1080 public void SetCreationTimeUtcArgumentNullException1 ()
1082 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1086 [ExpectedException(typeof (ArgumentException))]
1087 public void SetCreationTimeUtcArgumenException1 ()
1089 File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1093 [ExpectedException(typeof (ArgumentException))]
1094 public void SetCreationTimeUtcArgumenException2 ()
1096 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1100 // On Unix there are no invalid path chars.
1101 public void SetCreationTimeUtcArgumentException3 ()
1103 if (Path.InvalidPathChars.Length > 1) {
1106 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1107 } catch (ArgumentException) {
1111 Assertion.Assert ("#01", pass);
1116 [ExpectedException(typeof (FileNotFoundException))]
1117 public void SetCreationTimeUtcFileNotFoundException1 ()
1119 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1122 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1126 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1127 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1129 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1130 // DeleteFile (path);
1131 // FileStream stream = null;
1133 // stream = File.Create (path);
1135 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1137 // if (stream != null)
1139 // DeleteFile (path);
1144 [ExpectedException(typeof (IOException))]
1145 public void SetCreationTimeUtcIOException1 ()
1147 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1149 FileStream stream = null;
1151 stream = File.Create (path);
1152 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1160 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1163 [ExpectedException(typeof (ArgumentNullException))]
1164 public void SetLastAccessTimeArgumentNullException1 ()
1166 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1170 [ExpectedException(typeof (ArgumentException))]
1171 public void SetLastAccessTimeArgumenException1 ()
1173 File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1177 [ExpectedException(typeof (ArgumentException))]
1178 public void SetLastAccessTimeArgumenException2 ()
1180 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1184 // On Unix there are no invalid path chars.
1185 public void SetLastAccessTimeArgumenException3 ()
1187 if (Path.InvalidPathChars.Length > 1) {
1190 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1191 } catch (ArgumentException) {
1195 Assertion.Assert ("#01", pass);
1200 [ExpectedException(typeof (FileNotFoundException))]
1201 public void SetLastAccessTimeFileNotFoundException1 ()
1203 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
1206 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1210 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1211 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
1213 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
1214 // DeleteFile (path);
1215 // FileStream stream = null;
1217 // stream = File.Create (path);
1219 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1221 // if (stream != null)
1223 // DeleteFile (path);
1228 [ExpectedException(typeof (IOException))]
1229 public void SetLastAccessTimeIOException1 ()
1231 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
1233 FileStream stream = null;
1235 stream = File.Create (path);
1236 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1245 [ExpectedException(typeof (ArgumentNullException))]
1246 public void SetLastAccessTimeUtcArgumentNullException1 ()
1248 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1252 [ExpectedException(typeof (ArgumentException))]
1253 public void SetCLastAccessTimeUtcArgumenException1 ()
1255 File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1259 [ExpectedException(typeof (ArgumentException))]
1260 public void SetLastAccessTimeUtcArgumenException2 ()
1262 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1266 // On Unix there are no invalid path chars.
1267 public void SetLastAccessTimeUtcArgumenException3 ()
1269 if (Path.InvalidPathChars.Length > 1) {
1272 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1273 } catch (ArgumentException) {
1277 Assertion.Assert ("#01", pass);
1282 [ExpectedException(typeof (FileNotFoundException))]
1283 public void SetLastAccessTimeUtcFileNotFoundException1 ()
1285 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
1288 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1292 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1293 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
1295 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
1296 // DeleteFile (path);
1297 // FileStream stream = null;
1299 // stream = File.Create (path);
1301 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1303 // if (stream != null)
1305 // DeleteFile (path);
1310 [ExpectedException(typeof (IOException))]
1311 public void SetLastAccessTimeUtcIOException1 ()
1313 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
1315 FileStream stream = null;
1317 stream = File.Create (path);
1318 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1326 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
1329 [ExpectedException(typeof (ArgumentNullException))]
1330 public void SetLastWriteTimeArgumentNullException1 ()
1332 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1336 [ExpectedException(typeof (ArgumentException))]
1337 public void SetLastWriteTimeArgumenException1 ()
1339 File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1343 [ExpectedException(typeof (ArgumentException))]
1344 public void SetLastWriteTimeArgumenException2 ()
1346 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1350 // On Unix there are no invalid path chars.
1351 public void SetLastWriteTimeArgumenException3 ()
1353 if (Path.InvalidPathChars.Length > 1) {
1356 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1357 } catch (ArgumentException) {
1361 Assertion.Assert ("#01", pass);
1366 [ExpectedException(typeof (FileNotFoundException))]
1367 public void SetLastWriteTimeFileNotFoundException1 ()
1369 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
1372 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1376 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1377 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
1379 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
1380 // DeleteFile (path);
1381 // FileStream stream = null;
1383 // stream = File.Create (path);
1385 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1387 // if (stream != null)
1389 // DeleteFile (path);
1394 [ExpectedException(typeof (IOException))]
1395 public void SetLastWriteTimeIOException1 ()
1397 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
1399 FileStream stream = null;
1401 stream = File.Create (path);
1402 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1411 [ExpectedException(typeof (ArgumentNullException))]
1412 public void SetLastWriteTimeUtcArgumentNullException1 ()
1414 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1418 [ExpectedException(typeof (ArgumentException))]
1419 public void SetCLastWriteTimeUtcArgumenException1 ()
1421 File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1425 [ExpectedException(typeof (ArgumentException))]
1426 public void SetLastWriteTimeUtcArgumenException2 ()
1428 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1432 // On Unix there are no invalid path chars.
1433 public void SetLastWriteTimeUtcArgumenException3 ()
1435 if (Path.InvalidPathChars.Length > 1) {
1438 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1439 } catch (ArgumentException) {
1443 Assertion.Assert ("#01", pass);
1448 [ExpectedException(typeof (FileNotFoundException))]
1449 public void SetLastWriteTimeUtcFileNotFoundException1 ()
1451 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
1454 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1458 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1459 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
1461 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
1462 // DeleteFile (path);
1463 // FileStream stream = null;
1465 // stream = File.Create (path);
1467 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1469 // if (stream != null)
1471 // DeleteFile (path);
1476 [ExpectedException(typeof (IOException))]
1477 public void SetLastWriteTimeUtcIOException1 ()
1479 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
1481 FileStream stream = null;
1483 stream = File.Create (path);
1484 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1493 public void OpenAppend ()
1495 string fn = Path.GetTempFileName ();
1496 using (FileStream s = File.Open (fn, FileMode.Append))
1502 private void DeleteFile (string path)
1504 if (File.Exists (path))