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;
48 public void FixtureSetUp ()
50 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
51 testfile = Path.Combine (path, "FileStreamTest.dat");
52 File.WriteAllText (testfile, "1");
56 public void FixtureTearDown ()
58 if (File.Exists (testfile))
59 File.Delete (testfile);
63 public void TestExists ()
66 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
68 Assert.IsFalse (File.Exists (null), "#1");
69 Assert.IsFalse (File.Exists (string.Empty), "#2");
70 Assert.IsFalse (File.Exists (" \t\t \t \n\t\n \n"), "#3");
72 s = File.Create (path);
74 Assert.IsTrue (File.Exists (path), "#4");
75 Assert.IsFalse (File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
84 public void Exists_InvalidFileName ()
86 Assert.IsFalse (File.Exists ("><|"), "#1");
87 Assert.IsFalse (File.Exists ("?*"), "#2");
91 public void Exists_InvalidDirectory ()
93 Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
97 public void Create_Path_Null ()
102 } catch (ArgumentNullException ex) {
103 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
104 Assert.IsNull (ex.InnerException, "#3");
105 Assert.IsNotNull (ex.Message, "#4");
106 Assert.AreEqual ("path", ex.ParamName, "#5");
111 public void Create_Path_Directory ()
113 string path = Path.Combine (TempFolder, "foo");
114 Directory.CreateDirectory (path);
118 } catch (UnauthorizedAccessException ex) {
119 // Access to the path '...' is denied
120 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
121 Assert.IsNull (ex.InnerException, "#3");
122 Assert.IsNotNull (ex.Message, "#4");
123 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
125 DeleteDirectory (path);
130 public void Create_Path_Empty ()
133 File.Create (string.Empty);
135 } catch (ArgumentException ex) {
136 // Empty file name is not legal
137 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
138 Assert.IsNull (ex.InnerException, "#3");
139 Assert.IsNotNull (ex.Message, "#4");
140 Assert.IsNull (ex.ParamName, "#5");
145 public void Create_Path_ReadOnly ()
147 string path = Path.Combine (TempFolder, "foo");
148 File.Create (path).Close ();
149 File.SetAttributes (path, FileAttributes.ReadOnly);
153 } catch (UnauthorizedAccessException ex) {
154 // Access to the path '...' is denied
155 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
156 Assert.IsNull (ex.InnerException, "#3");
157 Assert.IsNotNull (ex.Message, "#4");
158 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
160 File.SetAttributes (path, FileAttributes.Normal);
165 public void Create_Path_Whitespace ()
170 } catch (ArgumentException ex) {
171 // The path is not of a legal form
172 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
173 Assert.IsNull (ex.InnerException, "#3");
174 Assert.IsNotNull (ex.Message, "#4");
175 Assert.IsNull (ex.ParamName, "#5");
180 public void Create_Directory_DoesNotExist ()
182 FileStream stream = null;
183 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
186 stream = File.Create (path);
188 } catch (DirectoryNotFoundException ex) {
189 // Could not find a part of the path "..."
190 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
191 Assert.IsNull (ex.InnerException, "#3");
192 Assert.IsNotNull (ex.Message, "#4");
193 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
202 public void Create ()
204 FileStream stream = null;
207 /* positive test: create resources/foo */
208 path = TempFolder + Path.DirectorySeparatorChar + "foo";
211 stream = File.Create (path);
212 Assert.IsTrue (File.Exists (path), "#1");
222 /* positive test: repeat test above again to test for overwriting file */
223 path = TempFolder + Path.DirectorySeparatorChar + "foo";
225 stream = File.Create (path);
226 Assert.IsTrue (File.Exists (path), "#2");
236 public void Copy_SourceFileName_Null ()
239 File.Copy (null, "b");
241 } catch (ArgumentNullException ex) {
242 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
243 Assert.IsNull (ex.InnerException, "#3");
244 Assert.IsNotNull (ex.Message, "#4");
245 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
250 public void Copy_DestFileName_Null ()
253 File.Copy ("a", null);
255 } catch (ArgumentNullException ex) {
256 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
257 Assert.IsNull (ex.InnerException, "#3");
258 Assert.IsNotNull (ex.Message, "#4");
259 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
264 public void Copy_SourceFileName_Empty ()
267 File.Copy (string.Empty, "b");
269 } catch (ArgumentException ex) {
270 // Empty file name is not legal
271 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
272 Assert.IsNull (ex.InnerException, "#3");
273 Assert.IsNotNull (ex.Message, "#4");
274 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
279 public void Copy_DestFileName_Empty ()
282 File.Copy ("a", string.Empty);
284 } catch (ArgumentException ex) {
285 // Empty file name is not legal
286 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
287 Assert.IsNull (ex.InnerException, "#3");
288 Assert.IsNotNull (ex.Message, "#4");
289 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
294 public void Copy_SourceFileName_Whitespace ()
297 File.Copy (" ", "b");
299 } catch (ArgumentException ex) {
300 // The path is not of a legal form
301 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
302 Assert.IsNull (ex.InnerException, "#3");
303 Assert.IsNotNull (ex.Message, "#4");
304 Assert.IsNull (ex.ParamName, "#5");
309 public void Copy_DestFileName_Whitespace ()
312 File.Copy ("a", " ");
314 } catch (ArgumentException ex) {
315 // The path is not of a legal form
316 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
317 Assert.IsNull (ex.InnerException, "#3");
318 Assert.IsNotNull (ex.Message, "#4");
319 Assert.IsNull (ex.ParamName, "#5");
324 public void Copy_SourceFileName_DoesNotExist ()
327 File.Copy ("doesnotexist", "b");
329 } catch (FileNotFoundException ex) {
330 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
331 Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
332 Assert.IsNull (ex.InnerException, "#4");
333 Assert.IsNotNull (ex.Message, "#5");
338 public void Copy_DestFileName_AlreadyExists ()
340 string source = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
341 string dest = TempFolder + Path.DirectorySeparatorChar + "bar";
345 File.Create (source).Close ();
346 File.Copy (source, dest);
348 File.Copy (source, dest);
350 } catch (IOException ex) {
351 // The file '...' already exists.
352 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
353 Assert.IsNull (ex.InnerException, "#3");
354 Assert.IsNotNull (ex.Message, "#4");
355 Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
364 public void Copy_SourceFileName_DestFileName_Same ()
366 string source = TempFolder + Path.DirectorySeparatorChar + "SameFile.txt";
370 File.Create (source).Close ();
372 File.Copy (source, source, true);
374 } catch (IOException ex) {
375 // process cannot access file ... because it is being used by another process
376 Assert.IsNull (ex.InnerException, "#2");
377 Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
387 string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
388 string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
389 /* positive test: copy resources/AFile.txt to resources/bar */
394 File.Create (path2).Close ();
395 File.Copy (path2, path1);
396 Assert.IsTrue (File.Exists (path2), "#A1");
397 Assert.IsTrue (File.Exists (path1), "#A2");
399 Assert.IsTrue (File.Exists (path1), "#B1");
400 File.Copy (path2, path1, true);
401 Assert.IsTrue (File.Exists (path2), "#B2");
402 Assert.IsTrue (File.Exists (path1), "#B3");
410 public void Delete_Path_Null ()
415 } catch (ArgumentNullException ex) {
416 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
417 Assert.IsNull (ex.InnerException, "#3");
418 Assert.IsNotNull (ex.Message, "#4");
419 Assert.AreEqual ("path", ex.ParamName, "#5");
424 public void Delete_Path_Empty ()
427 File.Delete (string.Empty);
429 } catch (ArgumentException ex) {
430 // Empty file name is not legal
431 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
432 Assert.IsNull (ex.InnerException, "#3");
433 Assert.IsNotNull (ex.Message, "#4");
434 Assert.IsNull (ex.ParamName, "#5");
439 public void Delete_Path_Whitespace ()
444 } catch (ArgumentException ex) {
445 // The path is not of a legal form
446 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
447 Assert.IsNull (ex.InnerException, "#3");
448 Assert.IsNotNull (ex.Message, "#4");
449 Assert.IsNull (ex.ParamName, "#5");
454 public void Delete_Directory_DoesNotExist ()
456 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
457 if (Directory.Exists (path))
458 Directory.Delete (path, true);
463 } catch (DirectoryNotFoundException ex) {
464 // Could not find a part of the path "..."
465 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
466 Assert.IsNull (ex.InnerException, "#3");
467 Assert.IsNotNull (ex.Message, "#4");
468 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
473 public void Delete ()
475 string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
476 DeleteFile (foopath);
478 File.Create (foopath).Close ();
479 File.Delete (foopath);
480 Assert.IsFalse (File.Exists (foopath));
481 File.Delete (foopath);
483 DeleteFile (foopath);
487 [Test] // bug #323389
488 [Category ("NotWorking")]
489 public void Delete_FileLock ()
491 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
493 FileStream stream = null;
495 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
499 } catch (IOException ex) {
500 // The process cannot access the file '...'
501 // because it is being used by another process
502 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
503 Assert.IsNull (ex.InnerException, "#3");
504 Assert.IsNotNull (ex.Message, "#4");
505 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
515 [ExpectedException (typeof(UnauthorizedAccessException))]
516 public void Delete_File_ReadOnly ()
519 Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
521 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
524 File.Create (path).Close ();
525 File.SetAttributes (path, FileAttributes.ReadOnly);
528 File.SetAttributes (path, FileAttributes.Normal);
534 public void GetAttributes_Archive ()
537 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
539 FileAttributes attrs;
541 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
542 File.Create (path).Close ();
544 attrs = File.GetAttributes (path);
545 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
547 attrs &= ~FileAttributes.Archive;
548 File.SetAttributes (path, attrs);
550 attrs = File.GetAttributes (path);
551 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
555 public void GetAttributes_Default_File ()
558 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
560 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
561 File.Create (path).Close ();
563 FileAttributes attrs = File.GetAttributes (path);
565 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
566 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
567 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
568 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
569 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
570 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
574 public void GetAttributes_Default_Directory ()
576 FileAttributes attrs = File.GetAttributes (TempFolder);
578 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
579 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
580 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
581 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
582 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
583 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
587 public void GetAttributes_Directory ()
589 FileAttributes attrs = File.GetAttributes (TempFolder);
591 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
593 attrs &= ~FileAttributes.Directory;
594 File.SetAttributes (TempFolder, attrs);
596 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
598 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
599 File.Create (path).Close ();
601 attrs = File.GetAttributes (path);
602 attrs |= FileAttributes.Directory;
603 File.SetAttributes (path, attrs);
605 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
609 public void GetAttributes_ReadOnly ()
611 FileAttributes attrs;
613 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
614 File.Create (path).Close ();
616 attrs = File.GetAttributes (path);
617 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
620 attrs |= FileAttributes.ReadOnly;
621 File.SetAttributes (path, attrs);
623 attrs = File.GetAttributes (path);
624 Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
626 File.SetAttributes (path, FileAttributes.Normal);
631 public void GetAttributes_System ()
634 Assert.Ignore ("FileAttributes.System is not supported on Unix.");
636 FileAttributes attrs;
638 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
639 File.Create (path).Close ();
641 attrs = File.GetAttributes (path);
642 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
644 attrs |= FileAttributes.System;
645 File.SetAttributes (path, FileAttributes.System);
647 attrs = File.GetAttributes (path);
648 Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
652 public void GetAttributes_Path_DoesNotExist ()
654 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
656 File.GetAttributes (path);
658 } catch (FileNotFoundException ex) {
659 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
660 Assert.AreEqual (path, ex.FileName, "#3");
661 Assert.IsNull (ex.InnerException, "#4");
662 Assert.IsNotNull (ex.Message, "#5");
667 public void GetAttributes_Path_Empty ()
670 File.GetAttributes (string.Empty);
672 } catch (ArgumentException ex) {
673 // Empty file name is not legal
674 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
675 Assert.IsNull (ex.InnerException, "#3");
676 Assert.IsNotNull (ex.Message, "#4");
677 Assert.IsNull (ex.ParamName, "#5");
682 public void GetAttributes_Path_Null ()
685 File.GetAttributes (null);
687 } catch (ArgumentNullException ex) {
688 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
689 Assert.IsNull (ex.InnerException, "#3");
690 Assert.IsNotNull (ex.Message, "#4");
691 Assert.AreEqual ("path", ex.ParamName, "#5");
696 public void Move_SourceFileName_Null ()
699 File.Move (null, "b");
701 } catch (ArgumentNullException ex) {
702 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
703 Assert.IsNull (ex.InnerException, "#3");
704 Assert.IsNotNull (ex.Message, "#4");
705 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
710 public void Move_DestFileName_Null ()
713 File.Move ("a", null);
715 } catch (ArgumentNullException ex) {
716 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
717 Assert.IsNull (ex.InnerException, "#3");
718 Assert.IsNotNull (ex.Message, "#4");
719 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
724 public void Move_SourceFileName_Empty ()
727 File.Move (string.Empty, "b");
729 } catch (ArgumentException ex) {
730 // Empty file name is not legal
731 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
732 Assert.IsNull (ex.InnerException, "#3");
733 Assert.IsNotNull (ex.Message, "#4");
734 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
739 public void Move_DestFileName_Empty ()
742 File.Move ("a", string.Empty);
744 } catch (ArgumentException ex) {
745 // Empty file name is not legal
746 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
747 Assert.IsNull (ex.InnerException, "#3");
748 Assert.IsNotNull (ex.Message, "#4");
749 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
754 public void Move_SourceFileName_Whitespace ()
757 File.Move (" ", "b");
759 } catch (ArgumentException ex) {
760 // The path is not of a legal form
761 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
762 Assert.IsNull (ex.InnerException, "#3");
763 Assert.IsNotNull (ex.Message, "#4");
764 Assert.IsNull (ex.ParamName, "#5");
769 public void Move_DestFileName_Whitespace ()
772 File.Move ("a", " ");
774 } catch (ArgumentException ex) {
775 // The path is not of a legal form
776 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
777 Assert.IsNull (ex.InnerException, "#3");
778 Assert.IsNotNull (ex.Message, "#4");
779 Assert.IsNull (ex.ParamName, "#5");
784 public void Move_SourceFileName_DoesNotExist ()
786 string file = TempFolder + Path.DirectorySeparatorChar + "doesnotexist";
789 File.Move (file, "b");
791 } catch (FileNotFoundException ex) {
792 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
793 Assert.AreEqual (file, ex.FileName, "#3");
794 Assert.IsNull (ex.InnerException, "#4");
795 Assert.IsNotNull (ex.Message, "#5");
800 public void Move_DestFileName_DirectoryDoesNotExist ()
802 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
803 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
804 DeleteFile (sourceFile);
806 File.Create (sourceFile).Close ();
808 File.Move (sourceFile, destFile);
810 } catch (DirectoryNotFoundException ex) {
811 // Could not find a part of the path
812 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
813 Assert.IsNull (ex.InnerException, "#3");
814 Assert.IsNotNull (ex.Message, "#4");
816 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
818 Assert.IsTrue (ex.Message.IndexOf (destFile) != -1, "#5");
822 DeleteFile (sourceFile);
827 public void Move_DestFileName_AlreadyExists ()
829 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
832 // move to same directory
833 File.Create (sourceFile).Close ();
835 File.Move (sourceFile, TempFolder);
837 } catch (IOException ex) {
838 // Cannot create a file when that file already exists
839 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
840 Assert.IsNull (ex.InnerException, "#A3");
841 Assert.IsNotNull (ex.Message, "#A4");
842 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
843 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
845 DeleteFile (sourceFile);
848 // move to exist file
849 File.Create (sourceFile).Close ();
850 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
851 File.Create (destFile).Close ();
853 File.Move (sourceFile, destFile);
855 } catch (IOException ex) {
856 // Cannot create a file when that file already exists
857 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
858 Assert.IsNull (ex.InnerException, "#B3");
859 Assert.IsNotNull (ex.Message, "#B4");
860 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
861 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
863 DeleteFile (sourceFile);
864 DeleteFile (destFile);
867 // move to existing directory
868 File.Create (sourceFile).Close ();
869 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
870 Directory.CreateDirectory (destFile);
872 File.Move (sourceFile, destFile);
874 } catch (IOException ex) {
875 // Cannot create a file when that file already exists
876 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
877 Assert.IsNull (ex.InnerException, "#C3");
878 Assert.IsNotNull (ex.Message, "#C4");
879 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
880 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
882 DeleteFile (sourceFile);
883 DeleteDirectory (destFile);
890 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
891 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
892 if (!File.Exists (bar)) {
893 FileStream f = File.Create(bar);
897 Assert.IsTrue (File.Exists (bar), "#1");
898 File.Move (bar, baz);
899 Assert.IsFalse (File.Exists (bar), "#2");
900 Assert.IsTrue (File.Exists (baz), "#3");
902 // Test moving of directories
903 string dir = Path.Combine (TempFolder, "dir");
904 string dir2 = Path.Combine (TempFolder, "dir2");
905 string dir_foo = Path.Combine (dir, "foo");
906 string dir2_foo = Path.Combine (dir2, "foo");
908 if (Directory.Exists (dir))
909 Directory.Delete (dir, true);
911 Directory.CreateDirectory (dir);
912 Directory.CreateDirectory (dir2);
913 File.Create (dir_foo).Close ();
914 File.Move (dir_foo, dir2_foo);
915 Assert.IsTrue (File.Exists (dir2_foo), "#4");
917 Directory.Delete (dir, true);
918 Directory.Delete (dir2, true);
919 DeleteFile (dir_foo);
920 DeleteFile (dir2_foo);
924 public void Move_FileLock ()
926 string sourceFile = Path.GetTempFileName ();
927 string destFile = Path.GetTempFileName ();
929 // source file locked
930 using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
932 File.Move (sourceFile, destFile);
934 } catch (IOException ex) {
935 // The process cannot access the file because
936 // it is being used by another process
937 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
938 Assert.IsNull (ex.InnerException, "#A3");
939 Assert.IsNotNull (ex.Message, "#A4");
943 // destination file locked
944 using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
946 File.Move (sourceFile, destFile);
948 } catch (IOException ex) {
949 // The process cannot access the file because
950 // it is being used by another process
951 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
952 Assert.IsNull (ex.InnerException, "#B3");
953 Assert.IsNotNull (ex.Message, "#B4");
962 FileStream stream = null;
964 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
966 if (!File.Exists (path))
967 stream = File.Create (path);
969 stream = File.Open (path, FileMode.Open);
979 if (!File.Exists (path))
980 File.Create (path).Close ();
982 stream = File.Open (path, FileMode.Open);
983 Assert.IsTrue (stream.CanRead, "#A1");
984 Assert.IsTrue (stream.CanSeek, "#A2");
985 Assert.IsTrue (stream.CanWrite, "#A3");
988 stream = File.Open (path, FileMode.Open, FileAccess.Write);
989 Assert.IsFalse (stream.CanRead, "#B1");
990 Assert.IsTrue (stream.CanSeek, "#B2");
991 Assert.IsTrue (stream.CanWrite, "#B3");
994 stream = File.Open (path, FileMode.Open, FileAccess.Read);
995 Assert.IsTrue (stream.CanRead, "#C1");
996 Assert.IsTrue (stream.CanSeek, "#C2");
997 Assert.IsFalse (stream.CanWrite, "#C3");
1007 /* Exception tests */
1008 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
1010 stream = File.Open (path, FileMode.Open);
1011 Assert.Fail ("#D1");
1012 } catch (FileNotFoundException ex) {
1013 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
1014 Assert.AreEqual (path, ex.FileName, "#D3");
1015 Assert.IsNull (ex.InnerException, "#D4");
1016 Assert.IsNotNull (ex.Message, "#D5");
1025 public void Open_CreateNewMode_ReadAccess ()
1027 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1028 FileStream stream = null;
1030 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1032 } catch (ArgumentException ex) {
1033 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1034 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1035 Assert.IsNull (ex.InnerException, "#3");
1036 Assert.IsNotNull (ex.Message, "#4");
1037 Assert.IsNull (ex.ParamName, "#5");
1046 public void Open_AppendMode_ReadAccess ()
1048 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1049 FileStream s = null;
1050 if (!File.Exists (path))
1051 File.Create (path).Close ();
1053 s = File.Open (path, FileMode.Append, FileAccess.Read);
1055 } catch (ArgumentException ex) {
1056 // Combining FileMode: Append with FileAccess: Read is invalid
1057 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1058 Assert.IsNull (ex.InnerException, "#3");
1059 Assert.IsNotNull (ex.Message, "#4");
1060 Assert.IsNull (ex.ParamName, "#5");
1069 public void OpenRead ()
1071 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1072 if (!File.Exists (path))
1073 File.Create (path).Close ();
1074 FileStream stream = null;
1077 stream = File.OpenRead (path);
1078 Assert.IsTrue (stream.CanRead, "#1");
1079 Assert.IsTrue (stream.CanSeek, "#2");
1080 Assert.IsFalse (stream.CanWrite, "#3");
1089 public void OpenWrite ()
1091 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1092 if (!File.Exists (path))
1093 File.Create (path).Close ();
1094 FileStream stream = null;
1097 stream = File.OpenWrite (path);
1098 Assert.IsFalse (stream.CanRead, "#1");
1099 Assert.IsTrue (stream.CanSeek, "#2");
1100 Assert.IsTrue (stream.CanWrite, "#3");
1110 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1111 public void TestGetCreationTime ()
1113 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1117 File.Create (path).Close();
1118 DateTime time = File.GetCreationTime (path);
1119 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1126 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1127 public void CreationTime ()
1130 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1132 string path = Path.GetTempFileName ();
1134 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1135 DateTime time = File.GetCreationTime (path);
1136 Assert.AreEqual (2002, time.Year, "#A1");
1137 Assert.AreEqual (4, time.Month, "#A2");
1138 Assert.AreEqual (6, time.Day, "#A3");
1139 Assert.AreEqual (4, time.Hour, "#A4");
1140 Assert.AreEqual (4, time.Second, "#A5");
1142 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1143 Assert.AreEqual (2002, time.Year, "#B1");
1144 Assert.AreEqual (4, time.Month, "#B2");
1145 Assert.AreEqual (6, time.Day, "#B3");
1146 Assert.AreEqual (4, time.Hour, "#B4");
1147 Assert.AreEqual (4, time.Second, "#B5");
1149 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1150 time = File.GetCreationTimeUtc (path);
1151 Assert.AreEqual (2002, time.Year, "#C1");
1152 Assert.AreEqual (4, time.Month, "#C2");
1153 Assert.AreEqual (6, time.Day, "#C3");
1154 Assert.AreEqual (4, time.Hour, "#C4");
1155 Assert.AreEqual (4, time.Second, "#C5");
1157 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1158 Assert.AreEqual (2002, time.Year, "#D1");
1159 Assert.AreEqual (4, time.Month, "#D2");
1160 Assert.AreEqual (6, time.Day, "#D3");
1161 Assert.AreEqual (4, time.Hour, "#D4");
1162 Assert.AreEqual (4, time.Second, "#D5");
1169 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1170 public void LastAccessTime ()
1172 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1173 if (File.Exists (path))
1175 FileStream stream = null;
1177 stream = File.Create (path);
1180 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1181 DateTime time = File.GetLastAccessTime (path);
1182 Assert.AreEqual (2002, time.Year, "#A1");
1183 Assert.AreEqual (4, time.Month, "#A2");
1184 Assert.AreEqual (6, time.Day, "#A3");
1185 Assert.AreEqual (4, time.Hour, "#A4");
1186 Assert.AreEqual (4, time.Second, "#A5");
1188 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1189 Assert.AreEqual (2002, time.Year, "#B1");
1190 Assert.AreEqual (4, time.Month, "#B2");
1191 Assert.AreEqual (6, time.Day, "#B3");
1192 Assert.AreEqual (4, time.Hour, "#B4");
1193 Assert.AreEqual (4, time.Second, "#B5");
1195 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1196 time = File.GetLastAccessTimeUtc (path);
1197 Assert.AreEqual (2002, time.Year, "#C1");
1198 Assert.AreEqual (4, time.Month, "#C2");
1199 Assert.AreEqual (6, time.Day, "#C3");
1200 Assert.AreEqual (4, time.Hour, "#C4");
1201 Assert.AreEqual (4, time.Second, "#C5");
1203 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1204 Assert.AreEqual (2002, time.Year, "#D1");
1205 Assert.AreEqual (4, time.Month, "#D2");
1206 Assert.AreEqual (6, time.Day, "#D3");
1207 Assert.AreEqual (4, time.Hour, "#D4");
1208 Assert.AreEqual (4, time.Second, "#D5");
1217 public void LastWriteTime ()
1219 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1220 if (File.Exists (path))
1222 FileStream stream = null;
1224 stream = File.Create (path);
1227 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1228 DateTime time = File.GetLastWriteTime (path);
1229 Assert.AreEqual (2002, time.Year, "#A1");
1230 Assert.AreEqual (4, time.Month, "#A2");
1231 Assert.AreEqual (6, time.Day, "#A3");
1232 Assert.AreEqual (4, time.Hour, "#A4");
1233 Assert.AreEqual (4, time.Second, "#A5");
1235 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1236 Assert.AreEqual (2002, time.Year, "#B1");
1237 Assert.AreEqual (4, time.Month, "#B2");
1238 Assert.AreEqual (6, time.Day, "#B3");
1239 Assert.AreEqual (4, time.Hour, "#B4");
1240 Assert.AreEqual (4, time.Second, "#B5");
1242 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1243 time = File.GetLastWriteTimeUtc (path);
1244 Assert.AreEqual (2002, time.Year, "#C1");
1245 Assert.AreEqual (4, time.Month, "#C2");
1246 Assert.AreEqual (6, time.Day, "#C3");
1247 Assert.AreEqual (4, time.Hour, "#C4");
1248 Assert.AreEqual (4, time.Second, "#C5");
1250 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1251 Assert.AreEqual (2002, time.Year, "#D1");
1252 Assert.AreEqual (4, time.Month, "#D2");
1253 Assert.AreEqual (6, time.Day, "#D3");
1254 Assert.AreEqual (4, time.Hour, "#D4");
1255 Assert.AreEqual (4, time.Second, "#D5");
1264 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1265 public void GetCreationTime_Path_Null ()
1268 File.GetCreationTime (null as string);
1270 } catch (ArgumentNullException ex) {
1271 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1272 Assert.IsNull (ex.InnerException, "#3");
1273 Assert.IsNotNull (ex.Message, "#4");
1274 Assert.AreEqual ("path", ex.ParamName, "#5");
1279 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1280 public void GetCreationTime_Path_Empty ()
1283 File.GetCreationTime (string.Empty);
1285 } catch (ArgumentException ex) {
1286 // Empty file name is not legal
1287 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1288 Assert.IsNull (ex.InnerException, "#3");
1289 Assert.IsNotNull (ex.Message, "#4");
1290 Assert.IsNull (ex.ParamName, "#5");
1295 //[Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1296 public void GetCreationTime_Path_DoesNotExist ()
1298 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1302 DateTime time = File.GetCreationTime (path);
1303 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1304 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1305 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1306 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1307 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1308 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1309 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1312 File.GetCreationTime (path);
1314 } catch (IOException ex) {
1315 // Could not find a part of the path "..."
1316 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1317 Assert.IsNull (ex.InnerException, "#3");
1318 Assert.IsNotNull (ex.Message, "#4");
1319 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1325 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1326 public void GetCreationTime_Path_Whitespace ()
1329 File.GetCreationTime (" ");
1331 } catch (ArgumentException ex) {
1332 // The path is not of a legal form
1333 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1334 Assert.IsNull (ex.InnerException, "#3");
1335 Assert.IsNotNull (ex.Message, "#4");
1336 Assert.IsNull (ex.ParamName, "#5");
1341 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1342 public void GetCreationTime_Path_InvalidPathChars ()
1345 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1347 } catch (ArgumentException ex) {
1348 // Illegal characters in path
1349 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1350 Assert.IsNull (ex.InnerException, "#3");
1351 Assert.IsNotNull (ex.Message, "#4");
1352 Assert.IsNull (ex.ParamName, "#5");
1357 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1358 public void GetCreationTimeUtc_Path_Null ()
1361 File.GetCreationTimeUtc (null as string);
1363 } catch (ArgumentNullException ex) {
1364 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1365 Assert.IsNull (ex.InnerException, "#3");
1366 Assert.IsNotNull (ex.Message, "#4");
1367 Assert.AreEqual ("path", ex.ParamName, "#5");
1372 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1373 public void GetCreationTimeUtc_Path_Empty ()
1376 File.GetCreationTimeUtc (string.Empty);
1378 } catch (ArgumentException ex) {
1379 // Empty file name is not legal
1380 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1381 Assert.IsNull (ex.InnerException, "#3");
1382 Assert.IsNotNull (ex.Message, "#4");
1383 Assert.IsNull (ex.ParamName, "#5");
1388 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1389 public void GetCreationTimeUtc_Path_DoesNotExist ()
1391 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1395 DateTime time = File.GetCreationTimeUtc (path);
1396 Assert.AreEqual (1601, time.Year, "#1");
1397 Assert.AreEqual (1, time.Month, "#2");
1398 Assert.AreEqual (1, time.Day, "#3");
1399 Assert.AreEqual (0, time.Hour, "#4");
1400 Assert.AreEqual (0, time.Second, "#5");
1401 Assert.AreEqual (0, time.Millisecond, "#6");
1404 File.GetCreationTimeUtc (path);
1406 } catch (IOException ex) {
1407 // Could not find a part of the path "..."
1408 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1409 Assert.IsNull (ex.InnerException, "#3");
1410 Assert.IsNotNull (ex.Message, "#4");
1411 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1417 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1418 public void GetCreationTimeUtc_Path_Whitespace ()
1421 File.GetCreationTimeUtc (" ");
1423 } catch (ArgumentException ex) {
1424 // The path is not of a legal form
1425 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1426 Assert.IsNull (ex.InnerException, "#3");
1427 Assert.IsNotNull (ex.Message, "#4");
1428 Assert.IsNull (ex.ParamName, "#5");
1433 [Category("TargetJvmNotSupported")] // GetCreationTime not supported for TARGET_JVM
1434 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1437 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1439 } catch (ArgumentException ex) {
1440 // Illegal characters in path
1441 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1442 Assert.IsNull (ex.InnerException, "#3");
1443 Assert.IsNotNull (ex.Message, "#4");
1444 Assert.IsNull (ex.ParamName, "#5");
1449 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1450 public void GetLastAccessTime_Path_Null ()
1453 File.GetLastAccessTime (null as string);
1455 } catch (ArgumentNullException ex) {
1456 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1457 Assert.IsNull (ex.InnerException, "#3");
1458 Assert.IsNotNull (ex.Message, "#4");
1459 Assert.AreEqual ("path", ex.ParamName, "#5");
1464 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1465 public void GetLastAccessTime_Path_Empty ()
1468 File.GetLastAccessTime (string.Empty);
1470 } catch (ArgumentException ex) {
1471 // Empty file name is not legal
1472 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1473 Assert.IsNull (ex.InnerException, "#3");
1474 Assert.IsNotNull (ex.Message, "#4");
1475 Assert.IsNull (ex.ParamName, "#5");
1480 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1481 public void GetLastAccessTime_Path_DoesNotExist ()
1483 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1487 DateTime time = File.GetLastAccessTime (path);
1488 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1489 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1490 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1491 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1492 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1493 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1494 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1497 File.GetLastAccessTime (path);
1499 } catch (IOException ex) {
1500 // Could not find a part of the path "..."
1501 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1502 Assert.IsNull (ex.InnerException, "#3");
1503 Assert.IsNotNull (ex.Message, "#4");
1504 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1510 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1511 public void GetLastAccessTime_Path_Whitespace ()
1514 File.GetLastAccessTime (" ");
1516 } catch (ArgumentException ex) {
1517 // The path is not of a legal form
1518 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1519 Assert.IsNull (ex.InnerException, "#3");
1520 Assert.IsNotNull (ex.Message, "#4");
1521 Assert.IsNull (ex.ParamName, "#5");
1526 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1527 public void GetLastAccessTime_Path_InvalidPathChars ()
1530 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1532 } catch (ArgumentException ex) {
1533 // Illegal characters in path
1534 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1535 Assert.IsNull (ex.InnerException, "#3");
1536 Assert.IsNotNull (ex.Message, "#4");
1537 Assert.IsNull (ex.ParamName, "#5");
1542 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1543 public void GetLastAccessTimeUtc_Path_Null ()
1546 File.GetLastAccessTimeUtc (null as string);
1548 } catch (ArgumentNullException ex) {
1549 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1550 Assert.IsNull (ex.InnerException, "#3");
1551 Assert.IsNotNull (ex.Message, "#4");
1552 Assert.AreEqual ("path", ex.ParamName, "#5");
1557 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1558 public void GetLastAccessTimeUtc_Path_Empty ()
1561 File.GetLastAccessTimeUtc (string.Empty);
1563 } catch (ArgumentException ex) {
1564 // Empty file name is not legal
1565 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1566 Assert.IsNull (ex.InnerException, "#3");
1567 Assert.IsNotNull (ex.Message, "#4");
1568 Assert.IsNull (ex.ParamName, "#5");
1573 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1574 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1576 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1580 DateTime time = File.GetLastAccessTimeUtc (path);
1581 Assert.AreEqual (1601, time.Year, "#1");
1582 Assert.AreEqual (1, time.Month, "#2");
1583 Assert.AreEqual (1, time.Day, "#3");
1584 Assert.AreEqual (0, time.Hour, "#4");
1585 Assert.AreEqual (0, time.Second, "#5");
1586 Assert.AreEqual (0, time.Millisecond, "#6");
1589 File.GetLastAccessTimeUtc (path);
1591 } catch (IOException ex) {
1592 // Could not find a part of the path "..."
1593 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1594 Assert.IsNull (ex.InnerException, "#3");
1595 Assert.IsNotNull (ex.Message, "#4");
1596 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1602 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1603 public void GetLastAccessTimeUtc_Path_Whitespace ()
1606 File.GetLastAccessTimeUtc (" ");
1608 } catch (ArgumentException ex) {
1609 // The path is not of a legal form
1610 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1611 Assert.IsNull (ex.InnerException, "#3");
1612 Assert.IsNotNull (ex.Message, "#4");
1613 Assert.IsNull (ex.ParamName, "#5");
1618 [Category("TargetJvmNotSupported")] // GetLastAccessTime not supported for TARGET_JVM
1619 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1622 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1624 } catch (ArgumentException ex) {
1625 // Illegal characters in path
1626 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1627 Assert.IsNull (ex.InnerException, "#3");
1628 Assert.IsNotNull (ex.Message, "#4");
1629 Assert.IsNull (ex.ParamName, "#5");
1634 public void GetLastWriteTime_Path_Null ()
1637 File.GetLastWriteTime (null as string);
1639 } catch (ArgumentNullException ex) {
1640 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1641 Assert.IsNull (ex.InnerException, "#3");
1642 Assert.IsNotNull (ex.Message, "#4");
1643 Assert.AreEqual ("path", ex.ParamName, "#5");
1648 public void GetLastWriteTime_Path_Empty ()
1651 File.GetLastWriteTime (string.Empty);
1653 } catch (ArgumentException ex) {
1654 // Empty file name is not legal
1655 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1656 Assert.IsNull (ex.InnerException, "#3");
1657 Assert.IsNotNull (ex.Message, "#4");
1658 Assert.IsNull (ex.ParamName, "#5");
1663 public void GetLastWriteTime_Path_DoesNotExist ()
1665 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1669 DateTime time = File.GetLastWriteTime (path);
1670 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1671 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1672 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1673 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1674 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1675 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1676 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1679 File.GetLastWriteTime (path);
1681 } catch (IOException ex) {
1682 // Could not find a part of the path "..."
1683 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1684 Assert.IsNull (ex.InnerException, "#3");
1685 Assert.IsNotNull (ex.Message, "#4");
1686 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1692 public void GetLastWriteTime_Path_Whitespace ()
1695 File.GetLastWriteTime (" ");
1697 } catch (ArgumentException ex) {
1698 // The path is not of a legal form
1699 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1700 Assert.IsNull (ex.InnerException, "#3");
1701 Assert.IsNotNull (ex.Message, "#4");
1702 Assert.IsNull (ex.ParamName, "#5");
1707 public void GetLastWriteTime_Path_InvalidPathChars ()
1710 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1712 } catch (ArgumentException ex) {
1713 // Illegal characters in path
1714 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1715 Assert.IsNull (ex.InnerException, "#3");
1716 Assert.IsNotNull (ex.Message, "#4");
1717 Assert.IsNull (ex.ParamName, "#5");
1722 public void GetLastWriteTimeUtc_Path_Null ()
1725 File.GetLastWriteTimeUtc (null as string);
1727 } catch (ArgumentNullException ex) {
1728 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1729 Assert.IsNull (ex.InnerException, "#3");
1730 Assert.IsNotNull (ex.Message, "#4");
1731 Assert.AreEqual ("path", ex.ParamName, "#5");
1736 public void GetLastWriteTimeUtc_Path_Empty ()
1739 File.GetLastWriteTimeUtc (string.Empty);
1741 } catch (ArgumentException ex) {
1742 // Empty file name is not legal
1743 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1744 Assert.IsNull (ex.InnerException, "#3");
1745 Assert.IsNotNull (ex.Message, "#4");
1746 Assert.IsNull (ex.ParamName, "#5");
1751 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1753 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1757 DateTime time = File.GetLastWriteTimeUtc (path);
1758 Assert.AreEqual (1601, time.Year, "#1");
1759 Assert.AreEqual (1, time.Month, "#2");
1760 Assert.AreEqual (1, time.Day, "#3");
1761 Assert.AreEqual (0, time.Hour, "#4");
1762 Assert.AreEqual (0, time.Second, "#5");
1763 Assert.AreEqual (0, time.Millisecond, "#6");
1766 File.GetLastWriteTimeUtc (path);
1768 } catch (IOException ex) {
1769 // Could not find a part of the path "..."
1770 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1771 Assert.IsNull (ex.InnerException, "#3");
1772 Assert.IsNotNull (ex.Message, "#4");
1773 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1779 public void GetLastWriteTimeUtc_Path_Whitespace ()
1782 File.GetLastWriteTimeUtc (" ");
1784 } catch (ArgumentException ex) {
1785 // The path is not of a legal form
1786 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1787 Assert.IsNull (ex.InnerException, "#3");
1788 Assert.IsNotNull (ex.Message, "#4");
1789 Assert.IsNull (ex.ParamName, "#5");
1794 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1797 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1799 } catch (ArgumentException ex) {
1800 // Illegal characters in path
1801 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1802 Assert.IsNull (ex.InnerException, "#3");
1803 Assert.IsNotNull (ex.Message, "#4");
1804 Assert.IsNull (ex.ParamName, "#5");
1809 public void FileStreamClose ()
1811 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1812 FileStream stream = null;
1814 stream = File.Create (path);
1824 // SetCreationTime and SetCreationTimeUtc exceptions
1827 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1828 public void SetCreationTime_Path_Null ()
1831 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1833 } catch (ArgumentNullException ex) {
1834 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1835 Assert.IsNull (ex.InnerException, "#3");
1836 Assert.IsNotNull (ex.Message, "#4");
1837 Assert.AreEqual ("path", ex.ParamName, "#5");
1842 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1843 public void SetCreationTime_Path_Empty ()
1846 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1848 } catch (ArgumentException ex) {
1849 // Empty file name is not legal
1850 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1851 Assert.IsNull (ex.InnerException, "#3");
1852 Assert.IsNotNull (ex.Message, "#4");
1853 Assert.IsNull (ex.ParamName, "#5");
1858 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1859 public void SetCreationTime_Path_Whitespace ()
1862 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1864 } catch (ArgumentException ex) {
1865 // The path is not of a legal form
1866 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1867 Assert.IsNull (ex.InnerException, "#3");
1868 Assert.IsNotNull (ex.Message, "#4");
1869 Assert.IsNull (ex.ParamName, "#5");
1874 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1875 public void SetCreationTime_Path_InvalidPathChars ()
1877 // On Unix there are no invalid path chars.
1878 if (Path.InvalidPathChars.Length > 1) {
1880 File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1881 new DateTime (2000, 12, 12, 11, 59, 59));
1883 } catch (ArgumentException ex) {
1884 // Illegal characters in path
1885 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1886 Assert.IsNull (ex.InnerException, "#3");
1887 Assert.IsNotNull (ex.Message, "#4");
1888 Assert.IsNull (ex.ParamName, "#5");
1894 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1895 public void SetCreationTime_Path_DoesNotExist ()
1897 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1901 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1903 } catch (FileNotFoundException ex) {
1904 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1905 Assert.AreEqual (path, ex.FileName, "#3");
1906 Assert.IsNull (ex.InnerException, "#4");
1907 Assert.IsNotNull (ex.Message, "#5");
1912 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1913 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1915 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1916 // FileStream stream = null;
1917 // DeleteFile (path);
1919 // stream = File.Create (path);
1921 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1923 // if (stream != null)
1925 // DeleteFile (path);
1930 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1931 public void SetCreationTime_FileLock ()
1933 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1935 FileStream stream = null;
1937 stream = File.Create (path);
1939 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1941 } catch (IOException ex) {
1942 // The process cannot access the file '...'
1943 // because it is being used by another process
1944 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1945 Assert.IsNull (ex.InnerException, "#3");
1946 Assert.IsNotNull (ex.Message, "#4");
1947 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1957 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1958 public void SetCreationTimeUtc_Path_Null ()
1961 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1963 } catch (ArgumentNullException ex) {
1964 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1965 Assert.IsNull (ex.InnerException, "#3");
1966 Assert.IsNotNull (ex.Message, "#4");
1967 Assert.AreEqual ("path", ex.ParamName, "#5");
1972 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1973 public void SetCreationTimeUtc_Path_Empty ()
1976 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1978 } catch (ArgumentException ex) {
1979 // Empty file name is not legal
1980 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1981 Assert.IsNull (ex.InnerException, "#3");
1982 Assert.IsNotNull (ex.Message, "#4");
1983 Assert.IsNull (ex.ParamName, "#5");
1988 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
1989 public void SetCreationTimeUtc_Path_Whitespace ()
1992 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1994 } catch (ArgumentException ex) {
1995 // The path is not of a legal form
1996 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1997 Assert.IsNull (ex.InnerException, "#3");
1998 Assert.IsNotNull (ex.Message, "#4");
1999 Assert.IsNull (ex.ParamName, "#5");
2004 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
2005 public void SetCreationTimeUtc_Path_InvalidPathChars ()
2007 // On Unix there are no invalid path chars.
2008 if (Path.InvalidPathChars.Length > 1) {
2010 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
2011 new DateTime (2000, 12, 12, 11, 59, 59));
2013 } catch (ArgumentException ex) {
2014 // Illegal characters in path
2015 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2016 Assert.IsNull (ex.InnerException, "#3");
2017 Assert.IsNotNull (ex.Message, "#4");
2018 Assert.IsNull (ex.ParamName, "#5");
2024 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
2025 public void SetCreationTimeUtc_Path_DoesNotExist ()
2027 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
2031 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2033 } catch (FileNotFoundException ex) {
2034 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2035 Assert.AreEqual (path, ex.FileName, "#3");
2036 Assert.IsNull (ex.InnerException, "#3");
2037 Assert.IsNotNull (ex.Message, "#4");
2042 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2043 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
2045 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
2046 // DeleteFile (path);
2047 // FileStream stream = null;
2049 // stream = File.Create (path);
2051 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2053 // if (stream != null)
2055 // DeleteFile (path);
2060 [Category("TargetJvmNotSupported")] // SetCreationTime not supported for TARGET_JVM
2061 public void SetCreationTimeUtc_FileLock ()
2063 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
2065 FileStream stream = null;
2067 stream = File.Create (path);
2069 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2071 } catch (IOException ex) {
2072 // The process cannot access the file "..."
2073 // because it is being used by another process
2074 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2075 Assert.IsNull (ex.InnerException, "#3");
2076 Assert.IsNotNull (ex.Message, "#4");
2077 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2086 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
2089 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2090 public void SetLastAccessTime_Path_Null ()
2093 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2095 } catch (ArgumentNullException ex) {
2096 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2097 Assert.IsNull (ex.InnerException, "#3");
2098 Assert.IsNotNull (ex.Message, "#4");
2099 Assert.AreEqual ("path", ex.ParamName, "#5");
2104 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2105 public void SetLastAccessTime_Path_Empty ()
2108 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2110 } catch (ArgumentException ex) {
2111 // Empty file name is not legal
2112 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2113 Assert.IsNull (ex.InnerException, "#3");
2114 Assert.IsNotNull (ex.Message, "#4");
2115 Assert.IsNull (ex.ParamName, "#5");
2120 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2121 public void SetLastAccessTime_Path_Whitespace ()
2124 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2126 } catch (ArgumentException ex) {
2127 // The path is not of a legal form
2128 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2129 Assert.IsNull (ex.InnerException, "#3");
2130 Assert.IsNotNull (ex.Message, "#4");
2131 Assert.IsNull (ex.ParamName, "#5");
2136 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2137 public void SetLastAccessTime_Path_InvalidPathChars ()
2139 // On Unix there are no invalid path chars.
2140 if (Path.InvalidPathChars.Length > 1) {
2142 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2143 new DateTime (2000, 12, 12, 11, 59, 59));
2145 } catch (ArgumentException ex) {
2146 // Illegal characters in path
2147 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2148 Assert.IsNull (ex.InnerException, "#3");
2149 Assert.IsNotNull (ex.Message, "#4");
2150 Assert.IsNull (ex.ParamName, "#5");
2156 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2157 public void SetLastAccessTime_Path_DoesNotExist ()
2159 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2163 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2165 } catch (FileNotFoundException ex) {
2166 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2167 Assert.AreEqual (path, ex.FileName, "#3");
2168 Assert.IsNull (ex.InnerException, "#4");
2169 Assert.IsNotNull (ex.Message, "#5");
2174 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2175 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2177 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2178 // DeleteFile (path);
2179 // FileStream stream = null;
2181 // stream = File.Create (path);
2183 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2185 // if (stream != null)
2187 // DeleteFile (path);
2192 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2193 public void SetLastAccessTime_FileLock ()
2195 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2197 FileStream stream = null;
2199 stream = File.Create (path);
2201 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2203 } catch (IOException ex) {
2204 // The process cannot access the file "..."
2205 // because it is being used by another process
2206 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2207 Assert.IsNull (ex.InnerException, "#3");
2208 Assert.IsNotNull (ex.Message, "#4");
2209 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2219 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2220 public void SetLastAccessTimeUtc_Path_Null ()
2223 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2225 } catch (ArgumentNullException ex) {
2226 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2227 Assert.IsNull (ex.InnerException, "#3");
2228 Assert.IsNotNull (ex.Message, "#4");
2229 Assert.AreEqual ("path", ex.ParamName, "#5");
2234 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2235 public void SetCLastAccessTimeUtc_Path_Empty ()
2238 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2240 } catch (ArgumentException ex) {
2241 // Empty file name is not legal
2242 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2243 Assert.IsNull (ex.InnerException, "#3");
2244 Assert.IsNotNull (ex.Message, "#4");
2245 Assert.IsNull (ex.ParamName, "#5");
2250 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2251 public void SetLastAccessTimeUtc_Path_Whitespace ()
2254 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2256 } catch (ArgumentException ex) {
2257 // The path is not of a legal form
2258 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2259 Assert.IsNull (ex.InnerException, "#3");
2260 Assert.IsNotNull (ex.Message, "#4");
2261 Assert.IsNull (ex.ParamName, "#5");
2266 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2267 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2269 // On Unix there are no invalid path chars.
2270 if (Path.InvalidPathChars.Length > 1) {
2272 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2273 new DateTime (2000, 12, 12, 11, 59, 59));
2275 } catch (ArgumentException ex) {
2276 // Illegal characters in path
2277 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2278 Assert.IsNull (ex.InnerException, "#3");
2279 Assert.IsNotNull (ex.Message, "#4");
2280 Assert.IsNull (ex.ParamName, "#5");
2286 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2287 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2289 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2293 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2295 } catch (FileNotFoundException ex) {
2296 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2297 Assert.AreEqual (path, ex.FileName, "#3");
2298 Assert.IsNull (ex.InnerException, "#4");
2299 Assert.IsNotNull (ex.Message, "#5");
2304 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2305 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2307 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2308 // DeleteFile (path);
2309 // FileStream stream = null;
2311 // stream = File.Create (path);
2313 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2315 // if (stream != null)
2317 // DeleteFile (path);
2322 [Category("TargetJvmNotSupported")] // SetLastAccessTime not supported for TARGET_JVM
2323 public void SetLastAccessTimeUtc_FileLock ()
2325 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2327 FileStream stream = null;
2329 stream = File.Create (path);
2331 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2333 } catch (IOException ex) {
2334 // The process cannot access the file "..."
2335 // because it is being used by another process
2336 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2337 Assert.IsNull (ex.InnerException, "#3");
2338 Assert.IsNotNull (ex.Message, "#4");
2339 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2348 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2351 public void SetLastWriteTime_Path_Null ()
2354 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2356 } catch (ArgumentNullException ex) {
2357 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2358 Assert.IsNull (ex.InnerException, "#3");
2359 Assert.IsNotNull (ex.Message, "#4");
2360 Assert.AreEqual ("path", ex.ParamName, "#5");
2365 public void SetLastWriteTime_Path_Empty ()
2368 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2370 } catch (ArgumentException ex) {
2371 // Empty file name is not legal
2372 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2373 Assert.IsNull (ex.InnerException, "#3");
2374 Assert.IsNotNull (ex.Message, "#4");
2375 Assert.IsNull (ex.ParamName, "#5");
2380 public void SetLastWriteTime_Path_Whitespace ()
2383 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2385 } catch (ArgumentException ex) {
2386 // The path is not of a legal form
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");
2395 public void SetLastWriteTime_Path_InvalidPathChars ()
2397 // On Unix there are no invalid path chars.
2398 if (Path.InvalidPathChars.Length > 1) {
2400 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2401 new DateTime (2000, 12, 12, 11, 59, 59));
2403 } catch (ArgumentException ex) {
2404 // Illegal characters in path
2405 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2406 Assert.IsNull (ex.InnerException, "#3");
2407 Assert.IsNotNull (ex.Message, "#4");
2408 Assert.IsNull (ex.ParamName, "#5");
2414 public void SetLastWriteTime_Path_DoesNotExist ()
2416 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2420 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2422 } catch (FileNotFoundException ex) {
2423 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2424 Assert.AreEqual (path, ex.FileName, "#3");
2425 Assert.IsNull (ex.InnerException, "#4");
2426 Assert.IsNotNull (ex.Message, "#5");
2431 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2432 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2434 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2435 // DeleteFile (path);
2436 // FileStream stream = null;
2438 // stream = File.Create (path);
2440 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2442 // if (stream != null)
2444 // DeleteFile (path);
2449 public void SetLastWriteTime_FileLock ()
2451 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2453 FileStream stream = null;
2455 stream = File.Create (path);
2457 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2459 } catch (IOException ex) {
2460 // The process cannot access the file '...'
2461 // because it is being used by another process
2462 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2463 Assert.IsNull (ex.InnerException, "#3");
2464 Assert.IsNotNull (ex.Message, "#4");
2465 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2475 public void SetLastWriteTimeUtc_Path_Null ()
2478 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2480 } catch (ArgumentNullException ex) {
2481 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2482 Assert.IsNull (ex.InnerException, "#3");
2483 Assert.IsNotNull (ex.Message, "#4");
2484 Assert.AreEqual ("path", ex.ParamName, "#5");
2489 public void SetLastWriteTimeUtc_Path_Empty ()
2492 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2494 } catch (ArgumentException ex) {
2495 // Empty file name is not legal
2496 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2497 Assert.IsNull (ex.InnerException, "#3");
2498 Assert.IsNotNull (ex.Message, "#4");
2499 Assert.IsNull (ex.ParamName, "#5");
2504 public void SetLastWriteTimeUtc_Path_Whitespace ()
2507 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2509 } catch (ArgumentException ex) {
2510 // The path is not of a legal form
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");
2519 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2521 // On Unix there are no invalid path chars.
2522 if (Path.InvalidPathChars.Length > 1) {
2524 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2525 new DateTime (2000, 12, 12, 11, 59, 59));
2527 } catch (ArgumentException ex) {
2528 // Illegal characters in path
2529 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2530 Assert.IsNull (ex.InnerException, "#3");
2531 Assert.IsNotNull (ex.Message, "#4");
2532 Assert.IsNull (ex.ParamName, "#5");
2538 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2540 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2544 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2546 } catch (FileNotFoundException ex) {
2547 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2548 Assert.AreEqual (path, ex.FileName, "#3");
2549 Assert.IsNull (ex.InnerException, "#4");
2550 Assert.IsNotNull (ex.Message, "#5");
2555 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2556 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2558 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2559 // DeleteFile (path);
2560 // FileStream stream = null;
2562 // stream = File.Create (path);
2564 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2566 // if (stream != null)
2568 // DeleteFile (path);
2573 public void SetLastWriteTimeUtc_FileLock ()
2575 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2577 FileStream stream = null;
2579 stream = File.Create (path);
2581 File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2583 } catch (IOException ex) {
2584 // The process cannot access the file '...'
2585 // because it is being used by another process
2586 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2587 Assert.IsNull (ex.InnerException, "#3");
2588 Assert.IsNotNull (ex.Message, "#4");
2589 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2599 public void OpenAppend ()
2601 string fn = Path.GetTempFileName ();
2602 using (FileStream s = File.Open (fn, FileMode.Append)) {
2607 void Position (long value)
2609 using (FileStream fs = File.OpenRead (testfile)) {
2610 fs.Position = value;
2611 Assert.AreEqual (value, fs.Position, "Position");
2612 Assert.AreEqual (1, fs.Length, "Length");
2617 public void Position_Small ()
2619 Position (Int32.MaxValue);
2623 public void Position_Large ()
2625 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2626 Position ((long) Int32.MaxValue + 1);
2629 void Seek (long value)
2631 using (FileStream fs = File.OpenRead (testfile)) {
2632 fs.Seek (value, SeekOrigin.Begin);
2633 Assert.AreEqual (value, fs.Position, "Position");
2634 Assert.AreEqual (1, fs.Length, "Length");
2639 public void Seek_Small ()
2641 Seek (Int32.MaxValue);
2645 public void Seek_Large ()
2647 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2648 Seek ((long) Int32.MaxValue + 1);
2651 void LockUnlock (long value)
2653 using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2654 fs.Lock (value - 1, 1);
2655 fs.Unlock (value - 1, 1);
2658 fs.Unlock (0, value);
2663 public void Lock_Small ()
2665 LockUnlock ((long) Int32.MaxValue);
2669 public void Lock_Large ()
2671 // note: already worked without HAVE_LARGE_FILE_SUPPORT
2672 LockUnlock ((long) Int32.MaxValue + 1);
2677 public void ReadWriteAllText ()
2679 // The MSDN docs said something about
2680 // not including a final new line. it looks
2681 // like that was not true. I'm not sure what
2682 // that was talking about
2683 read_all (string.Empty);
2692 read_all ("a\r\na");
2696 read_all ("\r\n\r\n");
2700 public void ReplaceTest ()
2702 string tmp = Path.Combine (TempFolder, "ReplaceTest");
2703 Directory.CreateDirectory (tmp);
2704 string origFile = Path.Combine (tmp, "origFile");
2705 string replaceFile = Path.Combine (tmp, "replaceFile");
2706 string backupFile = Path.Combine (tmp, "backupFile");
2708 using (StreamWriter sw = File.CreateText (origFile)) {
2709 sw.WriteLine ("origFile");
2711 using (StreamWriter sw = File.CreateText (replaceFile)) {
2712 sw.WriteLine ("replaceFile");
2714 using (StreamWriter sw = File.CreateText (backupFile)) {
2715 sw.WriteLine ("backupFile");
2718 File.Replace (origFile, replaceFile, backupFile);
2719 Assert.IsFalse (File.Exists (origFile), "#1");
2720 using (StreamReader sr = File.OpenText (replaceFile)) {
2721 string txt = sr.ReadLine ();
2722 Assert.AreEqual ("origFile", txt, "#2");
2724 using (StreamReader sr = File.OpenText (backupFile)) {
2725 string txt = sr.ReadLine ();
2726 Assert.AreEqual ("replaceFile", txt, "#3");
2731 static bool RunningOnUnix {
2733 int p = (int) Environment.OSVersion.Platform;
2734 return ((p == 4) || (p == 128) || (p == 6));
2738 void DeleteFile (string path)
2740 if (File.Exists (path))
2744 void DeleteDirectory (string path)
2746 if (Directory.Exists (path))
2747 Directory.Delete (path, true);
2751 void read_all (string s)
2753 string f = Path.GetTempFileName ();
2755 File.WriteAllText (f, s);
2756 string r = File.ReadAllText (f);
2757 Assert.AreEqual (s, r);
2764 void MoveTest (FileAccess acc, FileShare share, bool works)
2766 var file = "kk597rfdnllh89";
2768 File.Delete (file + ".old");
2769 using (var v = File.Create (file)) { }
2771 using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2773 File.Move(file, file + ".old");
2775 Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
2776 } catch (IOException) {
2778 Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
2784 public void MoveTest ()
2786 MoveTest (FileAccess.Read, FileShare.None, false);
2787 MoveTest (FileAccess.Read, FileShare.Read, false);
2788 MoveTest (FileAccess.Read, FileShare.Write, false);
2789 MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2790 MoveTest (FileAccess.Read, FileShare.Delete, true);
2791 MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2792 MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2793 MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2794 MoveTest (FileAccess.Write, FileShare.None, false);
2795 MoveTest (FileAccess.Write, FileShare.Read, false);
2796 MoveTest (FileAccess.Write, FileShare.Write, false);
2797 MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2798 MoveTest (FileAccess.Write, FileShare.Delete, true);
2799 MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2800 MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2801 MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2802 MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2803 MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2804 MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2805 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2806 MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2807 MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2808 MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2809 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);