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 public void TestGetCreationTime ()
1112 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1116 File.Create (path).Close();
1117 DateTime time = File.GetCreationTime (path);
1118 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1125 public void CreationTime ()
1128 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1130 string path = Path.GetTempFileName ();
1132 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1133 DateTime time = File.GetCreationTime (path);
1134 Assert.AreEqual (2002, time.Year, "#A1");
1135 Assert.AreEqual (4, time.Month, "#A2");
1136 Assert.AreEqual (6, time.Day, "#A3");
1137 Assert.AreEqual (4, time.Hour, "#A4");
1138 Assert.AreEqual (4, time.Second, "#A5");
1140 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1141 Assert.AreEqual (2002, time.Year, "#B1");
1142 Assert.AreEqual (4, time.Month, "#B2");
1143 Assert.AreEqual (6, time.Day, "#B3");
1144 Assert.AreEqual (4, time.Hour, "#B4");
1145 Assert.AreEqual (4, time.Second, "#B5");
1147 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1148 time = File.GetCreationTimeUtc (path);
1149 Assert.AreEqual (2002, time.Year, "#C1");
1150 Assert.AreEqual (4, time.Month, "#C2");
1151 Assert.AreEqual (6, time.Day, "#C3");
1152 Assert.AreEqual (4, time.Hour, "#C4");
1153 Assert.AreEqual (4, time.Second, "#C5");
1155 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1156 Assert.AreEqual (2002, time.Year, "#D1");
1157 Assert.AreEqual (4, time.Month, "#D2");
1158 Assert.AreEqual (6, time.Day, "#D3");
1159 Assert.AreEqual (4, time.Hour, "#D4");
1160 Assert.AreEqual (4, time.Second, "#D5");
1167 public void LastAccessTime ()
1169 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1170 if (File.Exists (path))
1172 FileStream stream = null;
1174 stream = File.Create (path);
1177 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1178 DateTime time = File.GetLastAccessTime (path);
1179 Assert.AreEqual (2002, time.Year, "#A1");
1180 Assert.AreEqual (4, time.Month, "#A2");
1181 Assert.AreEqual (6, time.Day, "#A3");
1182 Assert.AreEqual (4, time.Hour, "#A4");
1183 Assert.AreEqual (4, time.Second, "#A5");
1185 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1186 Assert.AreEqual (2002, time.Year, "#B1");
1187 Assert.AreEqual (4, time.Month, "#B2");
1188 Assert.AreEqual (6, time.Day, "#B3");
1189 Assert.AreEqual (4, time.Hour, "#B4");
1190 Assert.AreEqual (4, time.Second, "#B5");
1192 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1193 time = File.GetLastAccessTimeUtc (path);
1194 Assert.AreEqual (2002, time.Year, "#C1");
1195 Assert.AreEqual (4, time.Month, "#C2");
1196 Assert.AreEqual (6, time.Day, "#C3");
1197 Assert.AreEqual (4, time.Hour, "#C4");
1198 Assert.AreEqual (4, time.Second, "#C5");
1200 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1201 Assert.AreEqual (2002, time.Year, "#D1");
1202 Assert.AreEqual (4, time.Month, "#D2");
1203 Assert.AreEqual (6, time.Day, "#D3");
1204 Assert.AreEqual (4, time.Hour, "#D4");
1205 Assert.AreEqual (4, time.Second, "#D5");
1214 public void LastWriteTime ()
1216 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1217 if (File.Exists (path))
1219 FileStream stream = null;
1221 stream = File.Create (path);
1224 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1225 DateTime time = File.GetLastWriteTime (path);
1226 Assert.AreEqual (2002, time.Year, "#A1");
1227 Assert.AreEqual (4, time.Month, "#A2");
1228 Assert.AreEqual (6, time.Day, "#A3");
1229 Assert.AreEqual (4, time.Hour, "#A4");
1230 Assert.AreEqual (4, time.Second, "#A5");
1232 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1233 Assert.AreEqual (2002, time.Year, "#B1");
1234 Assert.AreEqual (4, time.Month, "#B2");
1235 Assert.AreEqual (6, time.Day, "#B3");
1236 Assert.AreEqual (4, time.Hour, "#B4");
1237 Assert.AreEqual (4, time.Second, "#B5");
1239 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1240 time = File.GetLastWriteTimeUtc (path);
1241 Assert.AreEqual (2002, time.Year, "#C1");
1242 Assert.AreEqual (4, time.Month, "#C2");
1243 Assert.AreEqual (6, time.Day, "#C3");
1244 Assert.AreEqual (4, time.Hour, "#C4");
1245 Assert.AreEqual (4, time.Second, "#C5");
1247 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1248 Assert.AreEqual (2002, time.Year, "#D1");
1249 Assert.AreEqual (4, time.Month, "#D2");
1250 Assert.AreEqual (6, time.Day, "#D3");
1251 Assert.AreEqual (4, time.Hour, "#D4");
1252 Assert.AreEqual (4, time.Second, "#D5");
1261 public void GetCreationTime_Path_Null ()
1264 File.GetCreationTime (null as string);
1266 } catch (ArgumentNullException ex) {
1267 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1268 Assert.IsNull (ex.InnerException, "#3");
1269 Assert.IsNotNull (ex.Message, "#4");
1270 Assert.AreEqual ("path", ex.ParamName, "#5");
1275 public void GetCreationTime_Path_Empty ()
1278 File.GetCreationTime (string.Empty);
1280 } catch (ArgumentException ex) {
1281 // Empty file name is not legal
1282 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1283 Assert.IsNull (ex.InnerException, "#3");
1284 Assert.IsNotNull (ex.Message, "#4");
1285 Assert.IsNull (ex.ParamName, "#5");
1290 public void GetCreationTime_Path_DoesNotExist ()
1292 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1296 DateTime time = File.GetCreationTime (path);
1297 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1298 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1299 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1300 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1301 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1302 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1303 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1306 File.GetCreationTime (path);
1308 } catch (IOException ex) {
1309 // Could not find a part of the path "..."
1310 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1311 Assert.IsNull (ex.InnerException, "#3");
1312 Assert.IsNotNull (ex.Message, "#4");
1313 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1319 public void GetCreationTime_Path_Whitespace ()
1322 File.GetCreationTime (" ");
1324 } catch (ArgumentException ex) {
1325 // The path is not of a legal form
1326 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1327 Assert.IsNull (ex.InnerException, "#3");
1328 Assert.IsNotNull (ex.Message, "#4");
1329 Assert.IsNull (ex.ParamName, "#5");
1334 public void GetCreationTime_Path_InvalidPathChars ()
1337 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1339 } catch (ArgumentException ex) {
1340 // Illegal characters in path
1341 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1342 Assert.IsNull (ex.InnerException, "#3");
1343 Assert.IsNotNull (ex.Message, "#4");
1344 Assert.IsNull (ex.ParamName, "#5");
1349 public void GetCreationTimeUtc_Path_Null ()
1352 File.GetCreationTimeUtc (null as string);
1354 } catch (ArgumentNullException ex) {
1355 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1356 Assert.IsNull (ex.InnerException, "#3");
1357 Assert.IsNotNull (ex.Message, "#4");
1358 Assert.AreEqual ("path", ex.ParamName, "#5");
1363 public void GetCreationTimeUtc_Path_Empty ()
1366 File.GetCreationTimeUtc (string.Empty);
1368 } catch (ArgumentException ex) {
1369 // Empty file name is not legal
1370 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1371 Assert.IsNull (ex.InnerException, "#3");
1372 Assert.IsNotNull (ex.Message, "#4");
1373 Assert.IsNull (ex.ParamName, "#5");
1378 public void GetCreationTimeUtc_Path_DoesNotExist ()
1380 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1384 DateTime time = File.GetCreationTimeUtc (path);
1385 Assert.AreEqual (1601, time.Year, "#1");
1386 Assert.AreEqual (1, time.Month, "#2");
1387 Assert.AreEqual (1, time.Day, "#3");
1388 Assert.AreEqual (0, time.Hour, "#4");
1389 Assert.AreEqual (0, time.Second, "#5");
1390 Assert.AreEqual (0, time.Millisecond, "#6");
1393 File.GetCreationTimeUtc (path);
1395 } catch (IOException ex) {
1396 // Could not find a part of the path "..."
1397 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1398 Assert.IsNull (ex.InnerException, "#3");
1399 Assert.IsNotNull (ex.Message, "#4");
1400 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1406 public void GetCreationTimeUtc_Path_Whitespace ()
1409 File.GetCreationTimeUtc (" ");
1411 } catch (ArgumentException ex) {
1412 // The path is not of a legal form
1413 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1414 Assert.IsNull (ex.InnerException, "#3");
1415 Assert.IsNotNull (ex.Message, "#4");
1416 Assert.IsNull (ex.ParamName, "#5");
1421 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1424 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1426 } catch (ArgumentException ex) {
1427 // Illegal characters in path
1428 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1429 Assert.IsNull (ex.InnerException, "#3");
1430 Assert.IsNotNull (ex.Message, "#4");
1431 Assert.IsNull (ex.ParamName, "#5");
1436 public void GetLastAccessTime_Path_Null ()
1439 File.GetLastAccessTime (null as string);
1441 } catch (ArgumentNullException ex) {
1442 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1443 Assert.IsNull (ex.InnerException, "#3");
1444 Assert.IsNotNull (ex.Message, "#4");
1445 Assert.AreEqual ("path", ex.ParamName, "#5");
1450 public void GetLastAccessTime_Path_Empty ()
1453 File.GetLastAccessTime (string.Empty);
1455 } catch (ArgumentException ex) {
1456 // Empty file name is not legal
1457 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1458 Assert.IsNull (ex.InnerException, "#3");
1459 Assert.IsNotNull (ex.Message, "#4");
1460 Assert.IsNull (ex.ParamName, "#5");
1465 public void GetLastAccessTime_Path_DoesNotExist ()
1467 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1471 DateTime time = File.GetLastAccessTime (path);
1472 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1473 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1474 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1475 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1476 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1477 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1478 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1481 File.GetLastAccessTime (path);
1483 } catch (IOException ex) {
1484 // Could not find a part of the path "..."
1485 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1486 Assert.IsNull (ex.InnerException, "#3");
1487 Assert.IsNotNull (ex.Message, "#4");
1488 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1494 public void GetLastAccessTime_Path_Whitespace ()
1497 File.GetLastAccessTime (" ");
1499 } catch (ArgumentException ex) {
1500 // The path is not of a legal form
1501 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1502 Assert.IsNull (ex.InnerException, "#3");
1503 Assert.IsNotNull (ex.Message, "#4");
1504 Assert.IsNull (ex.ParamName, "#5");
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 public void GetLastAccessTimeUtc_Path_Null ()
1527 File.GetLastAccessTimeUtc (null as string);
1529 } catch (ArgumentNullException ex) {
1530 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1531 Assert.IsNull (ex.InnerException, "#3");
1532 Assert.IsNotNull (ex.Message, "#4");
1533 Assert.AreEqual ("path", ex.ParamName, "#5");
1538 public void GetLastAccessTimeUtc_Path_Empty ()
1541 File.GetLastAccessTimeUtc (string.Empty);
1543 } catch (ArgumentException ex) {
1544 // Empty file name is not legal
1545 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1546 Assert.IsNull (ex.InnerException, "#3");
1547 Assert.IsNotNull (ex.Message, "#4");
1548 Assert.IsNull (ex.ParamName, "#5");
1553 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1555 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1559 DateTime time = File.GetLastAccessTimeUtc (path);
1560 Assert.AreEqual (1601, time.Year, "#1");
1561 Assert.AreEqual (1, time.Month, "#2");
1562 Assert.AreEqual (1, time.Day, "#3");
1563 Assert.AreEqual (0, time.Hour, "#4");
1564 Assert.AreEqual (0, time.Second, "#5");
1565 Assert.AreEqual (0, time.Millisecond, "#6");
1568 File.GetLastAccessTimeUtc (path);
1570 } catch (IOException ex) {
1571 // Could not find a part of the path "..."
1572 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1573 Assert.IsNull (ex.InnerException, "#3");
1574 Assert.IsNotNull (ex.Message, "#4");
1575 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1581 public void GetLastAccessTimeUtc_Path_Whitespace ()
1584 File.GetLastAccessTimeUtc (" ");
1586 } catch (ArgumentException ex) {
1587 // The path is not of a legal form
1588 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1589 Assert.IsNull (ex.InnerException, "#3");
1590 Assert.IsNotNull (ex.Message, "#4");
1591 Assert.IsNull (ex.ParamName, "#5");
1596 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1599 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1601 } catch (ArgumentException ex) {
1602 // Illegal characters in path
1603 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1604 Assert.IsNull (ex.InnerException, "#3");
1605 Assert.IsNotNull (ex.Message, "#4");
1606 Assert.IsNull (ex.ParamName, "#5");
1611 public void GetLastWriteTime_Path_Null ()
1614 File.GetLastWriteTime (null as string);
1616 } catch (ArgumentNullException ex) {
1617 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1618 Assert.IsNull (ex.InnerException, "#3");
1619 Assert.IsNotNull (ex.Message, "#4");
1620 Assert.AreEqual ("path", ex.ParamName, "#5");
1625 public void GetLastWriteTime_Path_Empty ()
1628 File.GetLastWriteTime (string.Empty);
1630 } catch (ArgumentException ex) {
1631 // Empty file name is not legal
1632 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1633 Assert.IsNull (ex.InnerException, "#3");
1634 Assert.IsNotNull (ex.Message, "#4");
1635 Assert.IsNull (ex.ParamName, "#5");
1640 public void GetLastWriteTime_Path_DoesNotExist ()
1642 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1646 DateTime time = File.GetLastWriteTime (path);
1647 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1648 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1649 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1650 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1651 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1652 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1653 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1656 File.GetLastWriteTime (path);
1658 } catch (IOException ex) {
1659 // Could not find a part of the path "..."
1660 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1661 Assert.IsNull (ex.InnerException, "#3");
1662 Assert.IsNotNull (ex.Message, "#4");
1663 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1669 public void GetLastWriteTime_Path_Whitespace ()
1672 File.GetLastWriteTime (" ");
1674 } catch (ArgumentException ex) {
1675 // The path is not of a legal form
1676 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1677 Assert.IsNull (ex.InnerException, "#3");
1678 Assert.IsNotNull (ex.Message, "#4");
1679 Assert.IsNull (ex.ParamName, "#5");
1684 public void GetLastWriteTime_Path_InvalidPathChars ()
1687 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1689 } catch (ArgumentException ex) {
1690 // Illegal characters in path
1691 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1692 Assert.IsNull (ex.InnerException, "#3");
1693 Assert.IsNotNull (ex.Message, "#4");
1694 Assert.IsNull (ex.ParamName, "#5");
1699 public void GetLastWriteTimeUtc_Path_Null ()
1702 File.GetLastWriteTimeUtc (null as string);
1704 } catch (ArgumentNullException ex) {
1705 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1706 Assert.IsNull (ex.InnerException, "#3");
1707 Assert.IsNotNull (ex.Message, "#4");
1708 Assert.AreEqual ("path", ex.ParamName, "#5");
1713 public void GetLastWriteTimeUtc_Path_Empty ()
1716 File.GetLastWriteTimeUtc (string.Empty);
1718 } catch (ArgumentException ex) {
1719 // Empty file name is not legal
1720 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1721 Assert.IsNull (ex.InnerException, "#3");
1722 Assert.IsNotNull (ex.Message, "#4");
1723 Assert.IsNull (ex.ParamName, "#5");
1728 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1730 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1734 DateTime time = File.GetLastWriteTimeUtc (path);
1735 Assert.AreEqual (1601, time.Year, "#1");
1736 Assert.AreEqual (1, time.Month, "#2");
1737 Assert.AreEqual (1, time.Day, "#3");
1738 Assert.AreEqual (0, time.Hour, "#4");
1739 Assert.AreEqual (0, time.Second, "#5");
1740 Assert.AreEqual (0, time.Millisecond, "#6");
1743 File.GetLastWriteTimeUtc (path);
1745 } catch (IOException ex) {
1746 // Could not find a part of the path "..."
1747 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1748 Assert.IsNull (ex.InnerException, "#3");
1749 Assert.IsNotNull (ex.Message, "#4");
1750 Assert.IsTrue (ex.Message.IndexOf ("\"" + path + "\"") != -1, "#5");
1756 public void GetLastWriteTimeUtc_Path_Whitespace ()
1759 File.GetLastWriteTimeUtc (" ");
1761 } catch (ArgumentException ex) {
1762 // The path is not of a legal form
1763 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1764 Assert.IsNull (ex.InnerException, "#3");
1765 Assert.IsNotNull (ex.Message, "#4");
1766 Assert.IsNull (ex.ParamName, "#5");
1771 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1774 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1776 } catch (ArgumentException ex) {
1777 // Illegal characters in path
1778 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1779 Assert.IsNull (ex.InnerException, "#3");
1780 Assert.IsNotNull (ex.Message, "#4");
1781 Assert.IsNull (ex.ParamName, "#5");
1786 public void FileStreamClose ()
1788 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1789 FileStream stream = null;
1791 stream = File.Create (path);
1801 // SetCreationTime and SetCreationTimeUtc exceptions
1804 public void SetCreationTime_Path_Null ()
1807 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1809 } catch (ArgumentNullException ex) {
1810 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1811 Assert.IsNull (ex.InnerException, "#3");
1812 Assert.IsNotNull (ex.Message, "#4");
1813 Assert.AreEqual ("path", ex.ParamName, "#5");
1818 public void SetCreationTime_Path_Empty ()
1821 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1823 } catch (ArgumentException ex) {
1824 // Empty file name is not legal
1825 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1826 Assert.IsNull (ex.InnerException, "#3");
1827 Assert.IsNotNull (ex.Message, "#4");
1828 Assert.IsNull (ex.ParamName, "#5");
1833 public void SetCreationTime_Path_Whitespace ()
1836 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1838 } catch (ArgumentException ex) {
1839 // The path is not of a legal form
1840 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1841 Assert.IsNull (ex.InnerException, "#3");
1842 Assert.IsNotNull (ex.Message, "#4");
1843 Assert.IsNull (ex.ParamName, "#5");
1848 public void SetCreationTime_Path_InvalidPathChars ()
1850 // On Unix there are no invalid path chars.
1851 if (Path.InvalidPathChars.Length > 1) {
1853 File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1854 new DateTime (2000, 12, 12, 11, 59, 59));
1856 } catch (ArgumentException ex) {
1857 // Illegal characters in path
1858 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1859 Assert.IsNull (ex.InnerException, "#3");
1860 Assert.IsNotNull (ex.Message, "#4");
1861 Assert.IsNull (ex.ParamName, "#5");
1867 public void SetCreationTime_Path_DoesNotExist ()
1869 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1873 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1875 } catch (FileNotFoundException ex) {
1876 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1877 Assert.AreEqual (path, ex.FileName, "#3");
1878 Assert.IsNull (ex.InnerException, "#4");
1879 Assert.IsNotNull (ex.Message, "#5");
1884 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1885 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1887 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1888 // FileStream stream = null;
1889 // DeleteFile (path);
1891 // stream = File.Create (path);
1893 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1895 // if (stream != null)
1897 // DeleteFile (path);
1902 public void SetCreationTime_FileLock ()
1904 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1906 FileStream stream = null;
1908 stream = File.Create (path);
1910 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1912 } catch (IOException ex) {
1913 // The process cannot access the file '...'
1914 // because it is being used by another process
1915 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1916 Assert.IsNull (ex.InnerException, "#3");
1917 Assert.IsNotNull (ex.Message, "#4");
1918 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1928 public void SetCreationTimeUtc_Path_Null ()
1931 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1933 } catch (ArgumentNullException ex) {
1934 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1935 Assert.IsNull (ex.InnerException, "#3");
1936 Assert.IsNotNull (ex.Message, "#4");
1937 Assert.AreEqual ("path", ex.ParamName, "#5");
1942 public void SetCreationTimeUtc_Path_Empty ()
1945 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1947 } catch (ArgumentException ex) {
1948 // Empty file name is not legal
1949 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1950 Assert.IsNull (ex.InnerException, "#3");
1951 Assert.IsNotNull (ex.Message, "#4");
1952 Assert.IsNull (ex.ParamName, "#5");
1957 public void SetCreationTimeUtc_Path_Whitespace ()
1960 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1962 } catch (ArgumentException ex) {
1963 // The path is not of a legal form
1964 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1965 Assert.IsNull (ex.InnerException, "#3");
1966 Assert.IsNotNull (ex.Message, "#4");
1967 Assert.IsNull (ex.ParamName, "#5");
1972 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1974 // On Unix there are no invalid path chars.
1975 if (Path.InvalidPathChars.Length > 1) {
1977 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1978 new DateTime (2000, 12, 12, 11, 59, 59));
1980 } catch (ArgumentException ex) {
1981 // Illegal characters in path
1982 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1983 Assert.IsNull (ex.InnerException, "#3");
1984 Assert.IsNotNull (ex.Message, "#4");
1985 Assert.IsNull (ex.ParamName, "#5");
1991 public void SetCreationTimeUtc_Path_DoesNotExist ()
1993 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1997 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1999 } catch (FileNotFoundException ex) {
2000 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2001 Assert.AreEqual (path, ex.FileName, "#3");
2002 Assert.IsNull (ex.InnerException, "#3");
2003 Assert.IsNotNull (ex.Message, "#4");
2008 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2009 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
2011 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
2012 // DeleteFile (path);
2013 // FileStream stream = null;
2015 // stream = File.Create (path);
2017 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2019 // if (stream != null)
2021 // DeleteFile (path);
2026 public void SetCreationTimeUtc_FileLock ()
2028 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
2030 FileStream stream = null;
2032 stream = File.Create (path);
2034 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2036 } catch (IOException ex) {
2037 // The process cannot access the file "..."
2038 // because it is being used by another process
2039 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2040 Assert.IsNull (ex.InnerException, "#3");
2041 Assert.IsNotNull (ex.Message, "#4");
2042 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2051 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
2054 public void SetLastAccessTime_Path_Null ()
2057 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2059 } catch (ArgumentNullException ex) {
2060 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2061 Assert.IsNull (ex.InnerException, "#3");
2062 Assert.IsNotNull (ex.Message, "#4");
2063 Assert.AreEqual ("path", ex.ParamName, "#5");
2068 public void SetLastAccessTime_Path_Empty ()
2071 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2073 } catch (ArgumentException ex) {
2074 // Empty file name is not legal
2075 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2076 Assert.IsNull (ex.InnerException, "#3");
2077 Assert.IsNotNull (ex.Message, "#4");
2078 Assert.IsNull (ex.ParamName, "#5");
2083 public void SetLastAccessTime_Path_Whitespace ()
2086 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2088 } catch (ArgumentException ex) {
2089 // The path is not of a legal form
2090 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2091 Assert.IsNull (ex.InnerException, "#3");
2092 Assert.IsNotNull (ex.Message, "#4");
2093 Assert.IsNull (ex.ParamName, "#5");
2098 public void SetLastAccessTime_Path_InvalidPathChars ()
2100 // On Unix there are no invalid path chars.
2101 if (Path.InvalidPathChars.Length > 1) {
2103 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2104 new DateTime (2000, 12, 12, 11, 59, 59));
2106 } catch (ArgumentException ex) {
2107 // Illegal characters in path
2108 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2109 Assert.IsNull (ex.InnerException, "#3");
2110 Assert.IsNotNull (ex.Message, "#4");
2111 Assert.IsNull (ex.ParamName, "#5");
2117 public void SetLastAccessTime_Path_DoesNotExist ()
2119 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2123 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2125 } catch (FileNotFoundException ex) {
2126 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2127 Assert.AreEqual (path, ex.FileName, "#3");
2128 Assert.IsNull (ex.InnerException, "#4");
2129 Assert.IsNotNull (ex.Message, "#5");
2134 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2135 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2137 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2138 // DeleteFile (path);
2139 // FileStream stream = null;
2141 // stream = File.Create (path);
2143 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2145 // if (stream != null)
2147 // DeleteFile (path);
2152 public void SetLastAccessTime_FileLock ()
2154 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2156 FileStream stream = null;
2158 stream = File.Create (path);
2160 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2162 } catch (IOException ex) {
2163 // The process cannot access the file "..."
2164 // because it is being used by another process
2165 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2166 Assert.IsNull (ex.InnerException, "#3");
2167 Assert.IsNotNull (ex.Message, "#4");
2168 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2178 public void SetLastAccessTimeUtc_Path_Null ()
2181 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2183 } catch (ArgumentNullException ex) {
2184 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2185 Assert.IsNull (ex.InnerException, "#3");
2186 Assert.IsNotNull (ex.Message, "#4");
2187 Assert.AreEqual ("path", ex.ParamName, "#5");
2192 public void SetCLastAccessTimeUtc_Path_Empty ()
2195 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2197 } catch (ArgumentException ex) {
2198 // Empty file name is not legal
2199 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2200 Assert.IsNull (ex.InnerException, "#3");
2201 Assert.IsNotNull (ex.Message, "#4");
2202 Assert.IsNull (ex.ParamName, "#5");
2207 public void SetLastAccessTimeUtc_Path_Whitespace ()
2210 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2212 } catch (ArgumentException ex) {
2213 // The path is not of a legal form
2214 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2215 Assert.IsNull (ex.InnerException, "#3");
2216 Assert.IsNotNull (ex.Message, "#4");
2217 Assert.IsNull (ex.ParamName, "#5");
2222 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2224 // On Unix there are no invalid path chars.
2225 if (Path.InvalidPathChars.Length > 1) {
2227 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2228 new DateTime (2000, 12, 12, 11, 59, 59));
2230 } catch (ArgumentException ex) {
2231 // Illegal characters in path
2232 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2233 Assert.IsNull (ex.InnerException, "#3");
2234 Assert.IsNotNull (ex.Message, "#4");
2235 Assert.IsNull (ex.ParamName, "#5");
2241 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2243 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2247 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2249 } catch (FileNotFoundException ex) {
2250 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2251 Assert.AreEqual (path, ex.FileName, "#3");
2252 Assert.IsNull (ex.InnerException, "#4");
2253 Assert.IsNotNull (ex.Message, "#5");
2258 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2259 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2261 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2262 // DeleteFile (path);
2263 // FileStream stream = null;
2265 // stream = File.Create (path);
2267 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2269 // if (stream != null)
2271 // DeleteFile (path);
2276 public void SetLastAccessTimeUtc_FileLock ()
2278 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2280 FileStream stream = null;
2282 stream = File.Create (path);
2284 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2286 } catch (IOException ex) {
2287 // The process cannot access the file "..."
2288 // because it is being used by another process
2289 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2290 Assert.IsNull (ex.InnerException, "#3");
2291 Assert.IsNotNull (ex.Message, "#4");
2292 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2301 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2304 public void SetLastWriteTime_Path_Null ()
2307 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2309 } catch (ArgumentNullException ex) {
2310 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2311 Assert.IsNull (ex.InnerException, "#3");
2312 Assert.IsNotNull (ex.Message, "#4");
2313 Assert.AreEqual ("path", ex.ParamName, "#5");
2318 public void SetLastWriteTime_Path_Empty ()
2321 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2323 } catch (ArgumentException ex) {
2324 // Empty file name is not legal
2325 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2326 Assert.IsNull (ex.InnerException, "#3");
2327 Assert.IsNotNull (ex.Message, "#4");
2328 Assert.IsNull (ex.ParamName, "#5");
2333 public void SetLastWriteTime_Path_Whitespace ()
2336 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2338 } catch (ArgumentException ex) {
2339 // The path is not of a legal form
2340 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2341 Assert.IsNull (ex.InnerException, "#3");
2342 Assert.IsNotNull (ex.Message, "#4");
2343 Assert.IsNull (ex.ParamName, "#5");
2348 public void SetLastWriteTime_Path_InvalidPathChars ()
2350 // On Unix there are no invalid path chars.
2351 if (Path.InvalidPathChars.Length > 1) {
2353 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2354 new DateTime (2000, 12, 12, 11, 59, 59));
2356 } catch (ArgumentException ex) {
2357 // Illegal characters in path
2358 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2359 Assert.IsNull (ex.InnerException, "#3");
2360 Assert.IsNotNull (ex.Message, "#4");
2361 Assert.IsNull (ex.ParamName, "#5");
2367 public void SetLastWriteTime_Path_DoesNotExist ()
2369 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2373 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2375 } catch (FileNotFoundException ex) {
2376 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2377 Assert.AreEqual (path, ex.FileName, "#3");
2378 Assert.IsNull (ex.InnerException, "#4");
2379 Assert.IsNotNull (ex.Message, "#5");
2384 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2385 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2387 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2388 // DeleteFile (path);
2389 // FileStream stream = null;
2391 // stream = File.Create (path);
2393 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2395 // if (stream != null)
2397 // DeleteFile (path);
2402 public void SetLastWriteTime_FileLock ()
2404 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2406 FileStream stream = null;
2408 stream = File.Create (path);
2410 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2412 } catch (IOException ex) {
2413 // The process cannot access the file '...'
2414 // because it is being used by another process
2415 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2416 Assert.IsNull (ex.InnerException, "#3");
2417 Assert.IsNotNull (ex.Message, "#4");
2418 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2428 public void SetLastWriteTimeUtc_Path_Null ()
2431 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2433 } catch (ArgumentNullException ex) {
2434 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2435 Assert.IsNull (ex.InnerException, "#3");
2436 Assert.IsNotNull (ex.Message, "#4");
2437 Assert.AreEqual ("path", ex.ParamName, "#5");
2442 public void SetLastWriteTimeUtc_Path_Empty ()
2445 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2447 } catch (ArgumentException ex) {
2448 // Empty file name is not legal
2449 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2450 Assert.IsNull (ex.InnerException, "#3");
2451 Assert.IsNotNull (ex.Message, "#4");
2452 Assert.IsNull (ex.ParamName, "#5");
2457 public void SetLastWriteTimeUtc_Path_Whitespace ()
2460 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2462 } catch (ArgumentException ex) {
2463 // The path is not of a legal form
2464 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2465 Assert.IsNull (ex.InnerException, "#3");
2466 Assert.IsNotNull (ex.Message, "#4");
2467 Assert.IsNull (ex.ParamName, "#5");
2472 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2474 // On Unix there are no invalid path chars.
2475 if (Path.InvalidPathChars.Length > 1) {
2477 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2478 new DateTime (2000, 12, 12, 11, 59, 59));
2480 } catch (ArgumentException ex) {
2481 // Illegal characters in path
2482 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2483 Assert.IsNull (ex.InnerException, "#3");
2484 Assert.IsNotNull (ex.Message, "#4");
2485 Assert.IsNull (ex.ParamName, "#5");
2491 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2493 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2497 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2499 } catch (FileNotFoundException ex) {
2500 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2501 Assert.AreEqual (path, ex.FileName, "#3");
2502 Assert.IsNull (ex.InnerException, "#4");
2503 Assert.IsNotNull (ex.Message, "#5");
2508 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2509 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2511 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2512 // DeleteFile (path);
2513 // FileStream stream = null;
2515 // stream = File.Create (path);
2517 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2519 // if (stream != null)
2521 // DeleteFile (path);
2526 public void SetLastWriteTimeUtc_FileLock ()
2528 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2530 FileStream stream = null;
2532 stream = File.Create (path);
2534 File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2536 } catch (IOException ex) {
2537 // The process cannot access the file '...'
2538 // because it is being used by another process
2539 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2540 Assert.IsNull (ex.InnerException, "#3");
2541 Assert.IsNotNull (ex.Message, "#4");
2542 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2552 public void OpenAppend ()
2554 string fn = Path.GetTempFileName ();
2555 using (FileStream s = File.Open (fn, FileMode.Append)) {
2560 void Position (long value)
2562 using (FileStream fs = File.OpenRead (testfile)) {
2563 fs.Position = value;
2564 Assert.AreEqual (value, fs.Position, "Position");
2565 Assert.AreEqual (1, fs.Length, "Length");
2570 public void Position_Small ()
2572 Position (Int32.MaxValue);
2576 public void Position_Large ()
2578 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2579 Position ((long) Int32.MaxValue + 1);
2582 void Seek (long value)
2584 using (FileStream fs = File.OpenRead (testfile)) {
2585 fs.Seek (value, SeekOrigin.Begin);
2586 Assert.AreEqual (value, fs.Position, "Position");
2587 Assert.AreEqual (1, fs.Length, "Length");
2592 public void Seek_Small ()
2594 Seek (Int32.MaxValue);
2598 public void Seek_Large ()
2600 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2601 Seek ((long) Int32.MaxValue + 1);
2604 void LockUnlock (long value)
2606 using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2607 fs.Lock (value - 1, 1);
2608 fs.Unlock (value - 1, 1);
2611 fs.Unlock (0, value);
2616 public void Lock_Small ()
2618 LockUnlock ((long) Int32.MaxValue);
2622 public void Lock_Large ()
2624 // note: already worked without HAVE_LARGE_FILE_SUPPORT
2625 LockUnlock ((long) Int32.MaxValue + 1);
2630 public void ReadWriteAllText ()
2632 // The MSDN docs said something about
2633 // not including a final new line. it looks
2634 // like that was not true. I'm not sure what
2635 // that was talking about
2636 read_all (string.Empty);
2645 read_all ("a\r\na");
2649 read_all ("\r\n\r\n");
2653 public void ReplaceTest ()
2655 string tmp = Path.Combine (TempFolder, "ReplaceTest");
2656 Directory.CreateDirectory (tmp);
2657 string origFile = Path.Combine (tmp, "origFile");
2658 string replaceFile = Path.Combine (tmp, "replaceFile");
2659 string backupFile = Path.Combine (tmp, "backupFile");
2661 using (StreamWriter sw = File.CreateText (origFile)) {
2662 sw.WriteLine ("origFile");
2664 using (StreamWriter sw = File.CreateText (replaceFile)) {
2665 sw.WriteLine ("replaceFile");
2667 using (StreamWriter sw = File.CreateText (backupFile)) {
2668 sw.WriteLine ("backupFile");
2671 File.Replace (origFile, replaceFile, backupFile);
2672 Assert.IsFalse (File.Exists (origFile), "#1");
2673 using (StreamReader sr = File.OpenText (replaceFile)) {
2674 string txt = sr.ReadLine ();
2675 Assert.AreEqual ("origFile", txt, "#2");
2677 using (StreamReader sr = File.OpenText (backupFile)) {
2678 string txt = sr.ReadLine ();
2679 Assert.AreEqual ("replaceFile", txt, "#3");
2684 static bool RunningOnUnix {
2686 int p = (int) Environment.OSVersion.Platform;
2687 return ((p == 4) || (p == 128) || (p == 6));
2691 void DeleteFile (string path)
2693 if (File.Exists (path))
2697 void DeleteDirectory (string path)
2699 if (Directory.Exists (path))
2700 Directory.Delete (path, true);
2704 void read_all (string s)
2706 string f = Path.GetTempFileName ();
2708 File.WriteAllText (f, s);
2709 string r = File.ReadAllText (f);
2710 Assert.AreEqual (s, r);
2717 void MoveTest (FileAccess acc, FileShare share, bool works)
2719 // use TEMP so since the default location (right along with the assemblies)
2720 // will get access denied when running under some environment (e.g. iOS devices)
2721 var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
2723 File.Delete (file + ".old");
2724 using (var v = File.Create (file)) { }
2726 using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2728 File.Move(file, file + ".old");
2730 Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
2731 } catch (IOException) {
2733 Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
2739 public void MoveTest ()
2741 MoveTest (FileAccess.Read, FileShare.None, false);
2742 MoveTest (FileAccess.Read, FileShare.Read, false);
2743 MoveTest (FileAccess.Read, FileShare.Write, false);
2744 MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2745 MoveTest (FileAccess.Read, FileShare.Delete, true);
2746 MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2747 MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2748 MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2749 MoveTest (FileAccess.Write, FileShare.None, false);
2750 MoveTest (FileAccess.Write, FileShare.Read, false);
2751 MoveTest (FileAccess.Write, FileShare.Write, false);
2752 MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2753 MoveTest (FileAccess.Write, FileShare.Delete, true);
2754 MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2755 MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2756 MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2757 MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2758 MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2759 MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2760 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2761 MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2762 MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2763 MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2764 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);