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
15 using System.Globalization;
16 using System.Threading;
18 using NUnit.Framework;
20 namespace MonoTests.System.IO
25 CultureInfo old_culture;
26 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
31 DeleteDirectory (TempFolder);
32 Directory.CreateDirectory (TempFolder);
33 old_culture = Thread.CurrentThread.CurrentCulture;
34 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
38 public void TearDown ()
40 DeleteDirectory (TempFolder);
41 Thread.CurrentThread.CurrentCulture = old_culture;
45 public void TestExists ()
48 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
50 Assert.IsFalse (File.Exists (null), "#1");
51 Assert.IsFalse (File.Exists (string.Empty), "#2");
52 Assert.IsFalse (File.Exists (" \t\t \t \n\t\n \n"), "#3");
54 s = File.Create (path);
56 Assert.IsTrue (File.Exists (path), "#4");
57 Assert.IsFalse (File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
66 public void Exists_InvalidFileName ()
68 Assert.IsFalse (File.Exists ("><|"), "#1");
69 Assert.IsFalse (File.Exists ("?*"), "#2");
73 public void Exists_InvalidDirectory ()
75 Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
79 public void Create_Path_Null ()
84 } catch (ArgumentNullException ex) {
85 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
86 Assert.IsNull (ex.InnerException, "#3");
87 Assert.IsNotNull (ex.Message, "#4");
88 Assert.AreEqual ("path", ex.ParamName, "#5");
93 public void Create_Path_Directory ()
95 string path = Path.Combine (TempFolder, "foo");
96 Directory.CreateDirectory (path);
100 } catch (UnauthorizedAccessException ex) {
101 // Access to the path '...' is denied
102 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
103 Assert.IsNull (ex.InnerException, "#3");
104 Assert.IsNotNull (ex.Message, "#4");
105 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
107 DeleteDirectory (path);
112 public void Create_Path_Empty ()
115 File.Create (string.Empty);
117 } catch (ArgumentException ex) {
118 // Empty file name is not legal
119 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
120 Assert.IsNull (ex.InnerException, "#3");
121 Assert.IsNotNull (ex.Message, "#4");
122 Assert.IsNull (ex.ParamName, "#5");
127 public void Create_Path_ReadOnly ()
129 string path = Path.Combine (TempFolder, "foo");
130 File.Create (path).Close ();
131 File.SetAttributes (path, FileAttributes.ReadOnly);
135 } catch (UnauthorizedAccessException ex) {
136 // Access to the path '...' is denied
137 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
138 Assert.IsNull (ex.InnerException, "#3");
139 Assert.IsNotNull (ex.Message, "#4");
140 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
142 File.SetAttributes (path, FileAttributes.Normal);
147 public void Create_Path_Whitespace ()
152 } catch (ArgumentException ex) {
153 // The path is not of a legal form
154 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
155 Assert.IsNull (ex.InnerException, "#3");
156 Assert.IsNotNull (ex.Message, "#4");
157 Assert.IsNull (ex.ParamName, "#5");
162 public void Create_Directory_DoesNotExist ()
164 FileStream stream = null;
165 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
168 stream = File.Create (path);
170 } catch (DirectoryNotFoundException ex) {
171 // Could not find a part of the path "..."
172 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
173 Assert.IsNull (ex.InnerException, "#3");
174 Assert.IsNotNull (ex.Message, "#4");
175 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
184 public void Create ()
186 FileStream stream = null;
189 /* positive test: create resources/foo */
190 path = TempFolder + Path.DirectorySeparatorChar + "foo";
193 stream = File.Create (path);
194 Assert.IsTrue (File.Exists (path), "#1");
204 /* positive test: repeat test above again to test for overwriting file */
205 path = TempFolder + Path.DirectorySeparatorChar + "foo";
207 stream = File.Create (path);
208 Assert.IsTrue (File.Exists (path), "#2");
218 public void Copy_SourceFileName_Null ()
221 File.Copy (null, "b");
223 } catch (ArgumentNullException ex) {
224 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
225 Assert.IsNull (ex.InnerException, "#3");
226 Assert.IsNotNull (ex.Message, "#4");
227 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
232 public void Copy_DestFileName_Null ()
235 File.Copy ("a", null);
237 } catch (ArgumentNullException ex) {
238 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
239 Assert.IsNull (ex.InnerException, "#3");
240 Assert.IsNotNull (ex.Message, "#4");
241 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
246 public void Copy_SourceFileName_Empty ()
249 File.Copy (string.Empty, "b");
251 } catch (ArgumentException ex) {
252 // Empty file name is not legal
253 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
254 Assert.IsNull (ex.InnerException, "#3");
255 Assert.IsNotNull (ex.Message, "#4");
256 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
261 public void Copy_DestFileName_Empty ()
264 File.Copy ("a", string.Empty);
266 } catch (ArgumentException ex) {
267 // Empty file name is not legal
268 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
269 Assert.IsNull (ex.InnerException, "#3");
270 Assert.IsNotNull (ex.Message, "#4");
271 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
276 public void Copy_SourceFileName_Whitespace ()
279 File.Copy (" ", "b");
281 } catch (ArgumentException ex) {
282 // The path is not of a legal form
283 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
284 Assert.IsNull (ex.InnerException, "#3");
285 Assert.IsNotNull (ex.Message, "#4");
286 Assert.IsNull (ex.ParamName, "#5");
291 public void Copy_DestFileName_Whitespace ()
294 File.Copy ("a", " ");
296 } catch (ArgumentException ex) {
297 // The path is not of a legal form
298 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
299 Assert.IsNull (ex.InnerException, "#3");
300 Assert.IsNotNull (ex.Message, "#4");
301 Assert.IsNull (ex.ParamName, "#5");
306 public void Copy_SourceFileName_DoesNotExist ()
309 File.Copy ("doesnotexist", "b");
311 } catch (FileNotFoundException ex) {
312 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
313 Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
314 Assert.IsNull (ex.InnerException, "#4");
315 Assert.IsNotNull (ex.Message, "#5");
320 public void Copy_DestFileName_AlreadyExists ()
322 string source = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
323 string dest = TempFolder + Path.DirectorySeparatorChar + "bar";
327 File.Create (source).Close ();
328 File.Copy (source, dest);
330 File.Copy (source, dest);
332 } catch (IOException ex) {
333 // The file '...' already exists.
334 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
335 Assert.IsNull (ex.InnerException, "#3");
336 Assert.IsNotNull (ex.Message, "#4");
337 Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
346 public void Copy_SourceFileName_DestFileName_Same ()
348 string source = TempFolder + Path.DirectorySeparatorChar + "SameFile.txt";
352 File.Create (source).Close ();
354 File.Copy (source, source, true);
356 } catch (IOException ex) {
357 // process cannot access file ... because it is being used by another process
358 Assert.IsNull (ex.InnerException, "#2");
359 Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
369 string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
370 string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
371 /* positive test: copy resources/AFile.txt to resources/bar */
376 File.Create (path2).Close ();
377 File.Copy (path2, path1);
378 Assert.IsTrue (File.Exists (path2), "#A1");
379 Assert.IsTrue (File.Exists (path1), "#A2");
381 Assert.IsTrue (File.Exists (path1), "#B1");
382 File.Copy (path2, path1, true);
383 Assert.IsTrue (File.Exists (path2), "#B2");
384 Assert.IsTrue (File.Exists (path1), "#B3");
392 public void Delete_Path_Null ()
397 } catch (ArgumentNullException ex) {
398 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
399 Assert.IsNull (ex.InnerException, "#3");
400 Assert.IsNotNull (ex.Message, "#4");
401 Assert.AreEqual ("path", ex.ParamName, "#5");
406 public void Delete_Path_Empty ()
409 File.Delete (string.Empty);
411 } catch (ArgumentException ex) {
412 // Empty file name is not legal
413 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
414 Assert.IsNull (ex.InnerException, "#3");
415 Assert.IsNotNull (ex.Message, "#4");
416 Assert.IsNull (ex.ParamName, "#5");
421 public void Delete_Path_Whitespace ()
426 } catch (ArgumentException ex) {
427 // The path is not of a legal form
428 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
429 Assert.IsNull (ex.InnerException, "#3");
430 Assert.IsNotNull (ex.Message, "#4");
431 Assert.IsNull (ex.ParamName, "#5");
436 public void Delete_Directory_DoesNotExist ()
438 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
439 if (Directory.Exists (path))
440 Directory.Delete (path, true);
445 } catch (DirectoryNotFoundException ex) {
446 // Could not find a part of the path "..."
447 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
448 Assert.IsNull (ex.InnerException, "#3");
449 Assert.IsNotNull (ex.Message, "#4");
450 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
455 public void Delete ()
457 string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
458 DeleteFile (foopath);
460 File.Create (foopath).Close ();
461 File.Delete (foopath);
462 Assert.IsFalse (File.Exists (foopath));
463 File.Delete (foopath);
465 DeleteFile (foopath);
469 [Test] // bug #323389
470 [Category ("NotWorking")]
471 public void Delete_FileLock ()
473 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
475 FileStream stream = null;
477 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
481 } catch (IOException ex) {
482 // The process cannot access the file '...'
483 // because it is being used by another process
484 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
485 Assert.IsNull (ex.InnerException, "#3");
486 Assert.IsNotNull (ex.Message, "#4");
487 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
497 [ExpectedException (typeof(UnauthorizedAccessException))]
498 public void Delete_File_ReadOnly ()
501 Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
503 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
506 File.Create (path).Close ();
507 File.SetAttributes (path, FileAttributes.ReadOnly);
510 File.SetAttributes (path, FileAttributes.Normal);
516 public void GetAttributes_Archive ()
519 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
521 FileAttributes attrs;
523 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
524 File.Create (path).Close ();
526 attrs = File.GetAttributes (path);
527 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
529 attrs &= ~FileAttributes.Archive;
530 File.SetAttributes (path, attrs);
532 attrs = File.GetAttributes (path);
533 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
537 public void GetAttributes_Default_File ()
540 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
542 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
543 File.Create (path).Close ();
545 FileAttributes attrs = File.GetAttributes (path);
547 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
548 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
549 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
550 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
551 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
552 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
556 public void GetAttributes_Default_Directory ()
558 FileAttributes attrs = File.GetAttributes (TempFolder);
560 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
561 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
562 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
563 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
564 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
565 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
569 public void GetAttributes_Directory ()
571 FileAttributes attrs = File.GetAttributes (TempFolder);
573 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
575 attrs &= ~FileAttributes.Directory;
576 File.SetAttributes (TempFolder, attrs);
578 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
580 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
581 File.Create (path).Close ();
583 attrs = File.GetAttributes (path);
584 attrs |= FileAttributes.Directory;
585 File.SetAttributes (path, attrs);
587 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
591 public void GetAttributes_ReadOnly ()
593 FileAttributes attrs;
595 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
596 File.Create (path).Close ();
598 attrs = File.GetAttributes (path);
599 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
602 attrs |= FileAttributes.ReadOnly;
603 File.SetAttributes (path, attrs);
605 attrs = File.GetAttributes (path);
606 Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
608 File.SetAttributes (path, FileAttributes.Normal);
613 public void GetAttributes_System ()
616 Assert.Ignore ("FileAttributes.System is not supported on Unix.");
618 FileAttributes attrs;
620 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
621 File.Create (path).Close ();
623 attrs = File.GetAttributes (path);
624 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
626 attrs |= FileAttributes.System;
627 File.SetAttributes (path, FileAttributes.System);
629 attrs = File.GetAttributes (path);
630 Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
634 public void GetAttributes_Path_DoesNotExist ()
636 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
638 File.GetAttributes (path);
640 } catch (FileNotFoundException ex) {
641 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
642 Assert.AreEqual (path, ex.FileName, "#3");
643 Assert.IsNull (ex.InnerException, "#4");
644 Assert.IsNotNull (ex.Message, "#5");
649 public void GetAttributes_Path_Empty ()
652 File.GetAttributes (string.Empty);
654 } catch (ArgumentException ex) {
655 // Empty file name is not legal
656 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
657 Assert.IsNull (ex.InnerException, "#3");
658 Assert.IsNotNull (ex.Message, "#4");
659 Assert.IsNull (ex.ParamName, "#5");
664 public void GetAttributes_Path_Null ()
667 File.GetAttributes (null);
669 } catch (ArgumentNullException ex) {
670 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
671 Assert.IsNull (ex.InnerException, "#3");
672 Assert.IsNotNull (ex.Message, "#4");
673 Assert.AreEqual ("path", ex.ParamName, "#5");
678 public void Move_SourceFileName_Null ()
681 File.Move (null, "b");
683 } catch (ArgumentNullException ex) {
684 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
685 Assert.IsNull (ex.InnerException, "#3");
686 Assert.IsNotNull (ex.Message, "#4");
687 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
692 public void Move_DestFileName_Null ()
695 File.Move ("a", null);
697 } catch (ArgumentNullException ex) {
698 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
699 Assert.IsNull (ex.InnerException, "#3");
700 Assert.IsNotNull (ex.Message, "#4");
701 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
706 public void Move_SourceFileName_Empty ()
709 File.Move (string.Empty, "b");
711 } catch (ArgumentException ex) {
712 // Empty file name is not legal
713 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
714 Assert.IsNull (ex.InnerException, "#3");
715 Assert.IsNotNull (ex.Message, "#4");
716 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
721 public void Move_DestFileName_Empty ()
724 File.Move ("a", string.Empty);
726 } catch (ArgumentException ex) {
727 // Empty file name is not legal
728 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
729 Assert.IsNull (ex.InnerException, "#3");
730 Assert.IsNotNull (ex.Message, "#4");
731 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
736 public void Move_SourceFileName_Whitespace ()
739 File.Move (" ", "b");
741 } catch (ArgumentException ex) {
742 // The path is not of a legal form
743 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
744 Assert.IsNull (ex.InnerException, "#3");
745 Assert.IsNotNull (ex.Message, "#4");
746 Assert.IsNull (ex.ParamName, "#5");
751 public void Move_DestFileName_Whitespace ()
754 File.Move ("a", " ");
756 } catch (ArgumentException ex) {
757 // The path is not of a legal form
758 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
759 Assert.IsNull (ex.InnerException, "#3");
760 Assert.IsNotNull (ex.Message, "#4");
761 Assert.IsNull (ex.ParamName, "#5");
766 public void Move_SourceFileName_DoesNotExist ()
768 string file = TempFolder + Path.DirectorySeparatorChar + "doesnotexist";
771 File.Move (file, "b");
773 } catch (FileNotFoundException ex) {
774 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
775 Assert.AreEqual (file, ex.FileName, "#3");
776 Assert.IsNull (ex.InnerException, "#4");
777 Assert.IsNotNull (ex.Message, "#5");
782 public void Move_DestFileName_DirectoryDoesNotExist ()
784 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
785 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
786 DeleteFile (sourceFile);
788 File.Create (sourceFile).Close ();
790 File.Move (sourceFile, destFile);
792 } catch (DirectoryNotFoundException ex) {
793 // Could not find a part of the path
794 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
795 Assert.IsNull (ex.InnerException, "#3");
796 Assert.IsNotNull (ex.Message, "#4");
798 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
800 Assert.IsTrue (ex.Message.IndexOf (destFile) != -1, "#5");
804 DeleteFile (sourceFile);
809 public void Move_DestFileName_AlreadyExists ()
811 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
814 // move to same directory
815 File.Create (sourceFile).Close ();
817 File.Move (sourceFile, TempFolder);
819 } catch (IOException ex) {
820 // Cannot create a file when that file already exists
821 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
822 Assert.IsNull (ex.InnerException, "#A3");
823 Assert.IsNotNull (ex.Message, "#A4");
824 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
825 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
827 DeleteFile (sourceFile);
830 // move to exist file
831 File.Create (sourceFile).Close ();
832 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
833 File.Create (destFile).Close ();
835 File.Move (sourceFile, destFile);
837 } catch (IOException ex) {
838 // Cannot create a file when that file already exists
839 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
840 Assert.IsNull (ex.InnerException, "#B3");
841 Assert.IsNotNull (ex.Message, "#B4");
842 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
843 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
845 DeleteFile (sourceFile);
846 DeleteFile (destFile);
849 // move to existing directory
850 File.Create (sourceFile).Close ();
851 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
852 Directory.CreateDirectory (destFile);
854 File.Move (sourceFile, destFile);
856 } catch (IOException ex) {
857 // Cannot create a file when that file already exists
858 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
859 Assert.IsNull (ex.InnerException, "#C3");
860 Assert.IsNotNull (ex.Message, "#C4");
861 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
862 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
864 DeleteFile (sourceFile);
865 DeleteDirectory (destFile);
872 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
873 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
874 if (!File.Exists (bar)) {
875 FileStream f = File.Create(bar);
879 Assert.IsTrue (File.Exists (bar), "#1");
880 File.Move (bar, baz);
881 Assert.IsFalse (File.Exists (bar), "#2");
882 Assert.IsTrue (File.Exists (baz), "#3");
884 // Test moving of directories
885 string dir = Path.Combine (TempFolder, "dir");
886 string dir2 = Path.Combine (TempFolder, "dir2");
887 string dir_foo = Path.Combine (dir, "foo");
888 string dir2_foo = Path.Combine (dir2, "foo");
890 if (Directory.Exists (dir))
891 Directory.Delete (dir, true);
893 Directory.CreateDirectory (dir);
894 Directory.CreateDirectory (dir2);
895 File.Create (dir_foo).Close ();
896 File.Move (dir_foo, dir2_foo);
897 Assert.IsTrue (File.Exists (dir2_foo), "#4");
899 Directory.Delete (dir, true);
900 Directory.Delete (dir2, true);
901 DeleteFile (dir_foo);
902 DeleteFile (dir2_foo);
906 public void Move_FileLock ()
908 string sourceFile = Path.GetTempFileName ();
909 string destFile = Path.GetTempFileName ();
911 // source file locked
912 using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
914 File.Move (sourceFile, destFile);
916 } catch (IOException ex) {
917 // The process cannot access the file because
918 // it is being used by another process
919 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
920 Assert.IsNull (ex.InnerException, "#A3");
921 Assert.IsNotNull (ex.Message, "#A4");
925 // destination file locked
926 using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
928 File.Move (sourceFile, destFile);
930 } catch (IOException ex) {
931 // The process cannot access the file because
932 // it is being used by another process
933 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
934 Assert.IsNull (ex.InnerException, "#B3");
935 Assert.IsNotNull (ex.Message, "#B4");
944 FileStream stream = null;
946 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
948 if (!File.Exists (path))
949 stream = File.Create (path);
951 stream = File.Open (path, FileMode.Open);
961 if (!File.Exists (path))
962 File.Create (path).Close ();
964 stream = File.Open (path, FileMode.Open);
965 Assert.IsTrue (stream.CanRead, "#A1");
966 Assert.IsTrue (stream.CanSeek, "#A2");
967 Assert.IsTrue (stream.CanWrite, "#A3");
970 stream = File.Open (path, FileMode.Open, FileAccess.Write);
971 Assert.IsFalse (stream.CanRead, "#B1");
972 Assert.IsTrue (stream.CanSeek, "#B2");
973 Assert.IsTrue (stream.CanWrite, "#B3");
976 stream = File.Open (path, FileMode.Open, FileAccess.Read);
977 Assert.IsTrue (stream.CanRead, "#C1");
978 Assert.IsTrue (stream.CanSeek, "#C2");
979 Assert.IsFalse (stream.CanWrite, "#C3");
989 /* Exception tests */
990 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
992 stream = File.Open (path, FileMode.Open);
994 } catch (FileNotFoundException ex) {
995 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
996 Assert.AreEqual (path, ex.FileName, "#D3");
997 Assert.IsNull (ex.InnerException, "#D4");
998 Assert.IsNotNull (ex.Message, "#D5");
1007 public void Open_CreateNewMode_ReadAccess ()
1009 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1010 FileStream stream = null;
1012 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1014 } catch (ArgumentException ex) {
1015 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1016 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1017 Assert.IsNull (ex.InnerException, "#3");
1018 Assert.IsNotNull (ex.Message, "#4");
1019 Assert.IsNull (ex.ParamName, "#5");
1028 public void Open_AppendMode_ReadAccess ()
1030 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1031 FileStream s = null;
1032 if (!File.Exists (path))
1033 File.Create (path).Close ();
1035 s = File.Open (path, FileMode.Append, FileAccess.Read);
1037 } catch (ArgumentException ex) {
1038 // Combining FileMode: Append with FileAccess: Read is invalid
1039 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1040 Assert.IsNull (ex.InnerException, "#3");
1041 Assert.IsNotNull (ex.Message, "#4");
1042 Assert.IsNull (ex.ParamName, "#5");
1051 public void OpenRead ()
1053 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1054 if (!File.Exists (path))
1055 File.Create (path).Close ();
1056 FileStream stream = null;
1059 stream = File.OpenRead (path);
1060 Assert.IsTrue (stream.CanRead, "#1");
1061 Assert.IsTrue (stream.CanSeek, "#2");
1062 Assert.IsFalse (stream.CanWrite, "#3");
1071 public void OpenWrite ()
1073 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1074 if (!File.Exists (path))
1075 File.Create (path).Close ();
1076 FileStream stream = null;
1079 stream = File.OpenWrite (path);
1080 Assert.IsFalse (stream.CanRead, "#1");
1081 Assert.IsTrue (stream.CanSeek, "#2");
1082 Assert.IsTrue (stream.CanWrite, "#3");
1092 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1093 public void TestGetCreationTime ()
1095 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1099 File.Create (path).Close();
1100 DateTime time = File.GetCreationTime (path);
1101 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1108 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1109 public void CreationTime ()
1112 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1114 string path = Path.GetTempFileName ();
1116 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1117 DateTime time = File.GetCreationTime (path);
1118 Assert.AreEqual (2002, time.Year, "#A1");
1119 Assert.AreEqual (4, time.Month, "#A2");
1120 Assert.AreEqual (6, time.Day, "#A3");
1121 Assert.AreEqual (4, time.Hour, "#A4");
1122 Assert.AreEqual (4, time.Second, "#A5");
1124 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1125 Assert.AreEqual (2002, time.Year, "#B1");
1126 Assert.AreEqual (4, time.Month, "#B2");
1127 Assert.AreEqual (6, time.Day, "#B3");
1128 Assert.AreEqual (4, time.Hour, "#B4");
1129 Assert.AreEqual (4, time.Second, "#B5");
1131 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1132 time = File.GetCreationTimeUtc (path);
1133 Assert.AreEqual (2002, time.Year, "#C1");
1134 Assert.AreEqual (4, time.Month, "#C2");
1135 Assert.AreEqual (6, time.Day, "#C3");
1136 Assert.AreEqual (4, time.Hour, "#C4");
1137 Assert.AreEqual (4, time.Second, "#C5");
1139 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1140 Assert.AreEqual (2002, time.Year, "#D1");
1141 Assert.AreEqual (4, time.Month, "#D2");
1142 Assert.AreEqual (6, time.Day, "#D3");
1143 Assert.AreEqual (4, time.Hour, "#D4");
1144 Assert.AreEqual (4, time.Second, "#D5");
1151 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1152 public void LastAccessTime ()
1154 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1155 if (File.Exists (path))
1157 FileStream stream = null;
1159 stream = File.Create (path);
1162 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1163 DateTime time = File.GetLastAccessTime (path);
1164 Assert.AreEqual (2002, time.Year, "#A1");
1165 Assert.AreEqual (4, time.Month, "#A2");
1166 Assert.AreEqual (6, time.Day, "#A3");
1167 Assert.AreEqual (4, time.Hour, "#A4");
1168 Assert.AreEqual (4, time.Second, "#A5");
1170 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1171 Assert.AreEqual (2002, time.Year, "#B1");
1172 Assert.AreEqual (4, time.Month, "#B2");
1173 Assert.AreEqual (6, time.Day, "#B3");
1174 Assert.AreEqual (4, time.Hour, "#B4");
1175 Assert.AreEqual (4, time.Second, "#B5");
1177 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1178 time = File.GetLastAccessTimeUtc (path);
1179 Assert.AreEqual (2002, time.Year, "#C1");
1180 Assert.AreEqual (4, time.Month, "#C2");
1181 Assert.AreEqual (6, time.Day, "#C3");
1182 Assert.AreEqual (4, time.Hour, "#C4");
1183 Assert.AreEqual (4, time.Second, "#C5");
1185 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1186 Assert.AreEqual (2002, time.Year, "#D1");
1187 Assert.AreEqual (4, time.Month, "#D2");
1188 Assert.AreEqual (6, time.Day, "#D3");
1189 Assert.AreEqual (4, time.Hour, "#D4");
1190 Assert.AreEqual (4, time.Second, "#D5");
1199 public void LastWriteTime ()
1201 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1202 if (File.Exists (path))
1204 FileStream stream = null;
1206 stream = File.Create (path);
1209 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1210 DateTime time = File.GetLastWriteTime (path);
1211 Assert.AreEqual (2002, time.Year, "#A1");
1212 Assert.AreEqual (4, time.Month, "#A2");
1213 Assert.AreEqual (6, time.Day, "#A3");
1214 Assert.AreEqual (4, time.Hour, "#A4");
1215 Assert.AreEqual (4, time.Second, "#A5");
1217 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1218 Assert.AreEqual (2002, time.Year, "#B1");
1219 Assert.AreEqual (4, time.Month, "#B2");
1220 Assert.AreEqual (6, time.Day, "#B3");
1221 Assert.AreEqual (4, time.Hour, "#B4");
1222 Assert.AreEqual (4, time.Second, "#B5");
1224 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1225 time = File.GetLastWriteTimeUtc (path);
1226 Assert.AreEqual (2002, time.Year, "#C1");
1227 Assert.AreEqual (4, time.Month, "#C2");
1228 Assert.AreEqual (6, time.Day, "#C3");
1229 Assert.AreEqual (4, time.Hour, "#C4");
1230 Assert.AreEqual (4, time.Second, "#C5");
1232 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1233 Assert.AreEqual (2002, time.Year, "#D1");
1234 Assert.AreEqual (4, time.Month, "#D2");
1235 Assert.AreEqual (6, time.Day, "#D3");
1236 Assert.AreEqual (4, time.Hour, "#D4");
1237 Assert.AreEqual (4, time.Second, "#D5");
1246 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1247 public void GetCreationTime_Path_Null ()
1250 File.GetCreationTime (null as string);
1252 } catch (ArgumentNullException ex) {
1253 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1254 Assert.IsNull (ex.InnerException, "#3");
1255 Assert.IsNotNull (ex.Message, "#4");
1256 Assert.AreEqual ("path", ex.ParamName, "#5");
1261 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1262 public void GetCreationTime_Path_Empty ()
1265 File.GetCreationTime (string.Empty);
1267 } catch (ArgumentException ex) {
1268 // Empty file name is not legal
1269 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1270 Assert.IsNull (ex.InnerException, "#3");
1271 Assert.IsNotNull (ex.Message, "#4");
1272 Assert.IsNull (ex.ParamName, "#5");
1277 //[Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1278 public void GetCreationTime_Path_DoesNotExist ()
1280 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1284 DateTime time = File.GetCreationTime (path);
1285 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1286 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1287 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1288 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1289 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1290 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1291 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1294 File.GetCreationTime (path);
1296 } catch (IOException ex) {
1297 // Could not find a part of the path "..."
1298 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1299 Assert.IsNull (ex.InnerException, "#3");
1300 Assert.IsNotNull (ex.Message, "#4");
1301 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1307 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1308 public void GetCreationTime_Path_Whitespace ()
1311 File.GetCreationTime (" ");
1313 } catch (ArgumentException ex) {
1314 // The path is not of a legal form
1315 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1316 Assert.IsNull (ex.InnerException, "#3");
1317 Assert.IsNotNull (ex.Message, "#4");
1318 Assert.IsNull (ex.ParamName, "#5");
1323 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1324 public void GetCreationTime_Path_InvalidPathChars ()
1327 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1329 } catch (ArgumentException ex) {
1330 // Illegal characters in path
1331 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1332 Assert.IsNull (ex.InnerException, "#3");
1333 Assert.IsNotNull (ex.Message, "#4");
1334 Assert.IsNull (ex.ParamName, "#5");
1339 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1340 public void GetCreationTimeUtc_Path_Null ()
1343 File.GetCreationTimeUtc (null as string);
1345 } catch (ArgumentNullException ex) {
1346 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1347 Assert.IsNull (ex.InnerException, "#3");
1348 Assert.IsNotNull (ex.Message, "#4");
1349 Assert.AreEqual ("path", ex.ParamName, "#5");
1354 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1355 public void GetCreationTimeUtc_Path_Empty ()
1358 File.GetCreationTimeUtc (string.Empty);
1360 } catch (ArgumentException ex) {
1361 // Empty file name is not legal
1362 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1363 Assert.IsNull (ex.InnerException, "#3");
1364 Assert.IsNotNull (ex.Message, "#4");
1365 Assert.IsNull (ex.ParamName, "#5");
1370 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1371 public void GetCreationTimeUtc_Path_DoesNotExist ()
1373 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1377 DateTime time = File.GetCreationTimeUtc (path);
1378 Assert.AreEqual (1601, time.Year, "#1");
1379 Assert.AreEqual (1, time.Month, "#2");
1380 Assert.AreEqual (1, time.Day, "#3");
1381 Assert.AreEqual (0, time.Hour, "#4");
1382 Assert.AreEqual (0, time.Second, "#5");
1383 Assert.AreEqual (0, time.Millisecond, "#6");
1386 File.GetCreationTimeUtc (path);
1388 } catch (IOException ex) {
1389 // Could not find a part of the path "..."
1390 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1391 Assert.IsNull (ex.InnerException, "#3");
1392 Assert.IsNotNull (ex.Message, "#4");
1393 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1399 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1400 public void GetCreationTimeUtc_Path_Whitespace ()
1403 File.GetCreationTimeUtc (" ");
1405 } catch (ArgumentException ex) {
1406 // The path is not of a legal form
1407 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1408 Assert.IsNull (ex.InnerException, "#3");
1409 Assert.IsNotNull (ex.Message, "#4");
1410 Assert.IsNull (ex.ParamName, "#5");
1415 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1416 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1419 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1421 } catch (ArgumentException ex) {
1422 // Illegal characters in path
1423 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1424 Assert.IsNull (ex.InnerException, "#3");
1425 Assert.IsNotNull (ex.Message, "#4");
1426 Assert.IsNull (ex.ParamName, "#5");
1431 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1432 public void GetLastAccessTime_Path_Null ()
1435 File.GetLastAccessTime (null as string);
1437 } catch (ArgumentNullException ex) {
1438 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1439 Assert.IsNull (ex.InnerException, "#3");
1440 Assert.IsNotNull (ex.Message, "#4");
1441 Assert.AreEqual ("path", ex.ParamName, "#5");
1446 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1447 public void GetLastAccessTime_Path_Empty ()
1450 File.GetLastAccessTime (string.Empty);
1452 } catch (ArgumentException ex) {
1453 // Empty file name is not legal
1454 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1455 Assert.IsNull (ex.InnerException, "#3");
1456 Assert.IsNotNull (ex.Message, "#4");
1457 Assert.IsNull (ex.ParamName, "#5");
1462 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1463 public void GetLastAccessTime_Path_DoesNotExist ()
1465 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1469 DateTime time = File.GetLastAccessTime (path);
1470 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1471 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1472 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1473 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1474 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1475 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1476 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1479 File.GetLastAccessTime (path);
1481 } catch (IOException ex) {
1482 // Could not find a part of the path "..."
1483 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1484 Assert.IsNull (ex.InnerException, "#3");
1485 Assert.IsNotNull (ex.Message, "#4");
1486 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1492 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1493 public void GetLastAccessTime_Path_Whitespace ()
1496 File.GetLastAccessTime (" ");
1498 } catch (ArgumentException ex) {
1499 // The path is not of a legal form
1500 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1501 Assert.IsNull (ex.InnerException, "#3");
1502 Assert.IsNotNull (ex.Message, "#4");
1503 Assert.IsNull (ex.ParamName, "#5");
1508 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1509 public void GetLastAccessTime_Path_InvalidPathChars ()
1512 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1514 } catch (ArgumentException ex) {
1515 // Illegal characters in path
1516 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1517 Assert.IsNull (ex.InnerException, "#3");
1518 Assert.IsNotNull (ex.Message, "#4");
1519 Assert.IsNull (ex.ParamName, "#5");
1524 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1525 public void GetLastAccessTimeUtc_Path_Null ()
1528 File.GetLastAccessTimeUtc (null as string);
1530 } catch (ArgumentNullException ex) {
1531 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1532 Assert.IsNull (ex.InnerException, "#3");
1533 Assert.IsNotNull (ex.Message, "#4");
1534 Assert.AreEqual ("path", ex.ParamName, "#5");
1539 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1540 public void GetLastAccessTimeUtc_Path_Empty ()
1543 File.GetLastAccessTimeUtc (string.Empty);
1545 } catch (ArgumentException ex) {
1546 // Empty file name is not legal
1547 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1548 Assert.IsNull (ex.InnerException, "#3");
1549 Assert.IsNotNull (ex.Message, "#4");
1550 Assert.IsNull (ex.ParamName, "#5");
1555 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1556 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1558 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1562 DateTime time = File.GetLastAccessTimeUtc (path);
1563 Assert.AreEqual (1601, time.Year, "#1");
1564 Assert.AreEqual (1, time.Month, "#2");
1565 Assert.AreEqual (1, time.Day, "#3");
1566 Assert.AreEqual (0, time.Hour, "#4");
1567 Assert.AreEqual (0, time.Second, "#5");
1568 Assert.AreEqual (0, time.Millisecond, "#6");
1571 File.GetLastAccessTimeUtc (path);
1573 } catch (IOException ex) {
1574 // Could not find a part of the path "..."
1575 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1576 Assert.IsNull (ex.InnerException, "#3");
1577 Assert.IsNotNull (ex.Message, "#4");
1578 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1584 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1585 public void GetLastAccessTimeUtc_Path_Whitespace ()
1588 File.GetLastAccessTimeUtc (" ");
1590 } catch (ArgumentException ex) {
1591 // The path is not of a legal form
1592 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1593 Assert.IsNull (ex.InnerException, "#3");
1594 Assert.IsNotNull (ex.Message, "#4");
1595 Assert.IsNull (ex.ParamName, "#5");
1600 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1601 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1604 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1606 } catch (ArgumentException ex) {
1607 // Illegal characters in path
1608 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1609 Assert.IsNull (ex.InnerException, "#3");
1610 Assert.IsNotNull (ex.Message, "#4");
1611 Assert.IsNull (ex.ParamName, "#5");
1616 public void GetLastWriteTime_Path_Null ()
1619 File.GetLastWriteTime (null as string);
1621 } catch (ArgumentNullException ex) {
1622 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1623 Assert.IsNull (ex.InnerException, "#3");
1624 Assert.IsNotNull (ex.Message, "#4");
1625 Assert.AreEqual ("path", ex.ParamName, "#5");
1630 public void GetLastWriteTime_Path_Empty ()
1633 File.GetLastWriteTime (string.Empty);
1635 } catch (ArgumentException ex) {
1636 // Empty file name is not legal
1637 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1638 Assert.IsNull (ex.InnerException, "#3");
1639 Assert.IsNotNull (ex.Message, "#4");
1640 Assert.IsNull (ex.ParamName, "#5");
1645 public void GetLastWriteTime_Path_DoesNotExist ()
1647 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1651 DateTime time = File.GetLastWriteTime (path);
1652 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1653 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1654 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1655 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1656 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1657 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1658 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1661 File.GetLastWriteTime (path);
1663 } catch (IOException ex) {
1664 // Could not find a part of the path "..."
1665 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1666 Assert.IsNull (ex.InnerException, "#3");
1667 Assert.IsNotNull (ex.Message, "#4");
1668 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1674 public void GetLastWriteTime_Path_Whitespace ()
1677 File.GetLastWriteTime (" ");
1679 } catch (ArgumentException ex) {
1680 // The path is not of a legal form
1681 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1682 Assert.IsNull (ex.InnerException, "#3");
1683 Assert.IsNotNull (ex.Message, "#4");
1684 Assert.IsNull (ex.ParamName, "#5");
1689 public void GetLastWriteTime_Path_InvalidPathChars ()
1692 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1694 } catch (ArgumentException ex) {
1695 // Illegal characters in path
1696 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1697 Assert.IsNull (ex.InnerException, "#3");
1698 Assert.IsNotNull (ex.Message, "#4");
1699 Assert.IsNull (ex.ParamName, "#5");
1704 public void GetLastWriteTimeUtc_Path_Null ()
1707 File.GetLastWriteTimeUtc (null as string);
1709 } catch (ArgumentNullException ex) {
1710 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1711 Assert.IsNull (ex.InnerException, "#3");
1712 Assert.IsNotNull (ex.Message, "#4");
1713 Assert.AreEqual ("path", ex.ParamName, "#5");
1718 public void GetLastWriteTimeUtc_Path_Empty ()
1721 File.GetLastWriteTimeUtc (string.Empty);
1723 } catch (ArgumentException ex) {
1724 // Empty file name is not legal
1725 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1726 Assert.IsNull (ex.InnerException, "#3");
1727 Assert.IsNotNull (ex.Message, "#4");
1728 Assert.IsNull (ex.ParamName, "#5");
1733 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1735 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1739 DateTime time = File.GetLastWriteTimeUtc (path);
1740 Assert.AreEqual (1601, time.Year, "#1");
1741 Assert.AreEqual (1, time.Month, "#2");
1742 Assert.AreEqual (1, time.Day, "#3");
1743 Assert.AreEqual (0, time.Hour, "#4");
1744 Assert.AreEqual (0, time.Second, "#5");
1745 Assert.AreEqual (0, time.Millisecond, "#6");
1748 File.GetLastWriteTimeUtc (path);
1750 } catch (IOException ex) {
1751 // Could not find a part of the path "..."
1752 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1753 Assert.IsNull (ex.InnerException, "#3");
1754 Assert.IsNotNull (ex.Message, "#4");
1755 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1761 public void GetLastWriteTimeUtc_Path_Whitespace ()
1764 File.GetLastWriteTimeUtc (" ");
1766 } catch (ArgumentException ex) {
1767 // The path is not of a legal form
1768 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1769 Assert.IsNull (ex.InnerException, "#3");
1770 Assert.IsNotNull (ex.Message, "#4");
1771 Assert.IsNull (ex.ParamName, "#5");
1776 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1779 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1781 } catch (ArgumentException ex) {
1782 // Illegal characters in path
1783 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1784 Assert.IsNull (ex.InnerException, "#3");
1785 Assert.IsNotNull (ex.Message, "#4");
1786 Assert.IsNull (ex.ParamName, "#5");
1791 public void FileStreamClose ()
1793 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1794 FileStream stream = null;
1796 stream = File.Create (path);
1806 // SetCreationTime and SetCreationTimeUtc exceptions
1809 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1810 public void SetCreationTime_Path_Null ()
1813 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1815 } catch (ArgumentNullException ex) {
1816 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1817 Assert.IsNull (ex.InnerException, "#3");
1818 Assert.IsNotNull (ex.Message, "#4");
1819 Assert.AreEqual ("path", ex.ParamName, "#5");
1824 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1825 public void SetCreationTime_Path_Empty ()
1828 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1830 } catch (ArgumentException ex) {
1831 // Empty file name is not legal
1832 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1833 Assert.IsNull (ex.InnerException, "#3");
1834 Assert.IsNotNull (ex.Message, "#4");
1835 Assert.IsNull (ex.ParamName, "#5");
1840 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1841 public void SetCreationTime_Path_Whitespace ()
1844 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1846 } catch (ArgumentException ex) {
1847 // The path is not of a legal form
1848 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1849 Assert.IsNull (ex.InnerException, "#3");
1850 Assert.IsNotNull (ex.Message, "#4");
1851 Assert.IsNull (ex.ParamName, "#5");
1856 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1857 public void SetCreationTime_Path_InvalidPathChars ()
1859 // On Unix there are no invalid path chars.
1860 if (Path.InvalidPathChars.Length > 1) {
1862 File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1863 new DateTime (2000, 12, 12, 11, 59, 59));
1865 } catch (ArgumentException ex) {
1866 // Illegal characters in path
1867 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1868 Assert.IsNull (ex.InnerException, "#3");
1869 Assert.IsNotNull (ex.Message, "#4");
1870 Assert.IsNull (ex.ParamName, "#5");
1876 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1877 public void SetCreationTime_Path_DoesNotExist ()
1879 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1883 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1885 } catch (FileNotFoundException ex) {
1886 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1887 Assert.AreEqual (path, ex.FileName, "#3");
1888 Assert.IsNull (ex.InnerException, "#4");
1889 Assert.IsNotNull (ex.Message, "#5");
1894 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1895 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1897 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1898 // FileStream stream = null;
1899 // DeleteFile (path);
1901 // stream = File.Create (path);
1903 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1905 // if (stream != null)
1907 // DeleteFile (path);
1912 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1913 public void SetCreationTime_FileLock ()
1915 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1917 FileStream stream = null;
1919 stream = File.Create (path);
1921 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1923 } catch (IOException ex) {
1924 // The process cannot access the file '...'
1925 // because it is being used by another process
1926 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1927 Assert.IsNull (ex.InnerException, "#3");
1928 Assert.IsNotNull (ex.Message, "#4");
1929 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1939 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1940 public void SetCreationTimeUtc_Path_Null ()
1943 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1945 } catch (ArgumentNullException ex) {
1946 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1947 Assert.IsNull (ex.InnerException, "#3");
1948 Assert.IsNotNull (ex.Message, "#4");
1949 Assert.AreEqual ("path", ex.ParamName, "#5");
1954 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1955 public void SetCreationTimeUtc_Path_Empty ()
1958 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1960 } catch (ArgumentException ex) {
1961 // Empty file name is not legal
1962 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1963 Assert.IsNull (ex.InnerException, "#3");
1964 Assert.IsNotNull (ex.Message, "#4");
1965 Assert.IsNull (ex.ParamName, "#5");
1970 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1971 public void SetCreationTimeUtc_Path_Whitespace ()
1974 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1976 } catch (ArgumentException ex) {
1977 // The path is not of a legal form
1978 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1979 Assert.IsNull (ex.InnerException, "#3");
1980 Assert.IsNotNull (ex.Message, "#4");
1981 Assert.IsNull (ex.ParamName, "#5");
1986 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1987 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1989 // On Unix there are no invalid path chars.
1990 if (Path.InvalidPathChars.Length > 1) {
1992 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1993 new DateTime (2000, 12, 12, 11, 59, 59));
1995 } catch (ArgumentException ex) {
1996 // Illegal characters in path
1997 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1998 Assert.IsNull (ex.InnerException, "#3");
1999 Assert.IsNotNull (ex.Message, "#4");
2000 Assert.IsNull (ex.ParamName, "#5");
2006 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
2007 public void SetCreationTimeUtc_Path_DoesNotExist ()
2009 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
2013 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2015 } catch (FileNotFoundException ex) {
2016 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2017 Assert.AreEqual (path, ex.FileName, "#3");
2018 Assert.IsNull (ex.InnerException, "#3");
2019 Assert.IsNotNull (ex.Message, "#4");
2024 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2025 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
2027 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
2028 // DeleteFile (path);
2029 // FileStream stream = null;
2031 // stream = File.Create (path);
2033 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2035 // if (stream != null)
2037 // DeleteFile (path);
2042 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
2043 public void SetCreationTimeUtc_FileLock ()
2045 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
2047 FileStream stream = null;
2049 stream = File.Create (path);
2051 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2053 } catch (IOException ex) {
2054 // The process cannot access the file "..."
2055 // because it is being used by another process
2056 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2057 Assert.IsNull (ex.InnerException, "#3");
2058 Assert.IsNotNull (ex.Message, "#4");
2059 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2068 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
2071 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2072 public void SetLastAccessTime_Path_Null ()
2075 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2077 } catch (ArgumentNullException ex) {
2078 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2079 Assert.IsNull (ex.InnerException, "#3");
2080 Assert.IsNotNull (ex.Message, "#4");
2081 Assert.AreEqual ("path", ex.ParamName, "#5");
2086 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2087 public void SetLastAccessTime_Path_Empty ()
2090 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2092 } catch (ArgumentException ex) {
2093 // Empty file name is not legal
2094 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2095 Assert.IsNull (ex.InnerException, "#3");
2096 Assert.IsNotNull (ex.Message, "#4");
2097 Assert.IsNull (ex.ParamName, "#5");
2102 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2103 public void SetLastAccessTime_Path_Whitespace ()
2106 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2108 } catch (ArgumentException ex) {
2109 // The path is not of a legal form
2110 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2111 Assert.IsNull (ex.InnerException, "#3");
2112 Assert.IsNotNull (ex.Message, "#4");
2113 Assert.IsNull (ex.ParamName, "#5");
2118 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2119 public void SetLastAccessTime_Path_InvalidPathChars ()
2121 // On Unix there are no invalid path chars.
2122 if (Path.InvalidPathChars.Length > 1) {
2124 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2125 new DateTime (2000, 12, 12, 11, 59, 59));
2127 } catch (ArgumentException ex) {
2128 // Illegal characters in path
2129 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2130 Assert.IsNull (ex.InnerException, "#3");
2131 Assert.IsNotNull (ex.Message, "#4");
2132 Assert.IsNull (ex.ParamName, "#5");
2138 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2139 public void SetLastAccessTime_Path_DoesNotExist ()
2141 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2145 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2147 } catch (FileNotFoundException ex) {
2148 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2149 Assert.AreEqual (path, ex.FileName, "#3");
2150 Assert.IsNull (ex.InnerException, "#4");
2151 Assert.IsNotNull (ex.Message, "#5");
2156 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2157 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2159 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2160 // DeleteFile (path);
2161 // FileStream stream = null;
2163 // stream = File.Create (path);
2165 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2167 // if (stream != null)
2169 // DeleteFile (path);
2174 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2175 public void SetLastAccessTime_FileLock ()
2177 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2179 FileStream stream = null;
2181 stream = File.Create (path);
2183 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2185 } catch (IOException ex) {
2186 // The process cannot access the file "..."
2187 // because it is being used by another process
2188 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2189 Assert.IsNull (ex.InnerException, "#3");
2190 Assert.IsNotNull (ex.Message, "#4");
2191 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2201 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2202 public void SetLastAccessTimeUtc_Path_Null ()
2205 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2207 } catch (ArgumentNullException ex) {
2208 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2209 Assert.IsNull (ex.InnerException, "#3");
2210 Assert.IsNotNull (ex.Message, "#4");
2211 Assert.AreEqual ("path", ex.ParamName, "#5");
2216 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2217 public void SetCLastAccessTimeUtc_Path_Empty ()
2220 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2222 } catch (ArgumentException ex) {
2223 // Empty file name is not legal
2224 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2225 Assert.IsNull (ex.InnerException, "#3");
2226 Assert.IsNotNull (ex.Message, "#4");
2227 Assert.IsNull (ex.ParamName, "#5");
2232 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2233 public void SetLastAccessTimeUtc_Path_Whitespace ()
2236 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2238 } catch (ArgumentException ex) {
2239 // The path is not of a legal form
2240 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2241 Assert.IsNull (ex.InnerException, "#3");
2242 Assert.IsNotNull (ex.Message, "#4");
2243 Assert.IsNull (ex.ParamName, "#5");
2248 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2249 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2251 // On Unix there are no invalid path chars.
2252 if (Path.InvalidPathChars.Length > 1) {
2254 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2255 new DateTime (2000, 12, 12, 11, 59, 59));
2257 } catch (ArgumentException ex) {
2258 // Illegal characters in path
2259 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2260 Assert.IsNull (ex.InnerException, "#3");
2261 Assert.IsNotNull (ex.Message, "#4");
2262 Assert.IsNull (ex.ParamName, "#5");
2268 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2269 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2271 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2275 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2277 } catch (FileNotFoundException ex) {
2278 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2279 Assert.AreEqual (path, ex.FileName, "#3");
2280 Assert.IsNull (ex.InnerException, "#4");
2281 Assert.IsNotNull (ex.Message, "#5");
2286 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2287 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2289 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2290 // DeleteFile (path);
2291 // FileStream stream = null;
2293 // stream = File.Create (path);
2295 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2297 // if (stream != null)
2299 // DeleteFile (path);
2304 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2305 public void SetLastAccessTimeUtc_FileLock ()
2307 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2309 FileStream stream = null;
2311 stream = File.Create (path);
2313 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2315 } catch (IOException ex) {
2316 // The process cannot access the file "..."
2317 // because it is being used by another process
2318 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2319 Assert.IsNull (ex.InnerException, "#3");
2320 Assert.IsNotNull (ex.Message, "#4");
2321 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2330 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2333 public void SetLastWriteTime_Path_Null ()
2336 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2338 } catch (ArgumentNullException ex) {
2339 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2340 Assert.IsNull (ex.InnerException, "#3");
2341 Assert.IsNotNull (ex.Message, "#4");
2342 Assert.AreEqual ("path", ex.ParamName, "#5");
2347 public void SetLastWriteTime_Path_Empty ()
2350 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2352 } catch (ArgumentException ex) {
2353 // Empty file name is not legal
2354 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2355 Assert.IsNull (ex.InnerException, "#3");
2356 Assert.IsNotNull (ex.Message, "#4");
2357 Assert.IsNull (ex.ParamName, "#5");
2362 public void SetLastWriteTime_Path_Whitespace ()
2365 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2367 } catch (ArgumentException ex) {
2368 // The path is not of a legal form
2369 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2370 Assert.IsNull (ex.InnerException, "#3");
2371 Assert.IsNotNull (ex.Message, "#4");
2372 Assert.IsNull (ex.ParamName, "#5");
2377 public void SetLastWriteTime_Path_InvalidPathChars ()
2379 // On Unix there are no invalid path chars.
2380 if (Path.InvalidPathChars.Length > 1) {
2382 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2383 new DateTime (2000, 12, 12, 11, 59, 59));
2385 } catch (ArgumentException ex) {
2386 // Illegal characters in path
2387 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2388 Assert.IsNull (ex.InnerException, "#3");
2389 Assert.IsNotNull (ex.Message, "#4");
2390 Assert.IsNull (ex.ParamName, "#5");
2396 public void SetLastWriteTime_Path_DoesNotExist ()
2398 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2402 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2404 } catch (FileNotFoundException ex) {
2405 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2406 Assert.AreEqual (path, ex.FileName, "#3");
2407 Assert.IsNull (ex.InnerException, "#4");
2408 Assert.IsNotNull (ex.Message, "#5");
2413 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2414 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2416 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2417 // DeleteFile (path);
2418 // FileStream stream = null;
2420 // stream = File.Create (path);
2422 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2424 // if (stream != null)
2426 // DeleteFile (path);
2431 public void SetLastWriteTime_FileLock ()
2433 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2435 FileStream stream = null;
2437 stream = File.Create (path);
2439 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2441 } catch (IOException ex) {
2442 // The process cannot access the file '...'
2443 // because it is being used by another process
2444 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2445 Assert.IsNull (ex.InnerException, "#3");
2446 Assert.IsNotNull (ex.Message, "#4");
2447 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2457 public void SetLastWriteTimeUtc_Path_Null ()
2460 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2462 } catch (ArgumentNullException ex) {
2463 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2464 Assert.IsNull (ex.InnerException, "#3");
2465 Assert.IsNotNull (ex.Message, "#4");
2466 Assert.AreEqual ("path", ex.ParamName, "#5");
2471 public void SetLastWriteTimeUtc_Path_Empty ()
2474 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2476 } catch (ArgumentException ex) {
2477 // Empty file name is not legal
2478 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2479 Assert.IsNull (ex.InnerException, "#3");
2480 Assert.IsNotNull (ex.Message, "#4");
2481 Assert.IsNull (ex.ParamName, "#5");
2486 public void SetLastWriteTimeUtc_Path_Whitespace ()
2489 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2491 } catch (ArgumentException ex) {
2492 // The path is not of a legal form
2493 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2494 Assert.IsNull (ex.InnerException, "#3");
2495 Assert.IsNotNull (ex.Message, "#4");
2496 Assert.IsNull (ex.ParamName, "#5");
2501 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2503 // On Unix there are no invalid path chars.
2504 if (Path.InvalidPathChars.Length > 1) {
2506 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2507 new DateTime (2000, 12, 12, 11, 59, 59));
2509 } catch (ArgumentException ex) {
2510 // Illegal characters in path
2511 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2512 Assert.IsNull (ex.InnerException, "#3");
2513 Assert.IsNotNull (ex.Message, "#4");
2514 Assert.IsNull (ex.ParamName, "#5");
2520 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2522 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2526 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2528 } catch (FileNotFoundException ex) {
2529 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2530 Assert.AreEqual (path, ex.FileName, "#3");
2531 Assert.IsNull (ex.InnerException, "#4");
2532 Assert.IsNotNull (ex.Message, "#5");
2537 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2538 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2540 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2541 // DeleteFile (path);
2542 // FileStream stream = null;
2544 // stream = File.Create (path);
2546 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2548 // if (stream != null)
2550 // DeleteFile (path);
2555 public void SetLastWriteTimeUtc_FileLock ()
2557 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2559 FileStream stream = null;
2561 stream = File.Create (path);
2563 File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2565 } catch (IOException ex) {
2566 // The process cannot access the file '...'
2567 // because it is being used by another process
2568 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2569 Assert.IsNull (ex.InnerException, "#3");
2570 Assert.IsNotNull (ex.Message, "#4");
2571 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2581 public void OpenAppend ()
2583 string fn = Path.GetTempFileName ();
2584 using (FileStream s = File.Open (fn, FileMode.Append)) {
2591 public void ReadWriteAllText ()
2593 // The MSDN docs said something about
2594 // not including a final new line. it looks
2595 // like that was not true. I'm not sure what
2596 // that was talking about
2597 read_all (string.Empty);
2606 read_all ("a\r\na");
2610 read_all ("\r\n\r\n");
2614 public void ReplaceTest ()
2616 string tmp = Path.Combine (TempFolder, "ReplaceTest");
2617 Directory.CreateDirectory (tmp);
2618 string origFile = Path.Combine (tmp, "origFile");
2619 string replaceFile = Path.Combine (tmp, "replaceFile");
2620 string backupFile = Path.Combine (tmp, "backupFile");
2622 using (StreamWriter sw = File.CreateText (origFile)) {
2623 sw.WriteLine ("origFile");
2625 using (StreamWriter sw = File.CreateText (replaceFile)) {
2626 sw.WriteLine ("replaceFile");
2628 using (StreamWriter sw = File.CreateText (backupFile)) {
2629 sw.WriteLine ("backupFile");
2632 File.Replace (origFile, replaceFile, backupFile);
2633 Assert.IsFalse (File.Exists (origFile), "#1");
2634 using (StreamReader sr = File.OpenText (replaceFile)) {
2635 string txt = sr.ReadLine ();
2636 Assert.AreEqual ("origFile", txt, "#2");
2638 using (StreamReader sr = File.OpenText (backupFile)) {
2639 string txt = sr.ReadLine ();
2640 Assert.AreEqual ("replaceFile", txt, "#3");
2645 static bool RunningOnUnix {
2647 int p = (int) Environment.OSVersion.Platform;
2648 return ((p == 4) || (p == 128) || (p == 6));
2652 void DeleteFile (string path)
2654 if (File.Exists (path))
2658 void DeleteDirectory (string path)
2660 if (Directory.Exists (path))
2661 Directory.Delete (path, true);
2665 void read_all (string s)
2667 string f = Path.GetTempFileName ();
2669 File.WriteAllText (f, s);
2670 string r = File.ReadAllText (f);
2671 Assert.AreEqual (s, r);