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");
815 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
818 DeleteFile (sourceFile);
823 public void Move_DestFileName_AlreadyExists ()
825 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
828 // move to same directory
829 File.Create (sourceFile).Close ();
831 File.Move (sourceFile, TempFolder);
833 } catch (IOException ex) {
834 // Cannot create a file when that file already exists
835 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
836 Assert.IsNull (ex.InnerException, "#A3");
837 Assert.IsNotNull (ex.Message, "#A4");
838 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
839 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
841 DeleteFile (sourceFile);
844 // move to exist file
845 File.Create (sourceFile).Close ();
846 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
847 File.Create (destFile).Close ();
849 File.Move (sourceFile, destFile);
851 } catch (IOException ex) {
852 // Cannot create a file when that file already exists
853 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
854 Assert.IsNull (ex.InnerException, "#B3");
855 Assert.IsNotNull (ex.Message, "#B4");
856 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
857 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
859 DeleteFile (sourceFile);
860 DeleteFile (destFile);
863 // move to existing directory
864 File.Create (sourceFile).Close ();
865 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
866 Directory.CreateDirectory (destFile);
868 File.Move (sourceFile, destFile);
870 } catch (IOException ex) {
871 // Cannot create a file when that file already exists
872 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
873 Assert.IsNull (ex.InnerException, "#C3");
874 Assert.IsNotNull (ex.Message, "#C4");
875 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
876 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
878 DeleteFile (sourceFile);
879 DeleteDirectory (destFile);
886 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
887 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
888 if (!File.Exists (bar)) {
889 FileStream f = File.Create(bar);
893 Assert.IsTrue (File.Exists (bar), "#1");
894 File.Move (bar, baz);
895 Assert.IsFalse (File.Exists (bar), "#2");
896 Assert.IsTrue (File.Exists (baz), "#3");
898 // Test moving of directories
899 string dir = Path.Combine (TempFolder, "dir");
900 string dir2 = Path.Combine (TempFolder, "dir2");
901 string dir_foo = Path.Combine (dir, "foo");
902 string dir2_foo = Path.Combine (dir2, "foo");
904 if (Directory.Exists (dir))
905 Directory.Delete (dir, true);
907 Directory.CreateDirectory (dir);
908 Directory.CreateDirectory (dir2);
909 File.Create (dir_foo).Close ();
910 File.Move (dir_foo, dir2_foo);
911 Assert.IsTrue (File.Exists (dir2_foo), "#4");
913 Directory.Delete (dir, true);
914 Directory.Delete (dir2, true);
915 DeleteFile (dir_foo);
916 DeleteFile (dir2_foo);
920 public void Move_FileLock ()
922 string sourceFile = Path.GetTempFileName ();
923 string destFile = Path.GetTempFileName ();
925 // source file locked
926 using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
928 File.Move (sourceFile, destFile);
930 } catch (IOException ex) {
931 // The process cannot access the file because
932 // it is being used by another process
933 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
934 Assert.IsNull (ex.InnerException, "#A3");
935 Assert.IsNotNull (ex.Message, "#A4");
939 // destination file locked
940 using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
942 File.Move (sourceFile, destFile);
944 } catch (IOException ex) {
945 // The process cannot access the file because
946 // it is being used by another process
947 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
948 Assert.IsNull (ex.InnerException, "#B3");
949 Assert.IsNotNull (ex.Message, "#B4");
958 FileStream stream = null;
960 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
962 if (!File.Exists (path))
963 stream = File.Create (path);
965 stream = File.Open (path, FileMode.Open);
975 if (!File.Exists (path))
976 File.Create (path).Close ();
978 stream = File.Open (path, FileMode.Open);
979 Assert.IsTrue (stream.CanRead, "#A1");
980 Assert.IsTrue (stream.CanSeek, "#A2");
981 Assert.IsTrue (stream.CanWrite, "#A3");
984 stream = File.Open (path, FileMode.Open, FileAccess.Write);
985 Assert.IsFalse (stream.CanRead, "#B1");
986 Assert.IsTrue (stream.CanSeek, "#B2");
987 Assert.IsTrue (stream.CanWrite, "#B3");
990 stream = File.Open (path, FileMode.Open, FileAccess.Read);
991 Assert.IsTrue (stream.CanRead, "#C1");
992 Assert.IsTrue (stream.CanSeek, "#C2");
993 Assert.IsFalse (stream.CanWrite, "#C3");
1003 /* Exception tests */
1004 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
1006 stream = File.Open (path, FileMode.Open);
1007 Assert.Fail ("#D1");
1008 } catch (FileNotFoundException ex) {
1009 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
1010 Assert.AreEqual (path, ex.FileName, "#D3");
1011 Assert.IsNull (ex.InnerException, "#D4");
1012 Assert.IsNotNull (ex.Message, "#D5");
1021 public void Open_CreateNewMode_ReadAccess ()
1023 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1024 FileStream stream = null;
1026 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1028 } catch (ArgumentException ex) {
1029 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1030 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1031 Assert.IsNull (ex.InnerException, "#3");
1032 Assert.IsNotNull (ex.Message, "#4");
1033 Assert.IsNull (ex.ParamName, "#5");
1042 public void Open_AppendMode_ReadAccess ()
1044 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1045 FileStream s = null;
1046 if (!File.Exists (path))
1047 File.Create (path).Close ();
1049 s = File.Open (path, FileMode.Append, FileAccess.Read);
1051 } catch (ArgumentException ex) {
1052 // Combining FileMode: Append with FileAccess: Read is invalid
1053 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1054 Assert.IsNull (ex.InnerException, "#3");
1055 Assert.IsNotNull (ex.Message, "#4");
1056 Assert.IsNull (ex.ParamName, "#5");
1065 public void OpenRead ()
1067 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1068 if (!File.Exists (path))
1069 File.Create (path).Close ();
1070 FileStream stream = null;
1073 stream = File.OpenRead (path);
1074 Assert.IsTrue (stream.CanRead, "#1");
1075 Assert.IsTrue (stream.CanSeek, "#2");
1076 Assert.IsFalse (stream.CanWrite, "#3");
1085 public void OpenWrite ()
1087 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1088 if (!File.Exists (path))
1089 File.Create (path).Close ();
1090 FileStream stream = null;
1093 stream = File.OpenWrite (path);
1094 Assert.IsFalse (stream.CanRead, "#1");
1095 Assert.IsTrue (stream.CanSeek, "#2");
1096 Assert.IsTrue (stream.CanWrite, "#3");
1106 public void TestGetCreationTime ()
1108 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1112 File.Create (path).Close();
1113 DateTime time = File.GetCreationTime (path);
1114 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1121 public void CreationTime ()
1124 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1126 string path = Path.GetTempFileName ();
1128 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1129 DateTime time = File.GetCreationTime (path);
1130 Assert.AreEqual (2002, time.Year, "#A1");
1131 Assert.AreEqual (4, time.Month, "#A2");
1132 Assert.AreEqual (6, time.Day, "#A3");
1133 Assert.AreEqual (4, time.Hour, "#A4");
1134 Assert.AreEqual (4, time.Second, "#A5");
1136 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1137 Assert.AreEqual (2002, time.Year, "#B1");
1138 Assert.AreEqual (4, time.Month, "#B2");
1139 Assert.AreEqual (6, time.Day, "#B3");
1140 Assert.AreEqual (4, time.Hour, "#B4");
1141 Assert.AreEqual (4, time.Second, "#B5");
1143 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1144 time = File.GetCreationTimeUtc (path);
1145 Assert.AreEqual (2002, time.Year, "#C1");
1146 Assert.AreEqual (4, time.Month, "#C2");
1147 Assert.AreEqual (6, time.Day, "#C3");
1148 Assert.AreEqual (4, time.Hour, "#C4");
1149 Assert.AreEqual (4, time.Second, "#C5");
1151 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1152 Assert.AreEqual (2002, time.Year, "#D1");
1153 Assert.AreEqual (4, time.Month, "#D2");
1154 Assert.AreEqual (6, time.Day, "#D3");
1155 Assert.AreEqual (4, time.Hour, "#D4");
1156 Assert.AreEqual (4, time.Second, "#D5");
1163 public void LastAccessTime ()
1165 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1166 if (File.Exists (path))
1168 FileStream stream = null;
1170 stream = File.Create (path);
1173 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1174 DateTime time = File.GetLastAccessTime (path);
1175 Assert.AreEqual (2002, time.Year, "#A1");
1176 Assert.AreEqual (4, time.Month, "#A2");
1177 Assert.AreEqual (6, time.Day, "#A3");
1178 Assert.AreEqual (4, time.Hour, "#A4");
1179 Assert.AreEqual (4, time.Second, "#A5");
1181 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1182 Assert.AreEqual (2002, time.Year, "#B1");
1183 Assert.AreEqual (4, time.Month, "#B2");
1184 Assert.AreEqual (6, time.Day, "#B3");
1185 Assert.AreEqual (4, time.Hour, "#B4");
1186 Assert.AreEqual (4, time.Second, "#B5");
1188 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1189 time = File.GetLastAccessTimeUtc (path);
1190 Assert.AreEqual (2002, time.Year, "#C1");
1191 Assert.AreEqual (4, time.Month, "#C2");
1192 Assert.AreEqual (6, time.Day, "#C3");
1193 Assert.AreEqual (4, time.Hour, "#C4");
1194 Assert.AreEqual (4, time.Second, "#C5");
1196 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1197 Assert.AreEqual (2002, time.Year, "#D1");
1198 Assert.AreEqual (4, time.Month, "#D2");
1199 Assert.AreEqual (6, time.Day, "#D3");
1200 Assert.AreEqual (4, time.Hour, "#D4");
1201 Assert.AreEqual (4, time.Second, "#D5");
1210 public void LastWriteTime ()
1212 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1213 if (File.Exists (path))
1215 FileStream stream = null;
1217 stream = File.Create (path);
1220 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1221 DateTime time = File.GetLastWriteTime (path);
1222 Assert.AreEqual (2002, time.Year, "#A1");
1223 Assert.AreEqual (4, time.Month, "#A2");
1224 Assert.AreEqual (6, time.Day, "#A3");
1225 Assert.AreEqual (4, time.Hour, "#A4");
1226 Assert.AreEqual (4, time.Second, "#A5");
1228 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1229 Assert.AreEqual (2002, time.Year, "#B1");
1230 Assert.AreEqual (4, time.Month, "#B2");
1231 Assert.AreEqual (6, time.Day, "#B3");
1232 Assert.AreEqual (4, time.Hour, "#B4");
1233 Assert.AreEqual (4, time.Second, "#B5");
1235 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1236 time = File.GetLastWriteTimeUtc (path);
1237 Assert.AreEqual (2002, time.Year, "#C1");
1238 Assert.AreEqual (4, time.Month, "#C2");
1239 Assert.AreEqual (6, time.Day, "#C3");
1240 Assert.AreEqual (4, time.Hour, "#C4");
1241 Assert.AreEqual (4, time.Second, "#C5");
1243 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1244 Assert.AreEqual (2002, time.Year, "#D1");
1245 Assert.AreEqual (4, time.Month, "#D2");
1246 Assert.AreEqual (6, time.Day, "#D3");
1247 Assert.AreEqual (4, time.Hour, "#D4");
1248 Assert.AreEqual (4, time.Second, "#D5");
1257 public void GetCreationTime_Path_Null ()
1260 File.GetCreationTime (null as string);
1262 } catch (ArgumentNullException ex) {
1263 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1264 Assert.IsNull (ex.InnerException, "#3");
1265 Assert.IsNotNull (ex.Message, "#4");
1266 Assert.AreEqual ("path", ex.ParamName, "#5");
1271 public void GetCreationTime_Path_Empty ()
1274 File.GetCreationTime (string.Empty);
1276 } catch (ArgumentException ex) {
1277 // Empty file name is not legal
1278 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1279 Assert.IsNull (ex.InnerException, "#3");
1280 Assert.IsNotNull (ex.Message, "#4");
1281 Assert.IsNull (ex.ParamName, "#5");
1286 public void GetCreationTime_Path_DoesNotExist ()
1288 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1291 DateTime time = File.GetCreationTime (path);
1292 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1293 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1294 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1295 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1296 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1297 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1298 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1302 public void GetCreationTime_Path_Whitespace ()
1305 File.GetCreationTime (" ");
1307 } catch (ArgumentException ex) {
1308 // The path is not of a legal form
1309 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1310 Assert.IsNull (ex.InnerException, "#3");
1311 Assert.IsNotNull (ex.Message, "#4");
1312 Assert.IsNull (ex.ParamName, "#5");
1317 public void GetCreationTime_Path_InvalidPathChars ()
1320 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1322 } catch (ArgumentException ex) {
1323 // Illegal characters in path
1324 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1325 Assert.IsNull (ex.InnerException, "#3");
1326 Assert.IsNotNull (ex.Message, "#4");
1327 Assert.IsNull (ex.ParamName, "#5");
1332 public void GetCreationTimeUtc_Path_Null ()
1335 File.GetCreationTimeUtc (null as string);
1337 } catch (ArgumentNullException ex) {
1338 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1339 Assert.IsNull (ex.InnerException, "#3");
1340 Assert.IsNotNull (ex.Message, "#4");
1341 Assert.AreEqual ("path", ex.ParamName, "#5");
1346 public void GetCreationTimeUtc_Path_Empty ()
1349 File.GetCreationTimeUtc (string.Empty);
1351 } catch (ArgumentException ex) {
1352 // Empty file name is not legal
1353 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1354 Assert.IsNull (ex.InnerException, "#3");
1355 Assert.IsNotNull (ex.Message, "#4");
1356 Assert.IsNull (ex.ParamName, "#5");
1361 public void GetCreationTimeUtc_Path_DoesNotExist ()
1363 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1366 DateTime time = File.GetCreationTimeUtc (path);
1367 Assert.AreEqual (1601, time.Year, "#1");
1368 Assert.AreEqual (1, time.Month, "#2");
1369 Assert.AreEqual (1, time.Day, "#3");
1370 Assert.AreEqual (0, time.Hour, "#4");
1371 Assert.AreEqual (0, time.Second, "#5");
1372 Assert.AreEqual (0, time.Millisecond, "#6");
1376 public void GetCreationTimeUtc_Path_Whitespace ()
1379 File.GetCreationTimeUtc (" ");
1381 } catch (ArgumentException ex) {
1382 // The path is not of a legal form
1383 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1384 Assert.IsNull (ex.InnerException, "#3");
1385 Assert.IsNotNull (ex.Message, "#4");
1386 Assert.IsNull (ex.ParamName, "#5");
1391 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1394 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1396 } catch (ArgumentException ex) {
1397 // Illegal characters in path
1398 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1399 Assert.IsNull (ex.InnerException, "#3");
1400 Assert.IsNotNull (ex.Message, "#4");
1401 Assert.IsNull (ex.ParamName, "#5");
1406 public void GetLastAccessTime_Path_Null ()
1409 File.GetLastAccessTime (null as string);
1411 } catch (ArgumentNullException ex) {
1412 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1413 Assert.IsNull (ex.InnerException, "#3");
1414 Assert.IsNotNull (ex.Message, "#4");
1415 Assert.AreEqual ("path", ex.ParamName, "#5");
1420 public void GetLastAccessTime_Path_Empty ()
1423 File.GetLastAccessTime (string.Empty);
1425 } catch (ArgumentException ex) {
1426 // Empty file name is not legal
1427 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1428 Assert.IsNull (ex.InnerException, "#3");
1429 Assert.IsNotNull (ex.Message, "#4");
1430 Assert.IsNull (ex.ParamName, "#5");
1435 public void GetLastAccessTime_Path_DoesNotExist ()
1437 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1440 DateTime time = File.GetLastAccessTime (path);
1441 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1442 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1443 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1444 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1445 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1446 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1447 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1451 public void GetLastAccessTime_Path_Whitespace ()
1454 File.GetLastAccessTime (" ");
1456 } catch (ArgumentException ex) {
1457 // The path is not of a legal form
1458 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1459 Assert.IsNull (ex.InnerException, "#3");
1460 Assert.IsNotNull (ex.Message, "#4");
1461 Assert.IsNull (ex.ParamName, "#5");
1466 public void GetLastAccessTime_Path_InvalidPathChars ()
1469 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1471 } catch (ArgumentException ex) {
1472 // Illegal characters in path
1473 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1474 Assert.IsNull (ex.InnerException, "#3");
1475 Assert.IsNotNull (ex.Message, "#4");
1476 Assert.IsNull (ex.ParamName, "#5");
1481 public void GetLastAccessTimeUtc_Path_Null ()
1484 File.GetLastAccessTimeUtc (null as string);
1486 } catch (ArgumentNullException ex) {
1487 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1488 Assert.IsNull (ex.InnerException, "#3");
1489 Assert.IsNotNull (ex.Message, "#4");
1490 Assert.AreEqual ("path", ex.ParamName, "#5");
1495 public void GetLastAccessTimeUtc_Path_Empty ()
1498 File.GetLastAccessTimeUtc (string.Empty);
1500 } catch (ArgumentException ex) {
1501 // Empty file name is not legal
1502 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1503 Assert.IsNull (ex.InnerException, "#3");
1504 Assert.IsNotNull (ex.Message, "#4");
1505 Assert.IsNull (ex.ParamName, "#5");
1510 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1512 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1515 DateTime time = File.GetLastAccessTimeUtc (path);
1516 Assert.AreEqual (1601, time.Year, "#1");
1517 Assert.AreEqual (1, time.Month, "#2");
1518 Assert.AreEqual (1, time.Day, "#3");
1519 Assert.AreEqual (0, time.Hour, "#4");
1520 Assert.AreEqual (0, time.Second, "#5");
1521 Assert.AreEqual (0, time.Millisecond, "#6");
1525 public void GetLastAccessTimeUtc_Path_Whitespace ()
1528 File.GetLastAccessTimeUtc (" ");
1530 } catch (ArgumentException ex) {
1531 // The path is not of a legal form
1532 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1533 Assert.IsNull (ex.InnerException, "#3");
1534 Assert.IsNotNull (ex.Message, "#4");
1535 Assert.IsNull (ex.ParamName, "#5");
1540 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1543 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1545 } catch (ArgumentException ex) {
1546 // Illegal characters in path
1547 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1548 Assert.IsNull (ex.InnerException, "#3");
1549 Assert.IsNotNull (ex.Message, "#4");
1550 Assert.IsNull (ex.ParamName, "#5");
1555 public void GetLastWriteTime_Path_Null ()
1558 File.GetLastWriteTime (null as string);
1560 } catch (ArgumentNullException ex) {
1561 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1562 Assert.IsNull (ex.InnerException, "#3");
1563 Assert.IsNotNull (ex.Message, "#4");
1564 Assert.AreEqual ("path", ex.ParamName, "#5");
1569 public void GetLastWriteTime_Path_Empty ()
1572 File.GetLastWriteTime (string.Empty);
1574 } catch (ArgumentException ex) {
1575 // Empty file name is not legal
1576 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1577 Assert.IsNull (ex.InnerException, "#3");
1578 Assert.IsNotNull (ex.Message, "#4");
1579 Assert.IsNull (ex.ParamName, "#5");
1584 public void GetLastWriteTime_Path_DoesNotExist ()
1586 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1589 DateTime time = File.GetLastWriteTime (path);
1590 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1591 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1592 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1593 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1594 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1595 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1596 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1600 public void GetLastWriteTime_Path_Whitespace ()
1603 File.GetLastWriteTime (" ");
1605 } catch (ArgumentException ex) {
1606 // The path is not of a legal form
1607 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1608 Assert.IsNull (ex.InnerException, "#3");
1609 Assert.IsNotNull (ex.Message, "#4");
1610 Assert.IsNull (ex.ParamName, "#5");
1615 public void GetLastWriteTime_Path_InvalidPathChars ()
1618 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1620 } catch (ArgumentException ex) {
1621 // Illegal characters in path
1622 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1623 Assert.IsNull (ex.InnerException, "#3");
1624 Assert.IsNotNull (ex.Message, "#4");
1625 Assert.IsNull (ex.ParamName, "#5");
1630 public void GetLastWriteTimeUtc_Path_Null ()
1633 File.GetLastWriteTimeUtc (null as string);
1635 } catch (ArgumentNullException ex) {
1636 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1637 Assert.IsNull (ex.InnerException, "#3");
1638 Assert.IsNotNull (ex.Message, "#4");
1639 Assert.AreEqual ("path", ex.ParamName, "#5");
1644 public void GetLastWriteTimeUtc_Path_Empty ()
1647 File.GetLastWriteTimeUtc (string.Empty);
1649 } catch (ArgumentException ex) {
1650 // Empty file name is not legal
1651 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1652 Assert.IsNull (ex.InnerException, "#3");
1653 Assert.IsNotNull (ex.Message, "#4");
1654 Assert.IsNull (ex.ParamName, "#5");
1659 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1661 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1664 DateTime time = File.GetLastWriteTimeUtc (path);
1665 Assert.AreEqual (1601, time.Year, "#1");
1666 Assert.AreEqual (1, time.Month, "#2");
1667 Assert.AreEqual (1, time.Day, "#3");
1668 Assert.AreEqual (0, time.Hour, "#4");
1669 Assert.AreEqual (0, time.Second, "#5");
1670 Assert.AreEqual (0, time.Millisecond, "#6");
1674 public void GetLastWriteTimeUtc_Path_Whitespace ()
1677 File.GetLastWriteTimeUtc (" ");
1679 } catch (ArgumentException ex) {
1680 // The path is not of a legal form
1681 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1682 Assert.IsNull (ex.InnerException, "#3");
1683 Assert.IsNotNull (ex.Message, "#4");
1684 Assert.IsNull (ex.ParamName, "#5");
1689 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1692 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1694 } catch (ArgumentException ex) {
1695 // Illegal characters in path
1696 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1697 Assert.IsNull (ex.InnerException, "#3");
1698 Assert.IsNotNull (ex.Message, "#4");
1699 Assert.IsNull (ex.ParamName, "#5");
1704 public void FileStreamClose ()
1706 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1707 FileStream stream = null;
1709 stream = File.Create (path);
1719 // SetCreationTime and SetCreationTimeUtc exceptions
1722 public void SetCreationTime_Path_Null ()
1725 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
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 SetCreationTime_Path_Empty ()
1739 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
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 SetCreationTime_Path_Whitespace ()
1754 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1756 } catch (ArgumentException ex) {
1757 // The path is not of a legal form
1758 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1759 Assert.IsNull (ex.InnerException, "#3");
1760 Assert.IsNotNull (ex.Message, "#4");
1761 Assert.IsNull (ex.ParamName, "#5");
1766 public void SetCreationTime_Path_InvalidPathChars ()
1768 // On Unix there are no invalid path chars.
1769 if (Path.InvalidPathChars.Length > 1) {
1771 File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1772 new DateTime (2000, 12, 12, 11, 59, 59));
1774 } catch (ArgumentException ex) {
1775 // Illegal characters in path
1776 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1777 Assert.IsNull (ex.InnerException, "#3");
1778 Assert.IsNotNull (ex.Message, "#4");
1779 Assert.IsNull (ex.ParamName, "#5");
1785 public void SetCreationTime_Path_DoesNotExist ()
1787 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1791 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1793 } catch (FileNotFoundException ex) {
1794 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1795 Assert.AreEqual (path, ex.FileName, "#3");
1796 Assert.IsNull (ex.InnerException, "#4");
1797 Assert.IsNotNull (ex.Message, "#5");
1802 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1803 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1805 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1806 // FileStream stream = null;
1807 // DeleteFile (path);
1809 // stream = File.Create (path);
1811 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1813 // if (stream != null)
1815 // DeleteFile (path);
1820 public void SetCreationTime_FileLock ()
1822 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1824 FileStream stream = null;
1826 stream = File.Create (path);
1828 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1830 } catch (IOException ex) {
1831 // The process cannot access the file '...'
1832 // because it is being used by another process
1833 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1834 Assert.IsNull (ex.InnerException, "#3");
1835 Assert.IsNotNull (ex.Message, "#4");
1836 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1846 public void SetCreationTimeUtc_Path_Null ()
1849 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1851 } catch (ArgumentNullException ex) {
1852 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1853 Assert.IsNull (ex.InnerException, "#3");
1854 Assert.IsNotNull (ex.Message, "#4");
1855 Assert.AreEqual ("path", ex.ParamName, "#5");
1860 public void SetCreationTimeUtc_Path_Empty ()
1863 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1865 } catch (ArgumentException ex) {
1866 // Empty file name is not legal
1867 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1868 Assert.IsNull (ex.InnerException, "#3");
1869 Assert.IsNotNull (ex.Message, "#4");
1870 Assert.IsNull (ex.ParamName, "#5");
1875 public void SetCreationTimeUtc_Path_Whitespace ()
1878 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1880 } catch (ArgumentException ex) {
1881 // The path is not of a legal form
1882 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1883 Assert.IsNull (ex.InnerException, "#3");
1884 Assert.IsNotNull (ex.Message, "#4");
1885 Assert.IsNull (ex.ParamName, "#5");
1890 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1892 // On Unix there are no invalid path chars.
1893 if (Path.InvalidPathChars.Length > 1) {
1895 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1896 new DateTime (2000, 12, 12, 11, 59, 59));
1898 } catch (ArgumentException ex) {
1899 // Illegal characters in path
1900 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1901 Assert.IsNull (ex.InnerException, "#3");
1902 Assert.IsNotNull (ex.Message, "#4");
1903 Assert.IsNull (ex.ParamName, "#5");
1909 public void SetCreationTimeUtc_Path_DoesNotExist ()
1911 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1915 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1917 } catch (FileNotFoundException ex) {
1918 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1919 Assert.AreEqual (path, ex.FileName, "#3");
1920 Assert.IsNull (ex.InnerException, "#3");
1921 Assert.IsNotNull (ex.Message, "#4");
1926 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1927 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1929 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1930 // DeleteFile (path);
1931 // FileStream stream = null;
1933 // stream = File.Create (path);
1935 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1937 // if (stream != null)
1939 // DeleteFile (path);
1944 public void SetCreationTimeUtc_FileLock ()
1946 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1948 FileStream stream = null;
1950 stream = File.Create (path);
1952 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1954 } catch (IOException ex) {
1955 // The process cannot access the file "..."
1956 // because it is being used by another process
1957 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1958 Assert.IsNull (ex.InnerException, "#3");
1959 Assert.IsNotNull (ex.Message, "#4");
1960 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1969 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1972 public void SetLastAccessTime_Path_Null ()
1975 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1977 } catch (ArgumentNullException ex) {
1978 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1979 Assert.IsNull (ex.InnerException, "#3");
1980 Assert.IsNotNull (ex.Message, "#4");
1981 Assert.AreEqual ("path", ex.ParamName, "#5");
1986 public void SetLastAccessTime_Path_Empty ()
1989 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1991 } catch (ArgumentException ex) {
1992 // Empty file name is not legal
1993 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1994 Assert.IsNull (ex.InnerException, "#3");
1995 Assert.IsNotNull (ex.Message, "#4");
1996 Assert.IsNull (ex.ParamName, "#5");
2001 public void SetLastAccessTime_Path_Whitespace ()
2004 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2006 } catch (ArgumentException ex) {
2007 // The path is not of a legal form
2008 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2009 Assert.IsNull (ex.InnerException, "#3");
2010 Assert.IsNotNull (ex.Message, "#4");
2011 Assert.IsNull (ex.ParamName, "#5");
2016 public void SetLastAccessTime_Path_InvalidPathChars ()
2018 // On Unix there are no invalid path chars.
2019 if (Path.InvalidPathChars.Length > 1) {
2021 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2022 new DateTime (2000, 12, 12, 11, 59, 59));
2024 } catch (ArgumentException ex) {
2025 // Illegal characters in path
2026 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2027 Assert.IsNull (ex.InnerException, "#3");
2028 Assert.IsNotNull (ex.Message, "#4");
2029 Assert.IsNull (ex.ParamName, "#5");
2035 public void SetLastAccessTime_Path_DoesNotExist ()
2037 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2041 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2043 } catch (FileNotFoundException ex) {
2044 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2045 Assert.AreEqual (path, ex.FileName, "#3");
2046 Assert.IsNull (ex.InnerException, "#4");
2047 Assert.IsNotNull (ex.Message, "#5");
2052 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2053 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2055 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2056 // DeleteFile (path);
2057 // FileStream stream = null;
2059 // stream = File.Create (path);
2061 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2063 // if (stream != null)
2065 // DeleteFile (path);
2070 public void SetLastAccessTime_FileLock ()
2072 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2074 FileStream stream = null;
2076 stream = File.Create (path);
2078 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2080 } catch (IOException ex) {
2081 // The process cannot access the file "..."
2082 // because it is being used by another process
2083 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2084 Assert.IsNull (ex.InnerException, "#3");
2085 Assert.IsNotNull (ex.Message, "#4");
2086 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2096 public void SetLastAccessTimeUtc_Path_Null ()
2099 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2101 } catch (ArgumentNullException ex) {
2102 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2103 Assert.IsNull (ex.InnerException, "#3");
2104 Assert.IsNotNull (ex.Message, "#4");
2105 Assert.AreEqual ("path", ex.ParamName, "#5");
2110 public void SetCLastAccessTimeUtc_Path_Empty ()
2113 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2115 } catch (ArgumentException ex) {
2116 // Empty file name is not legal
2117 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2118 Assert.IsNull (ex.InnerException, "#3");
2119 Assert.IsNotNull (ex.Message, "#4");
2120 Assert.IsNull (ex.ParamName, "#5");
2125 public void SetLastAccessTimeUtc_Path_Whitespace ()
2128 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2130 } catch (ArgumentException ex) {
2131 // The path is not of a legal form
2132 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2133 Assert.IsNull (ex.InnerException, "#3");
2134 Assert.IsNotNull (ex.Message, "#4");
2135 Assert.IsNull (ex.ParamName, "#5");
2140 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2142 // On Unix there are no invalid path chars.
2143 if (Path.InvalidPathChars.Length > 1) {
2145 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2146 new DateTime (2000, 12, 12, 11, 59, 59));
2148 } catch (ArgumentException ex) {
2149 // Illegal characters in path
2150 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2151 Assert.IsNull (ex.InnerException, "#3");
2152 Assert.IsNotNull (ex.Message, "#4");
2153 Assert.IsNull (ex.ParamName, "#5");
2159 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2161 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2165 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2167 } catch (FileNotFoundException ex) {
2168 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2169 Assert.AreEqual (path, ex.FileName, "#3");
2170 Assert.IsNull (ex.InnerException, "#4");
2171 Assert.IsNotNull (ex.Message, "#5");
2176 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2177 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2179 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2180 // DeleteFile (path);
2181 // FileStream stream = null;
2183 // stream = File.Create (path);
2185 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2187 // if (stream != null)
2189 // DeleteFile (path);
2194 public void SetLastAccessTimeUtc_FileLock ()
2196 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2198 FileStream stream = null;
2200 stream = File.Create (path);
2202 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2204 } catch (IOException ex) {
2205 // The process cannot access the file "..."
2206 // because it is being used by another process
2207 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2208 Assert.IsNull (ex.InnerException, "#3");
2209 Assert.IsNotNull (ex.Message, "#4");
2210 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2219 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2222 public void SetLastWriteTime_Path_Null ()
2225 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2227 } catch (ArgumentNullException ex) {
2228 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2229 Assert.IsNull (ex.InnerException, "#3");
2230 Assert.IsNotNull (ex.Message, "#4");
2231 Assert.AreEqual ("path", ex.ParamName, "#5");
2236 public void SetLastWriteTime_Path_Empty ()
2239 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2241 } catch (ArgumentException ex) {
2242 // Empty file name is not legal
2243 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2244 Assert.IsNull (ex.InnerException, "#3");
2245 Assert.IsNotNull (ex.Message, "#4");
2246 Assert.IsNull (ex.ParamName, "#5");
2251 public void SetLastWriteTime_Path_Whitespace ()
2254 File.SetLastWriteTime (" ", 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 public void SetLastWriteTime_Path_InvalidPathChars ()
2268 // On Unix there are no invalid path chars.
2269 if (Path.InvalidPathChars.Length > 1) {
2271 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2272 new DateTime (2000, 12, 12, 11, 59, 59));
2274 } catch (ArgumentException ex) {
2275 // Illegal characters in path
2276 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2277 Assert.IsNull (ex.InnerException, "#3");
2278 Assert.IsNotNull (ex.Message, "#4");
2279 Assert.IsNull (ex.ParamName, "#5");
2285 public void SetLastWriteTime_Path_DoesNotExist ()
2287 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2291 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2293 } catch (FileNotFoundException ex) {
2294 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2295 Assert.AreEqual (path, ex.FileName, "#3");
2296 Assert.IsNull (ex.InnerException, "#4");
2297 Assert.IsNotNull (ex.Message, "#5");
2302 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2303 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2305 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2306 // DeleteFile (path);
2307 // FileStream stream = null;
2309 // stream = File.Create (path);
2311 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2313 // if (stream != null)
2315 // DeleteFile (path);
2320 public void SetLastWriteTime_FileLock ()
2322 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2324 FileStream stream = null;
2326 stream = File.Create (path);
2328 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2330 } catch (IOException ex) {
2331 // The process cannot access the file '...'
2332 // because it is being used by another process
2333 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2334 Assert.IsNull (ex.InnerException, "#3");
2335 Assert.IsNotNull (ex.Message, "#4");
2336 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2346 public void SetLastWriteTimeUtc_Path_Null ()
2349 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2351 } catch (ArgumentNullException ex) {
2352 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2353 Assert.IsNull (ex.InnerException, "#3");
2354 Assert.IsNotNull (ex.Message, "#4");
2355 Assert.AreEqual ("path", ex.ParamName, "#5");
2360 public void SetLastWriteTimeUtc_Path_Empty ()
2363 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2365 } catch (ArgumentException ex) {
2366 // Empty file name is not legal
2367 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2368 Assert.IsNull (ex.InnerException, "#3");
2369 Assert.IsNotNull (ex.Message, "#4");
2370 Assert.IsNull (ex.ParamName, "#5");
2375 public void SetLastWriteTimeUtc_Path_Whitespace ()
2378 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2380 } catch (ArgumentException ex) {
2381 // The path is not of a legal form
2382 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2383 Assert.IsNull (ex.InnerException, "#3");
2384 Assert.IsNotNull (ex.Message, "#4");
2385 Assert.IsNull (ex.ParamName, "#5");
2390 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2392 // On Unix there are no invalid path chars.
2393 if (Path.InvalidPathChars.Length > 1) {
2395 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2396 new DateTime (2000, 12, 12, 11, 59, 59));
2398 } catch (ArgumentException ex) {
2399 // Illegal characters in path
2400 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2401 Assert.IsNull (ex.InnerException, "#3");
2402 Assert.IsNotNull (ex.Message, "#4");
2403 Assert.IsNull (ex.ParamName, "#5");
2409 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2411 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2415 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2417 } catch (FileNotFoundException ex) {
2418 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2419 Assert.AreEqual (path, ex.FileName, "#3");
2420 Assert.IsNull (ex.InnerException, "#4");
2421 Assert.IsNotNull (ex.Message, "#5");
2426 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2427 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2429 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2430 // DeleteFile (path);
2431 // FileStream stream = null;
2433 // stream = File.Create (path);
2435 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2437 // if (stream != null)
2439 // DeleteFile (path);
2444 public void SetLastWriteTimeUtc_FileLock ()
2446 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2448 FileStream stream = null;
2450 stream = File.Create (path);
2452 File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2454 } catch (IOException ex) {
2455 // The process cannot access the file '...'
2456 // because it is being used by another process
2457 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2458 Assert.IsNull (ex.InnerException, "#3");
2459 Assert.IsNotNull (ex.Message, "#4");
2460 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2470 public void OpenAppend ()
2472 string fn = Path.GetTempFileName ();
2473 using (FileStream s = File.Open (fn, FileMode.Append)) {
2478 void Position (long value)
2480 using (FileStream fs = File.OpenRead (testfile)) {
2481 fs.Position = value;
2482 Assert.AreEqual (value, fs.Position, "Position");
2483 Assert.AreEqual (1, fs.Length, "Length");
2488 public void Position_Small ()
2490 Position (Int32.MaxValue);
2494 public void Position_Large ()
2496 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2497 Position ((long) Int32.MaxValue + 1);
2500 void Seek (long value)
2502 using (FileStream fs = File.OpenRead (testfile)) {
2503 fs.Seek (value, SeekOrigin.Begin);
2504 Assert.AreEqual (value, fs.Position, "Position");
2505 Assert.AreEqual (1, fs.Length, "Length");
2510 public void Seek_Small ()
2512 Seek (Int32.MaxValue);
2516 public void Seek_Large ()
2518 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2519 Seek ((long) Int32.MaxValue + 1);
2522 void LockUnlock (long value)
2524 using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2525 fs.Lock (value - 1, 1);
2526 fs.Unlock (value - 1, 1);
2529 fs.Unlock (0, value);
2534 public void Lock_Small ()
2536 LockUnlock ((long) Int32.MaxValue);
2540 public void Lock_Large ()
2542 // note: already worked without HAVE_LARGE_FILE_SUPPORT
2543 LockUnlock ((long) Int32.MaxValue + 1);
2547 public void ReadWriteAllText ()
2549 // The MSDN docs said something about
2550 // not including a final new line. it looks
2551 // like that was not true. I'm not sure what
2552 // that was talking about
2553 read_all (string.Empty);
2562 read_all ("a\r\na");
2566 read_all ("\r\n\r\n");
2570 public void ReplaceTest ()
2572 string tmp = Path.Combine (TempFolder, "ReplaceTest");
2573 Directory.CreateDirectory (tmp);
2574 string origFile = Path.Combine (tmp, "origFile");
2575 string replaceFile = Path.Combine (tmp, "replaceFile");
2576 string backupFile = Path.Combine (tmp, "backupFile");
2578 using (StreamWriter sw = File.CreateText (origFile)) {
2579 sw.WriteLine ("origFile");
2581 using (StreamWriter sw = File.CreateText (replaceFile)) {
2582 sw.WriteLine ("replaceFile");
2584 using (StreamWriter sw = File.CreateText (backupFile)) {
2585 sw.WriteLine ("backupFile");
2588 File.Replace (origFile, replaceFile, backupFile);
2589 Assert.IsFalse (File.Exists (origFile), "#1");
2590 using (StreamReader sr = File.OpenText (replaceFile)) {
2591 string txt = sr.ReadLine ();
2592 Assert.AreEqual ("origFile", txt, "#2");
2594 using (StreamReader sr = File.OpenText (backupFile)) {
2595 string txt = sr.ReadLine ();
2596 Assert.AreEqual ("replaceFile", txt, "#3");
2600 static bool RunningOnUnix {
2602 int p = (int) Environment.OSVersion.Platform;
2603 return ((p == 4) || (p == 128) || (p == 6));
2607 void DeleteFile (string path)
2609 if (File.Exists (path))
2613 void DeleteDirectory (string path)
2615 if (Directory.Exists (path))
2616 Directory.Delete (path, true);
2619 void read_all (string s)
2621 string f = Path.GetTempFileName ();
2623 File.WriteAllText (f, s);
2624 string r = File.ReadAllText (f);
2625 Assert.AreEqual (s, r);
2631 void MoveTest (FileAccess acc, FileShare share, bool works)
2633 // use TEMP so since the default location (right along with the assemblies)
2634 // will get access denied when running under some environment (e.g. iOS devices)
2635 var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
2637 File.Delete (file + ".old");
2638 using (var v = File.Create (file)) { }
2640 using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2642 File.Move(file, file + ".old");
2644 Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
2645 } catch (IOException) {
2647 Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
2653 public void MoveTest ()
2655 MoveTest (FileAccess.Read, FileShare.None, false);
2656 MoveTest (FileAccess.Read, FileShare.Read, false);
2657 MoveTest (FileAccess.Read, FileShare.Write, false);
2658 MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2659 MoveTest (FileAccess.Read, FileShare.Delete, true);
2660 MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2661 MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2662 MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2663 MoveTest (FileAccess.Write, FileShare.None, false);
2664 MoveTest (FileAccess.Write, FileShare.Read, false);
2665 MoveTest (FileAccess.Write, FileShare.Write, false);
2666 MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2667 MoveTest (FileAccess.Write, FileShare.Delete, true);
2668 MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2669 MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2670 MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2671 MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2672 MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2673 MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2674 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2675 MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2676 MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2677 MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2678 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);