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;
17 using System.Runtime.InteropServices;
20 using NUnit.Framework;
22 namespace MonoTests.System.IO
27 CultureInfo old_culture;
28 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
29 static Random random = new Random ();
34 DeleteDirectory (TempFolder);
35 Directory.CreateDirectory (TempFolder);
36 old_culture = Thread.CurrentThread.CurrentCulture;
37 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
41 public void TearDown ()
43 DeleteDirectory (TempFolder);
44 Thread.CurrentThread.CurrentCulture = old_culture;
51 public void FixtureSetUp ()
53 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
54 testfile = Path.Combine (path, "FileStreamTest.dat");
55 File.WriteAllText (testfile, "1");
59 public void FixtureTearDown ()
61 if (File.Exists (testfile))
62 File.Delete (testfile);
66 public void TestExists ()
69 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
71 Assert.IsFalse (File.Exists (null), "#1");
72 Assert.IsFalse (File.Exists (string.Empty), "#2");
73 Assert.IsFalse (File.Exists (" \t\t \t \n\t\n \n"), "#3");
75 s = File.Create (path);
77 Assert.IsTrue (File.Exists (path), "#4");
78 Assert.IsFalse (File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
87 public void Exists_InvalidFileName ()
89 Assert.IsFalse (File.Exists ("><|"), "#1");
90 Assert.IsFalse (File.Exists ("?*"), "#2");
94 public void Exists_InvalidDirectory ()
96 Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
100 public void Create_Path_Null ()
105 } catch (ArgumentNullException ex) {
106 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
107 Assert.IsNull (ex.InnerException, "#3");
108 Assert.IsNotNull (ex.Message, "#4");
109 Assert.AreEqual ("path", ex.ParamName, "#5");
114 public void Create_Path_Directory ()
116 string path = Path.Combine (TempFolder, "foo");
117 Directory.CreateDirectory (path);
121 } catch (UnauthorizedAccessException ex) {
122 // Access to the path '...' is denied
123 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
124 Assert.IsNull (ex.InnerException, "#3");
125 Assert.IsNotNull (ex.Message, "#4");
126 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
128 DeleteDirectory (path);
133 public void Create_Path_Empty ()
136 File.Create (string.Empty);
138 } catch (ArgumentException ex) {
139 // Empty file name is not legal
140 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
141 Assert.IsNull (ex.InnerException, "#3");
142 Assert.IsNotNull (ex.Message, "#4");
143 Assert.IsNull (ex.ParamName, "#5");
148 public void Create_Path_ReadOnly ()
150 string path = Path.Combine (TempFolder, "foo");
151 File.Create (path).Close ();
152 File.SetAttributes (path, FileAttributes.ReadOnly);
156 } catch (UnauthorizedAccessException ex) {
157 // Access to the path '...' is denied
158 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
159 Assert.IsNull (ex.InnerException, "#3");
160 Assert.IsNotNull (ex.Message, "#4");
161 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
163 File.SetAttributes (path, FileAttributes.Normal);
168 public void Create_Path_Whitespace ()
173 } catch (ArgumentException ex) {
174 // The path is not of a legal form
175 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
176 Assert.IsNull (ex.InnerException, "#3");
177 Assert.IsNotNull (ex.Message, "#4");
178 Assert.IsNull (ex.ParamName, "#5");
183 public void Create_Directory_DoesNotExist ()
185 FileStream stream = null;
186 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
189 stream = File.Create (path);
191 } catch (DirectoryNotFoundException ex) {
192 // Could not find a part of the path "..."
193 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
194 Assert.IsNull (ex.InnerException, "#3");
195 Assert.IsNotNull (ex.Message, "#4");
196 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
205 public void Create ()
207 FileStream stream = null;
210 /* positive test: create resources/foo */
211 path = TempFolder + Path.DirectorySeparatorChar + "foo";
214 stream = File.Create (path);
215 Assert.IsTrue (File.Exists (path), "#1");
225 /* positive test: repeat test above again to test for overwriting file */
226 path = TempFolder + Path.DirectorySeparatorChar + "foo";
228 stream = File.Create (path);
229 Assert.IsTrue (File.Exists (path), "#2");
239 public void Copy_SourceFileName_Null ()
242 File.Copy (null, "b");
244 } catch (ArgumentNullException ex) {
245 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
246 Assert.IsNull (ex.InnerException, "#3");
247 Assert.IsNotNull (ex.Message, "#4");
248 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
253 public void Copy_DestFileName_Null ()
256 File.Copy ("a", null);
258 } catch (ArgumentNullException ex) {
259 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
260 Assert.IsNull (ex.InnerException, "#3");
261 Assert.IsNotNull (ex.Message, "#4");
262 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
267 public void Copy_SourceFileName_Empty ()
270 File.Copy (string.Empty, "b");
272 } catch (ArgumentException ex) {
273 // Empty file name is not legal
274 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
275 Assert.IsNull (ex.InnerException, "#3");
276 Assert.IsNotNull (ex.Message, "#4");
277 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
282 public void Copy_DestFileName_Empty ()
285 File.Copy ("a", string.Empty);
287 } catch (ArgumentException ex) {
288 // Empty file name is not legal
289 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
290 Assert.IsNull (ex.InnerException, "#3");
291 Assert.IsNotNull (ex.Message, "#4");
292 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
297 public void Copy_SourceFileName_Whitespace ()
300 File.Copy (" ", "b");
302 } catch (ArgumentException ex) {
303 // The path is not of a legal form
304 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
305 Assert.IsNull (ex.InnerException, "#3");
306 Assert.IsNotNull (ex.Message, "#4");
307 Assert.IsNull (ex.ParamName, "#5");
312 public void Copy_DestFileName_Whitespace ()
315 File.Copy ("a", " ");
317 } catch (ArgumentException ex) {
318 // The path is not of a legal form
319 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
320 Assert.IsNull (ex.InnerException, "#3");
321 Assert.IsNotNull (ex.Message, "#4");
322 Assert.IsNull (ex.ParamName, "#5");
327 public void Copy_SourceFileName_DoesNotExist ()
330 File.Copy ("doesnotexist", "b");
332 } catch (FileNotFoundException ex) {
333 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
334 Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
335 Assert.IsNull (ex.InnerException, "#4");
336 Assert.IsNotNull (ex.Message, "#5");
341 public void Copy_DestFileName_AlreadyExists ()
343 string source = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
344 string dest = TempFolder + Path.DirectorySeparatorChar + "bar";
348 File.Create (source).Close ();
349 File.Copy (source, dest);
351 File.Copy (source, dest);
353 } catch (IOException ex) {
354 // The file '...' already exists.
355 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
356 Assert.IsNull (ex.InnerException, "#3");
357 Assert.IsNotNull (ex.Message, "#4");
358 Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
367 public void Copy_SourceFileName_DestFileName_Same ()
369 string source = TempFolder + Path.DirectorySeparatorChar + "SameFile.txt";
373 File.Create (source).Close ();
375 File.Copy (source, source, true);
377 } catch (IOException ex) {
378 // process cannot access file ... because it is being used by another process
379 Assert.IsNull (ex.InnerException, "#2");
380 Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
390 string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
391 string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
392 /* positive test: copy resources/AFile.txt to resources/bar */
397 File.Create (path2).Close ();
398 File.Copy (path2, path1);
399 Assert.IsTrue (File.Exists (path2), "#A1");
400 Assert.IsTrue (File.Exists (path1), "#A2");
402 Assert.IsTrue (File.Exists (path1), "#B1");
403 File.Copy (path2, path1, true);
404 Assert.IsTrue (File.Exists (path2), "#B2");
405 Assert.IsTrue (File.Exists (path1), "#B3");
413 public void Delete_Path_Null ()
418 } catch (ArgumentNullException ex) {
419 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
420 Assert.IsNull (ex.InnerException, "#3");
421 Assert.IsNotNull (ex.Message, "#4");
422 Assert.AreEqual ("path", ex.ParamName, "#5");
427 public void Delete_Path_Empty ()
430 File.Delete (string.Empty);
432 } catch (ArgumentException ex) {
433 // Empty file name is not legal
434 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
435 Assert.IsNull (ex.InnerException, "#3");
436 Assert.IsNotNull (ex.Message, "#4");
437 Assert.IsNull (ex.ParamName, "#5");
442 public void Delete_Path_Whitespace ()
447 } catch (ArgumentException ex) {
448 // The path is not of a legal form
449 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
450 Assert.IsNull (ex.InnerException, "#3");
451 Assert.IsNotNull (ex.Message, "#4");
452 Assert.IsNull (ex.ParamName, "#5");
457 public void Delete_Directory_DoesNotExist ()
459 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
460 if (Directory.Exists (path))
461 Directory.Delete (path, true);
466 } catch (DirectoryNotFoundException ex) {
467 // Could not find a part of the path "..."
468 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
469 Assert.IsNull (ex.InnerException, "#3");
470 Assert.IsNotNull (ex.Message, "#4");
471 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
476 public void Delete ()
478 string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
479 DeleteFile (foopath);
481 File.Create (foopath).Close ();
482 File.Delete (foopath);
483 Assert.IsFalse (File.Exists (foopath));
484 File.Delete (foopath);
486 DeleteFile (foopath);
490 [Test] // bug #323389
491 [Category ("NotWorking")]
492 public void Delete_FileLock ()
494 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
496 FileStream stream = null;
498 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
502 } catch (IOException ex) {
503 // The process cannot access the file '...'
504 // because it is being used by another process
505 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
506 Assert.IsNull (ex.InnerException, "#3");
507 Assert.IsNotNull (ex.Message, "#4");
508 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
518 [ExpectedException (typeof(UnauthorizedAccessException))]
519 public void Delete_File_ReadOnly ()
522 Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
524 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
527 File.Create (path).Close ();
528 File.SetAttributes (path, FileAttributes.ReadOnly);
531 File.SetAttributes (path, FileAttributes.Normal);
537 public void GetAttributes_Archive ()
540 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
542 FileAttributes attrs;
544 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
545 File.Create (path).Close ();
547 attrs = File.GetAttributes (path);
548 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
550 attrs &= ~FileAttributes.Archive;
551 File.SetAttributes (path, attrs);
553 attrs = File.GetAttributes (path);
554 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
558 public void GetAttributes_Default_File ()
561 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
563 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
564 File.Create (path).Close ();
566 FileAttributes attrs = File.GetAttributes (path);
568 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
569 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
570 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
571 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
572 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
573 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
577 public void GetAttributes_Default_Directory ()
579 FileAttributes attrs = File.GetAttributes (TempFolder);
581 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
582 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
583 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
584 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
585 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
586 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
590 public void GetAttributes_Directory ()
592 FileAttributes attrs = File.GetAttributes (TempFolder);
594 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
596 attrs &= ~FileAttributes.Directory;
597 File.SetAttributes (TempFolder, attrs);
599 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
601 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
602 File.Create (path).Close ();
604 attrs = File.GetAttributes (path);
605 attrs |= FileAttributes.Directory;
606 File.SetAttributes (path, attrs);
608 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
612 public void GetAttributes_ReadOnly ()
614 FileAttributes attrs;
616 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
617 File.Create (path).Close ();
619 attrs = File.GetAttributes (path);
620 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
623 attrs |= FileAttributes.ReadOnly;
624 File.SetAttributes (path, attrs);
626 attrs = File.GetAttributes (path);
627 Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
629 File.SetAttributes (path, FileAttributes.Normal);
634 public void GetAttributes_System ()
637 Assert.Ignore ("FileAttributes.System is not supported on Unix.");
639 FileAttributes attrs;
641 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
642 File.Create (path).Close ();
644 attrs = File.GetAttributes (path);
645 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
647 attrs |= FileAttributes.System;
648 File.SetAttributes (path, FileAttributes.System);
650 attrs = File.GetAttributes (path);
651 Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
655 public void GetAttributes_Path_DoesNotExist ()
657 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
659 File.GetAttributes (path);
661 } catch (FileNotFoundException ex) {
662 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
663 Assert.AreEqual (path, ex.FileName, "#3");
664 Assert.IsNull (ex.InnerException, "#4");
665 Assert.IsNotNull (ex.Message, "#5");
670 public void GetAttributes_Path_Empty ()
673 File.GetAttributes (string.Empty);
675 } catch (ArgumentException ex) {
676 // Empty file name is not legal
677 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
678 Assert.IsNull (ex.InnerException, "#3");
679 Assert.IsNotNull (ex.Message, "#4");
680 Assert.IsNull (ex.ParamName, "#5");
685 public void GetAttributes_Path_Null ()
688 File.GetAttributes (null);
690 } catch (ArgumentNullException ex) {
691 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
692 Assert.IsNull (ex.InnerException, "#3");
693 Assert.IsNotNull (ex.Message, "#4");
694 Assert.AreEqual ("path", ex.ParamName, "#5");
699 public void Move_SourceFileName_Null ()
702 File.Move (null, "b");
704 } catch (ArgumentNullException ex) {
705 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
706 Assert.IsNull (ex.InnerException, "#3");
707 Assert.IsNotNull (ex.Message, "#4");
708 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
713 public void Move_DestFileName_Null ()
716 File.Move ("a", null);
718 } catch (ArgumentNullException ex) {
719 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
720 Assert.IsNull (ex.InnerException, "#3");
721 Assert.IsNotNull (ex.Message, "#4");
722 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
727 public void Move_SourceFileName_Empty ()
730 File.Move (string.Empty, "b");
732 } catch (ArgumentException ex) {
733 // Empty file name is not legal
734 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
735 Assert.IsNull (ex.InnerException, "#3");
736 Assert.IsNotNull (ex.Message, "#4");
737 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
742 public void Move_DestFileName_Empty ()
745 File.Move ("a", string.Empty);
747 } catch (ArgumentException ex) {
748 // Empty file name is not legal
749 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
750 Assert.IsNull (ex.InnerException, "#3");
751 Assert.IsNotNull (ex.Message, "#4");
752 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
757 public void Move_SourceFileName_Whitespace ()
760 File.Move (" ", "b");
762 } catch (ArgumentException ex) {
763 // The path is not of a legal form
764 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
765 Assert.IsNull (ex.InnerException, "#3");
766 Assert.IsNotNull (ex.Message, "#4");
767 Assert.IsNull (ex.ParamName, "#5");
772 public void Move_DestFileName_Whitespace ()
775 File.Move ("a", " ");
777 } catch (ArgumentException ex) {
778 // The path is not of a legal form
779 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
780 Assert.IsNull (ex.InnerException, "#3");
781 Assert.IsNotNull (ex.Message, "#4");
782 Assert.IsNull (ex.ParamName, "#5");
787 public void Move_SourceFileName_DoesNotExist ()
789 string file = TempFolder + Path.DirectorySeparatorChar + "doesnotexist";
792 File.Move (file, "b");
794 } catch (FileNotFoundException ex) {
795 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
796 Assert.AreEqual (file, ex.FileName, "#3");
797 Assert.IsNull (ex.InnerException, "#4");
798 Assert.IsNotNull (ex.Message, "#5");
803 public void Move_DestFileName_DirectoryDoesNotExist ()
805 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
806 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
807 DeleteFile (sourceFile);
809 File.Create (sourceFile).Close ();
811 File.Move (sourceFile, destFile);
813 } catch (DirectoryNotFoundException ex) {
814 // Could not find a part of the path
815 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
816 Assert.IsNull (ex.InnerException, "#3");
817 Assert.IsNotNull (ex.Message, "#4");
818 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
821 DeleteFile (sourceFile);
826 public void Move_DestFileName_AlreadyExists ()
828 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
831 // move to same directory
832 File.Create (sourceFile).Close ();
834 File.Move (sourceFile, TempFolder);
836 } catch (IOException ex) {
837 // Cannot create a file when that file already exists
838 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
839 Assert.IsNull (ex.InnerException, "#A3");
840 Assert.IsNotNull (ex.Message, "#A4");
841 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
842 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
844 DeleteFile (sourceFile);
847 // move to exist file
848 File.Create (sourceFile).Close ();
849 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
850 File.Create (destFile).Close ();
852 File.Move (sourceFile, destFile);
854 } catch (IOException ex) {
855 // Cannot create a file when that file already exists
856 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
857 Assert.IsNull (ex.InnerException, "#B3");
858 Assert.IsNotNull (ex.Message, "#B4");
859 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
860 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
862 DeleteFile (sourceFile);
863 DeleteFile (destFile);
866 // move to existing directory
867 File.Create (sourceFile).Close ();
868 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
869 Directory.CreateDirectory (destFile);
871 File.Move (sourceFile, destFile);
873 } catch (IOException ex) {
874 // Cannot create a file when that file already exists
875 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
876 Assert.IsNull (ex.InnerException, "#C3");
877 Assert.IsNotNull (ex.Message, "#C4");
878 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
879 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
881 DeleteFile (sourceFile);
882 DeleteDirectory (destFile);
889 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
890 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
891 if (!File.Exists (bar)) {
892 FileStream f = File.Create(bar);
896 Assert.IsTrue (File.Exists (bar), "#1");
897 File.Move (bar, baz);
898 Assert.IsFalse (File.Exists (bar), "#2");
899 Assert.IsTrue (File.Exists (baz), "#3");
901 // Test moving of directories
902 string dir = Path.Combine (TempFolder, "dir");
903 string dir2 = Path.Combine (TempFolder, "dir2");
904 string dir_foo = Path.Combine (dir, "foo");
905 string dir2_foo = Path.Combine (dir2, "foo");
907 if (Directory.Exists (dir))
908 Directory.Delete (dir, true);
910 Directory.CreateDirectory (dir);
911 Directory.CreateDirectory (dir2);
912 File.Create (dir_foo).Close ();
913 File.Move (dir_foo, dir2_foo);
914 Assert.IsTrue (File.Exists (dir2_foo), "#4");
916 Directory.Delete (dir, true);
917 Directory.Delete (dir2, true);
918 DeleteFile (dir_foo);
919 DeleteFile (dir2_foo);
923 public void Move_FileLock ()
925 string sourceFile = Path.GetTempFileName ();
926 string destFile = Path.GetTempFileName ();
928 // source file locked
929 using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
931 File.Move (sourceFile, destFile);
933 } catch (IOException ex) {
934 // The process cannot access the file because
935 // it is being used by another process
936 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
937 Assert.IsNull (ex.InnerException, "#A3");
938 Assert.IsNotNull (ex.Message, "#A4");
942 // destination file locked
943 using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
945 File.Move (sourceFile, destFile);
947 } catch (IOException ex) {
948 // The process cannot access the file because
949 // it is being used by another process
950 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
951 Assert.IsNull (ex.InnerException, "#B3");
952 Assert.IsNotNull (ex.Message, "#B4");
961 FileStream stream = null;
963 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
965 if (!File.Exists (path))
966 stream = File.Create (path);
968 stream = File.Open (path, FileMode.Open);
978 if (!File.Exists (path))
979 File.Create (path).Close ();
981 stream = File.Open (path, FileMode.Open);
982 Assert.IsTrue (stream.CanRead, "#A1");
983 Assert.IsTrue (stream.CanSeek, "#A2");
984 Assert.IsTrue (stream.CanWrite, "#A3");
987 stream = File.Open (path, FileMode.Open, FileAccess.Write);
988 Assert.IsFalse (stream.CanRead, "#B1");
989 Assert.IsTrue (stream.CanSeek, "#B2");
990 Assert.IsTrue (stream.CanWrite, "#B3");
993 stream = File.Open (path, FileMode.Open, FileAccess.Read);
994 Assert.IsTrue (stream.CanRead, "#C1");
995 Assert.IsTrue (stream.CanSeek, "#C2");
996 Assert.IsFalse (stream.CanWrite, "#C3");
1006 /* Exception tests */
1007 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
1009 stream = File.Open (path, FileMode.Open);
1010 Assert.Fail ("#D1");
1011 } catch (FileNotFoundException ex) {
1012 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
1013 Assert.AreEqual (path, ex.FileName, "#D3");
1014 Assert.IsNull (ex.InnerException, "#D4");
1015 Assert.IsNotNull (ex.Message, "#D5");
1024 public void Open_CreateNewMode_ReadAccess ()
1026 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1027 FileStream stream = null;
1029 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1031 } catch (ArgumentException ex) {
1032 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1033 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1034 Assert.IsNull (ex.InnerException, "#3");
1035 Assert.IsNotNull (ex.Message, "#4");
1036 Assert.IsNull (ex.ParamName, "#5");
1045 public void Open_AppendMode_ReadAccess ()
1047 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1048 FileStream s = null;
1049 if (!File.Exists (path))
1050 File.Create (path).Close ();
1052 s = File.Open (path, FileMode.Append, FileAccess.Read);
1054 } catch (ArgumentException ex) {
1055 // Combining FileMode: Append with FileAccess: Read is invalid
1056 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1057 Assert.IsNull (ex.InnerException, "#3");
1058 Assert.IsNotNull (ex.Message, "#4");
1059 Assert.IsNull (ex.ParamName, "#5");
1068 public void OpenRead ()
1070 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1071 if (!File.Exists (path))
1072 File.Create (path).Close ();
1073 FileStream stream = null;
1076 stream = File.OpenRead (path);
1077 Assert.IsTrue (stream.CanRead, "#1");
1078 Assert.IsTrue (stream.CanSeek, "#2");
1079 Assert.IsFalse (stream.CanWrite, "#3");
1088 public void OpenWrite ()
1090 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1091 if (!File.Exists (path))
1092 File.Create (path).Close ();
1093 FileStream stream = null;
1096 stream = File.OpenWrite (path);
1097 Assert.IsFalse (stream.CanRead, "#1");
1098 Assert.IsTrue (stream.CanSeek, "#2");
1099 Assert.IsTrue (stream.CanWrite, "#3");
1109 public void TestGetCreationTime ()
1111 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1115 File.Create (path).Close();
1116 DateTime time = File.GetCreationTime (path);
1117 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1124 public void CreationTime ()
1127 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1129 string path = Path.GetTempFileName ();
1131 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1132 DateTime time = File.GetCreationTime (path);
1133 Assert.AreEqual (2002, time.Year, "#A1");
1134 Assert.AreEqual (4, time.Month, "#A2");
1135 Assert.AreEqual (6, time.Day, "#A3");
1136 Assert.AreEqual (4, time.Hour, "#A4");
1137 Assert.AreEqual (4, time.Second, "#A5");
1139 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1140 Assert.AreEqual (2002, time.Year, "#B1");
1141 Assert.AreEqual (4, time.Month, "#B2");
1142 Assert.AreEqual (6, time.Day, "#B3");
1143 Assert.AreEqual (4, time.Hour, "#B4");
1144 Assert.AreEqual (4, time.Second, "#B5");
1146 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1147 time = File.GetCreationTimeUtc (path);
1148 Assert.AreEqual (2002, time.Year, "#C1");
1149 Assert.AreEqual (4, time.Month, "#C2");
1150 Assert.AreEqual (6, time.Day, "#C3");
1151 Assert.AreEqual (4, time.Hour, "#C4");
1152 Assert.AreEqual (4, time.Second, "#C5");
1154 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1155 Assert.AreEqual (2002, time.Year, "#D1");
1156 Assert.AreEqual (4, time.Month, "#D2");
1157 Assert.AreEqual (6, time.Day, "#D3");
1158 Assert.AreEqual (4, time.Hour, "#D4");
1159 Assert.AreEqual (4, time.Second, "#D5");
1166 public void LastAccessTime ()
1168 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1169 if (File.Exists (path))
1171 FileStream stream = null;
1173 stream = File.Create (path);
1176 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1177 DateTime time = File.GetLastAccessTime (path);
1178 Assert.AreEqual (2002, time.Year, "#A1");
1179 Assert.AreEqual (4, time.Month, "#A2");
1180 Assert.AreEqual (6, time.Day, "#A3");
1181 Assert.AreEqual (4, time.Hour, "#A4");
1182 Assert.AreEqual (4, time.Second, "#A5");
1184 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1185 Assert.AreEqual (2002, time.Year, "#B1");
1186 Assert.AreEqual (4, time.Month, "#B2");
1187 Assert.AreEqual (6, time.Day, "#B3");
1188 Assert.AreEqual (4, time.Hour, "#B4");
1189 Assert.AreEqual (4, time.Second, "#B5");
1191 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1192 time = File.GetLastAccessTimeUtc (path);
1193 Assert.AreEqual (2002, time.Year, "#C1");
1194 Assert.AreEqual (4, time.Month, "#C2");
1195 Assert.AreEqual (6, time.Day, "#C3");
1196 Assert.AreEqual (4, time.Hour, "#C4");
1197 Assert.AreEqual (4, time.Second, "#C5");
1199 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1200 Assert.AreEqual (2002, time.Year, "#D1");
1201 Assert.AreEqual (4, time.Month, "#D2");
1202 Assert.AreEqual (6, time.Day, "#D3");
1203 Assert.AreEqual (4, time.Hour, "#D4");
1204 Assert.AreEqual (4, time.Second, "#D5");
1213 public void LastWriteTime ()
1215 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1216 if (File.Exists (path))
1218 FileStream stream = null;
1220 stream = File.Create (path);
1223 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1224 DateTime time = File.GetLastWriteTime (path);
1225 Assert.AreEqual (2002, time.Year, "#A1");
1226 Assert.AreEqual (4, time.Month, "#A2");
1227 Assert.AreEqual (6, time.Day, "#A3");
1228 Assert.AreEqual (4, time.Hour, "#A4");
1229 Assert.AreEqual (4, time.Second, "#A5");
1231 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1232 Assert.AreEqual (2002, time.Year, "#B1");
1233 Assert.AreEqual (4, time.Month, "#B2");
1234 Assert.AreEqual (6, time.Day, "#B3");
1235 Assert.AreEqual (4, time.Hour, "#B4");
1236 Assert.AreEqual (4, time.Second, "#B5");
1238 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1239 time = File.GetLastWriteTimeUtc (path);
1240 Assert.AreEqual (2002, time.Year, "#C1");
1241 Assert.AreEqual (4, time.Month, "#C2");
1242 Assert.AreEqual (6, time.Day, "#C3");
1243 Assert.AreEqual (4, time.Hour, "#C4");
1244 Assert.AreEqual (4, time.Second, "#C5");
1246 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1247 Assert.AreEqual (2002, time.Year, "#D1");
1248 Assert.AreEqual (4, time.Month, "#D2");
1249 Assert.AreEqual (6, time.Day, "#D3");
1250 Assert.AreEqual (4, time.Hour, "#D4");
1251 Assert.AreEqual (4, time.Second, "#D5");
1260 public void GetCreationTime_Path_Null ()
1263 File.GetCreationTime (null as string);
1265 } catch (ArgumentNullException ex) {
1266 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1267 Assert.IsNull (ex.InnerException, "#3");
1268 Assert.IsNotNull (ex.Message, "#4");
1269 Assert.AreEqual ("path", ex.ParamName, "#5");
1274 public void GetCreationTime_Path_Empty ()
1277 File.GetCreationTime (string.Empty);
1279 } catch (ArgumentException ex) {
1280 // Empty file name is not legal
1281 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1282 Assert.IsNull (ex.InnerException, "#3");
1283 Assert.IsNotNull (ex.Message, "#4");
1284 Assert.IsNull (ex.ParamName, "#5");
1289 public void GetCreationTime_Path_DoesNotExist ()
1291 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1294 DateTime time = File.GetCreationTime (path);
1295 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1296 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1297 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1298 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1299 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1300 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1301 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1305 public void GetCreationTime_Path_Whitespace ()
1308 File.GetCreationTime (" ");
1310 } catch (ArgumentException ex) {
1311 // The path is not of a legal form
1312 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1313 Assert.IsNull (ex.InnerException, "#3");
1314 Assert.IsNotNull (ex.Message, "#4");
1315 Assert.IsNull (ex.ParamName, "#5");
1320 public void GetCreationTime_Path_InvalidPathChars ()
1323 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1325 } catch (ArgumentException ex) {
1326 // Illegal characters in path
1327 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1328 Assert.IsNull (ex.InnerException, "#3");
1329 Assert.IsNotNull (ex.Message, "#4");
1330 Assert.IsNull (ex.ParamName, "#5");
1335 public void GetCreationTimeUtc_Path_Null ()
1338 File.GetCreationTimeUtc (null as string);
1340 } catch (ArgumentNullException ex) {
1341 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1342 Assert.IsNull (ex.InnerException, "#3");
1343 Assert.IsNotNull (ex.Message, "#4");
1344 Assert.AreEqual ("path", ex.ParamName, "#5");
1349 public void GetCreationTimeUtc_Path_Empty ()
1352 File.GetCreationTimeUtc (string.Empty);
1354 } catch (ArgumentException ex) {
1355 // Empty file name is not legal
1356 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1357 Assert.IsNull (ex.InnerException, "#3");
1358 Assert.IsNotNull (ex.Message, "#4");
1359 Assert.IsNull (ex.ParamName, "#5");
1364 public void GetCreationTimeUtc_Path_DoesNotExist ()
1366 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1369 DateTime time = File.GetCreationTimeUtc (path);
1370 Assert.AreEqual (1601, time.Year, "#1");
1371 Assert.AreEqual (1, time.Month, "#2");
1372 Assert.AreEqual (1, time.Day, "#3");
1373 Assert.AreEqual (0, time.Hour, "#4");
1374 Assert.AreEqual (0, time.Second, "#5");
1375 Assert.AreEqual (0, time.Millisecond, "#6");
1379 public void GetCreationTimeUtc_Path_Whitespace ()
1382 File.GetCreationTimeUtc (" ");
1384 } catch (ArgumentException ex) {
1385 // The path is not of a legal form
1386 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1387 Assert.IsNull (ex.InnerException, "#3");
1388 Assert.IsNotNull (ex.Message, "#4");
1389 Assert.IsNull (ex.ParamName, "#5");
1394 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1397 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1399 } catch (ArgumentException ex) {
1400 // Illegal characters in path
1401 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1402 Assert.IsNull (ex.InnerException, "#3");
1403 Assert.IsNotNull (ex.Message, "#4");
1404 Assert.IsNull (ex.ParamName, "#5");
1409 public void GetLastAccessTime_Path_Null ()
1412 File.GetLastAccessTime (null as string);
1414 } catch (ArgumentNullException ex) {
1415 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1416 Assert.IsNull (ex.InnerException, "#3");
1417 Assert.IsNotNull (ex.Message, "#4");
1418 Assert.AreEqual ("path", ex.ParamName, "#5");
1423 public void GetLastAccessTime_Path_Empty ()
1426 File.GetLastAccessTime (string.Empty);
1428 } catch (ArgumentException ex) {
1429 // Empty file name is not legal
1430 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1431 Assert.IsNull (ex.InnerException, "#3");
1432 Assert.IsNotNull (ex.Message, "#4");
1433 Assert.IsNull (ex.ParamName, "#5");
1438 public void GetLastAccessTime_Path_DoesNotExist ()
1440 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1443 DateTime time = File.GetLastAccessTime (path);
1444 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1445 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1446 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1447 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1448 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1449 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1450 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1454 public void GetLastAccessTime_Path_Whitespace ()
1457 File.GetLastAccessTime (" ");
1459 } catch (ArgumentException ex) {
1460 // The path is not of a legal form
1461 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1462 Assert.IsNull (ex.InnerException, "#3");
1463 Assert.IsNotNull (ex.Message, "#4");
1464 Assert.IsNull (ex.ParamName, "#5");
1469 public void GetLastAccessTime_Path_InvalidPathChars ()
1472 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1474 } catch (ArgumentException ex) {
1475 // Illegal characters in path
1476 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1477 Assert.IsNull (ex.InnerException, "#3");
1478 Assert.IsNotNull (ex.Message, "#4");
1479 Assert.IsNull (ex.ParamName, "#5");
1484 public void GetLastAccessTimeUtc_Path_Null ()
1487 File.GetLastAccessTimeUtc (null as string);
1489 } catch (ArgumentNullException ex) {
1490 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1491 Assert.IsNull (ex.InnerException, "#3");
1492 Assert.IsNotNull (ex.Message, "#4");
1493 Assert.AreEqual ("path", ex.ParamName, "#5");
1498 public void GetLastAccessTimeUtc_Path_Empty ()
1501 File.GetLastAccessTimeUtc (string.Empty);
1503 } catch (ArgumentException ex) {
1504 // Empty file name is not legal
1505 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1506 Assert.IsNull (ex.InnerException, "#3");
1507 Assert.IsNotNull (ex.Message, "#4");
1508 Assert.IsNull (ex.ParamName, "#5");
1513 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1515 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1518 DateTime time = File.GetLastAccessTimeUtc (path);
1519 Assert.AreEqual (1601, time.Year, "#1");
1520 Assert.AreEqual (1, time.Month, "#2");
1521 Assert.AreEqual (1, time.Day, "#3");
1522 Assert.AreEqual (0, time.Hour, "#4");
1523 Assert.AreEqual (0, time.Second, "#5");
1524 Assert.AreEqual (0, time.Millisecond, "#6");
1528 public void GetLastAccessTimeUtc_Path_Whitespace ()
1531 File.GetLastAccessTimeUtc (" ");
1533 } catch (ArgumentException ex) {
1534 // The path is not of a legal form
1535 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1536 Assert.IsNull (ex.InnerException, "#3");
1537 Assert.IsNotNull (ex.Message, "#4");
1538 Assert.IsNull (ex.ParamName, "#5");
1543 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1546 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1548 } catch (ArgumentException ex) {
1549 // Illegal characters in path
1550 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1551 Assert.IsNull (ex.InnerException, "#3");
1552 Assert.IsNotNull (ex.Message, "#4");
1553 Assert.IsNull (ex.ParamName, "#5");
1558 public void GetLastWriteTime_Path_Null ()
1561 File.GetLastWriteTime (null as string);
1563 } catch (ArgumentNullException ex) {
1564 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1565 Assert.IsNull (ex.InnerException, "#3");
1566 Assert.IsNotNull (ex.Message, "#4");
1567 Assert.AreEqual ("path", ex.ParamName, "#5");
1572 public void GetLastWriteTime_Path_Empty ()
1575 File.GetLastWriteTime (string.Empty);
1577 } catch (ArgumentException ex) {
1578 // Empty file name is not legal
1579 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1580 Assert.IsNull (ex.InnerException, "#3");
1581 Assert.IsNotNull (ex.Message, "#4");
1582 Assert.IsNull (ex.ParamName, "#5");
1587 public void GetLastWriteTime_Path_DoesNotExist ()
1589 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1592 DateTime time = File.GetLastWriteTime (path);
1593 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1594 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1595 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1596 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1597 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1598 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1599 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1603 public void GetLastWriteTime_Path_Whitespace ()
1606 File.GetLastWriteTime (" ");
1608 } catch (ArgumentException ex) {
1609 // The path is not of a legal form
1610 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1611 Assert.IsNull (ex.InnerException, "#3");
1612 Assert.IsNotNull (ex.Message, "#4");
1613 Assert.IsNull (ex.ParamName, "#5");
1618 public void GetLastWriteTime_Path_InvalidPathChars ()
1621 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1623 } catch (ArgumentException ex) {
1624 // Illegal characters in path
1625 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1626 Assert.IsNull (ex.InnerException, "#3");
1627 Assert.IsNotNull (ex.Message, "#4");
1628 Assert.IsNull (ex.ParamName, "#5");
1633 public void GetLastWriteTimeUtc_Path_Null ()
1636 File.GetLastWriteTimeUtc (null as string);
1638 } catch (ArgumentNullException ex) {
1639 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1640 Assert.IsNull (ex.InnerException, "#3");
1641 Assert.IsNotNull (ex.Message, "#4");
1642 Assert.AreEqual ("path", ex.ParamName, "#5");
1647 public void GetLastWriteTimeUtc_Path_Empty ()
1650 File.GetLastWriteTimeUtc (string.Empty);
1652 } catch (ArgumentException ex) {
1653 // Empty file name is not legal
1654 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1655 Assert.IsNull (ex.InnerException, "#3");
1656 Assert.IsNotNull (ex.Message, "#4");
1657 Assert.IsNull (ex.ParamName, "#5");
1662 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1664 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1667 DateTime time = File.GetLastWriteTimeUtc (path);
1668 Assert.AreEqual (1601, time.Year, "#1");
1669 Assert.AreEqual (1, time.Month, "#2");
1670 Assert.AreEqual (1, time.Day, "#3");
1671 Assert.AreEqual (0, time.Hour, "#4");
1672 Assert.AreEqual (0, time.Second, "#5");
1673 Assert.AreEqual (0, time.Millisecond, "#6");
1677 public void GetLastWriteTimeUtc_Path_Whitespace ()
1680 File.GetLastWriteTimeUtc (" ");
1682 } catch (ArgumentException ex) {
1683 // The path is not of a legal form
1684 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1685 Assert.IsNull (ex.InnerException, "#3");
1686 Assert.IsNotNull (ex.Message, "#4");
1687 Assert.IsNull (ex.ParamName, "#5");
1692 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1695 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1697 } catch (ArgumentException ex) {
1698 // Illegal characters in path
1699 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1700 Assert.IsNull (ex.InnerException, "#3");
1701 Assert.IsNotNull (ex.Message, "#4");
1702 Assert.IsNull (ex.ParamName, "#5");
1707 public void FileStreamClose ()
1709 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1710 FileStream stream = null;
1712 stream = File.Create (path);
1722 // SetCreationTime and SetCreationTimeUtc exceptions
1725 public void SetCreationTime_Path_Null ()
1728 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1730 } catch (ArgumentNullException ex) {
1731 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1732 Assert.IsNull (ex.InnerException, "#3");
1733 Assert.IsNotNull (ex.Message, "#4");
1734 Assert.AreEqual ("path", ex.ParamName, "#5");
1739 public void SetCreationTime_Path_Empty ()
1742 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1744 } catch (ArgumentException ex) {
1745 // Empty file name is not legal
1746 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1747 Assert.IsNull (ex.InnerException, "#3");
1748 Assert.IsNotNull (ex.Message, "#4");
1749 Assert.IsNull (ex.ParamName, "#5");
1754 public void SetCreationTime_Path_Whitespace ()
1757 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1759 } catch (ArgumentException ex) {
1760 // The path is not of a legal form
1761 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1762 Assert.IsNull (ex.InnerException, "#3");
1763 Assert.IsNotNull (ex.Message, "#4");
1764 Assert.IsNull (ex.ParamName, "#5");
1769 public void SetCreationTime_Path_InvalidPathChars ()
1771 // On Unix there are no invalid path chars.
1772 if (Path.InvalidPathChars.Length > 1) {
1774 File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1775 new DateTime (2000, 12, 12, 11, 59, 59));
1777 } catch (ArgumentException ex) {
1778 // Illegal characters in path
1779 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1780 Assert.IsNull (ex.InnerException, "#3");
1781 Assert.IsNotNull (ex.Message, "#4");
1782 Assert.IsNull (ex.ParamName, "#5");
1788 public void SetCreationTime_Path_DoesNotExist ()
1790 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1794 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1796 } catch (FileNotFoundException ex) {
1797 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1798 Assert.AreEqual (path, ex.FileName, "#3");
1799 Assert.IsNull (ex.InnerException, "#4");
1800 Assert.IsNotNull (ex.Message, "#5");
1805 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1806 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1808 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1809 // FileStream stream = null;
1810 // DeleteFile (path);
1812 // stream = File.Create (path);
1814 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1816 // if (stream != null)
1818 // DeleteFile (path);
1823 public void SetCreationTime_FileLock ()
1825 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1827 FileStream stream = null;
1829 stream = File.Create (path);
1831 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1833 } catch (IOException ex) {
1834 // The process cannot access the file '...'
1835 // because it is being used by another process
1836 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1837 Assert.IsNull (ex.InnerException, "#3");
1838 Assert.IsNotNull (ex.Message, "#4");
1839 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1849 public void SetCreationTimeUtc_Path_Null ()
1852 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1854 } catch (ArgumentNullException ex) {
1855 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1856 Assert.IsNull (ex.InnerException, "#3");
1857 Assert.IsNotNull (ex.Message, "#4");
1858 Assert.AreEqual ("path", ex.ParamName, "#5");
1863 public void SetCreationTimeUtc_Path_Empty ()
1866 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1868 } catch (ArgumentException ex) {
1869 // Empty file name is not legal
1870 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1871 Assert.IsNull (ex.InnerException, "#3");
1872 Assert.IsNotNull (ex.Message, "#4");
1873 Assert.IsNull (ex.ParamName, "#5");
1878 public void SetCreationTimeUtc_Path_Whitespace ()
1881 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1883 } catch (ArgumentException ex) {
1884 // The path is not of a legal form
1885 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1886 Assert.IsNull (ex.InnerException, "#3");
1887 Assert.IsNotNull (ex.Message, "#4");
1888 Assert.IsNull (ex.ParamName, "#5");
1893 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1895 // On Unix there are no invalid path chars.
1896 if (Path.InvalidPathChars.Length > 1) {
1898 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1899 new DateTime (2000, 12, 12, 11, 59, 59));
1901 } catch (ArgumentException ex) {
1902 // Illegal characters in path
1903 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1904 Assert.IsNull (ex.InnerException, "#3");
1905 Assert.IsNotNull (ex.Message, "#4");
1906 Assert.IsNull (ex.ParamName, "#5");
1912 public void SetCreationTimeUtc_Path_DoesNotExist ()
1914 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1918 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1920 } catch (FileNotFoundException ex) {
1921 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1922 Assert.AreEqual (path, ex.FileName, "#3");
1923 Assert.IsNull (ex.InnerException, "#3");
1924 Assert.IsNotNull (ex.Message, "#4");
1929 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1930 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1932 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1933 // DeleteFile (path);
1934 // FileStream stream = null;
1936 // stream = File.Create (path);
1938 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1940 // if (stream != null)
1942 // DeleteFile (path);
1947 public void SetCreationTimeUtc_FileLock ()
1949 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1951 FileStream stream = null;
1953 stream = File.Create (path);
1955 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1957 } catch (IOException ex) {
1958 // The process cannot access the file "..."
1959 // because it is being used by another process
1960 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1961 Assert.IsNull (ex.InnerException, "#3");
1962 Assert.IsNotNull (ex.Message, "#4");
1963 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1972 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1975 public void SetLastAccessTime_Path_Null ()
1978 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1980 } catch (ArgumentNullException ex) {
1981 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1982 Assert.IsNull (ex.InnerException, "#3");
1983 Assert.IsNotNull (ex.Message, "#4");
1984 Assert.AreEqual ("path", ex.ParamName, "#5");
1989 public void SetLastAccessTime_Path_Empty ()
1992 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1994 } catch (ArgumentException ex) {
1995 // Empty file name is not legal
1996 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1997 Assert.IsNull (ex.InnerException, "#3");
1998 Assert.IsNotNull (ex.Message, "#4");
1999 Assert.IsNull (ex.ParamName, "#5");
2004 public void SetLastAccessTime_Path_Whitespace ()
2007 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2009 } catch (ArgumentException ex) {
2010 // The path is not of a legal form
2011 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2012 Assert.IsNull (ex.InnerException, "#3");
2013 Assert.IsNotNull (ex.Message, "#4");
2014 Assert.IsNull (ex.ParamName, "#5");
2019 public void SetLastAccessTime_Path_InvalidPathChars ()
2021 // On Unix there are no invalid path chars.
2022 if (Path.InvalidPathChars.Length > 1) {
2024 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2025 new DateTime (2000, 12, 12, 11, 59, 59));
2027 } catch (ArgumentException ex) {
2028 // Illegal characters in path
2029 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2030 Assert.IsNull (ex.InnerException, "#3");
2031 Assert.IsNotNull (ex.Message, "#4");
2032 Assert.IsNull (ex.ParamName, "#5");
2038 public void SetLastAccessTime_Path_DoesNotExist ()
2040 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2044 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2046 } catch (FileNotFoundException ex) {
2047 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2048 Assert.AreEqual (path, ex.FileName, "#3");
2049 Assert.IsNull (ex.InnerException, "#4");
2050 Assert.IsNotNull (ex.Message, "#5");
2055 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2056 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2058 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2059 // DeleteFile (path);
2060 // FileStream stream = null;
2062 // stream = File.Create (path);
2064 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2066 // if (stream != null)
2068 // DeleteFile (path);
2073 public void SetLastAccessTime_FileLock ()
2075 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2077 FileStream stream = null;
2079 stream = File.Create (path);
2081 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2083 } catch (IOException ex) {
2084 // The process cannot access the file "..."
2085 // because it is being used by another process
2086 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2087 Assert.IsNull (ex.InnerException, "#3");
2088 Assert.IsNotNull (ex.Message, "#4");
2089 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2099 public void SetLastAccessTimeUtc_Path_Null ()
2102 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2104 } catch (ArgumentNullException ex) {
2105 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2106 Assert.IsNull (ex.InnerException, "#3");
2107 Assert.IsNotNull (ex.Message, "#4");
2108 Assert.AreEqual ("path", ex.ParamName, "#5");
2113 public void SetCLastAccessTimeUtc_Path_Empty ()
2116 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2118 } catch (ArgumentException ex) {
2119 // Empty file name is not legal
2120 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2121 Assert.IsNull (ex.InnerException, "#3");
2122 Assert.IsNotNull (ex.Message, "#4");
2123 Assert.IsNull (ex.ParamName, "#5");
2128 public void SetLastAccessTimeUtc_Path_Whitespace ()
2131 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2133 } catch (ArgumentException ex) {
2134 // The path is not of a legal form
2135 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2136 Assert.IsNull (ex.InnerException, "#3");
2137 Assert.IsNotNull (ex.Message, "#4");
2138 Assert.IsNull (ex.ParamName, "#5");
2143 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2145 // On Unix there are no invalid path chars.
2146 if (Path.InvalidPathChars.Length > 1) {
2148 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2149 new DateTime (2000, 12, 12, 11, 59, 59));
2151 } catch (ArgumentException ex) {
2152 // Illegal characters in path
2153 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2154 Assert.IsNull (ex.InnerException, "#3");
2155 Assert.IsNotNull (ex.Message, "#4");
2156 Assert.IsNull (ex.ParamName, "#5");
2162 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2164 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2168 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2170 } catch (FileNotFoundException ex) {
2171 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2172 Assert.AreEqual (path, ex.FileName, "#3");
2173 Assert.IsNull (ex.InnerException, "#4");
2174 Assert.IsNotNull (ex.Message, "#5");
2179 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2180 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2182 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2183 // DeleteFile (path);
2184 // FileStream stream = null;
2186 // stream = File.Create (path);
2188 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2190 // if (stream != null)
2192 // DeleteFile (path);
2197 public void SetLastAccessTimeUtc_FileLock ()
2199 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2201 FileStream stream = null;
2203 stream = File.Create (path);
2205 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2207 } catch (IOException ex) {
2208 // The process cannot access the file "..."
2209 // because it is being used by another process
2210 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2211 Assert.IsNull (ex.InnerException, "#3");
2212 Assert.IsNotNull (ex.Message, "#4");
2213 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2222 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2225 public void SetLastWriteTime_Path_Null ()
2228 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2230 } catch (ArgumentNullException ex) {
2231 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2232 Assert.IsNull (ex.InnerException, "#3");
2233 Assert.IsNotNull (ex.Message, "#4");
2234 Assert.AreEqual ("path", ex.ParamName, "#5");
2239 public void SetLastWriteTime_Path_Empty ()
2242 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2244 } catch (ArgumentException ex) {
2245 // Empty file name is not legal
2246 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2247 Assert.IsNull (ex.InnerException, "#3");
2248 Assert.IsNotNull (ex.Message, "#4");
2249 Assert.IsNull (ex.ParamName, "#5");
2254 public void SetLastWriteTime_Path_Whitespace ()
2257 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2259 } catch (ArgumentException ex) {
2260 // The path is not of a legal form
2261 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2262 Assert.IsNull (ex.InnerException, "#3");
2263 Assert.IsNotNull (ex.Message, "#4");
2264 Assert.IsNull (ex.ParamName, "#5");
2269 public void SetLastWriteTime_Path_InvalidPathChars ()
2271 // On Unix there are no invalid path chars.
2272 if (Path.InvalidPathChars.Length > 1) {
2274 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2275 new DateTime (2000, 12, 12, 11, 59, 59));
2277 } catch (ArgumentException ex) {
2278 // Illegal characters in path
2279 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2280 Assert.IsNull (ex.InnerException, "#3");
2281 Assert.IsNotNull (ex.Message, "#4");
2282 Assert.IsNull (ex.ParamName, "#5");
2288 public void SetLastWriteTime_Path_DoesNotExist ()
2290 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2294 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2296 } catch (FileNotFoundException ex) {
2297 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2298 Assert.AreEqual (path, ex.FileName, "#3");
2299 Assert.IsNull (ex.InnerException, "#4");
2300 Assert.IsNotNull (ex.Message, "#5");
2305 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2306 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2308 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2309 // DeleteFile (path);
2310 // FileStream stream = null;
2312 // stream = File.Create (path);
2314 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2316 // if (stream != null)
2318 // DeleteFile (path);
2323 public void SetLastWriteTime_FileLock ()
2325 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2327 FileStream stream = null;
2329 stream = File.Create (path);
2331 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2333 } catch (IOException ex) {
2334 // The process cannot access the file '...'
2335 // because it is being used by another process
2336 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2337 Assert.IsNull (ex.InnerException, "#3");
2338 Assert.IsNotNull (ex.Message, "#4");
2339 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2349 public void SetLastWriteTimeUtc_Path_Null ()
2352 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2354 } catch (ArgumentNullException ex) {
2355 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2356 Assert.IsNull (ex.InnerException, "#3");
2357 Assert.IsNotNull (ex.Message, "#4");
2358 Assert.AreEqual ("path", ex.ParamName, "#5");
2363 public void SetLastWriteTimeUtc_Path_Empty ()
2366 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2368 } catch (ArgumentException ex) {
2369 // Empty file name is not legal
2370 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2371 Assert.IsNull (ex.InnerException, "#3");
2372 Assert.IsNotNull (ex.Message, "#4");
2373 Assert.IsNull (ex.ParamName, "#5");
2378 public void SetLastWriteTimeUtc_Path_Whitespace ()
2381 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2383 } catch (ArgumentException ex) {
2384 // The path is not of a legal form
2385 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2386 Assert.IsNull (ex.InnerException, "#3");
2387 Assert.IsNotNull (ex.Message, "#4");
2388 Assert.IsNull (ex.ParamName, "#5");
2393 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2395 // On Unix there are no invalid path chars.
2396 if (Path.InvalidPathChars.Length > 1) {
2398 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2399 new DateTime (2000, 12, 12, 11, 59, 59));
2401 } catch (ArgumentException ex) {
2402 // Illegal characters in path
2403 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2404 Assert.IsNull (ex.InnerException, "#3");
2405 Assert.IsNotNull (ex.Message, "#4");
2406 Assert.IsNull (ex.ParamName, "#5");
2412 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2414 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2418 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2420 } catch (FileNotFoundException ex) {
2421 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2422 Assert.AreEqual (path, ex.FileName, "#3");
2423 Assert.IsNull (ex.InnerException, "#4");
2424 Assert.IsNotNull (ex.Message, "#5");
2429 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2430 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2432 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2433 // DeleteFile (path);
2434 // FileStream stream = null;
2436 // stream = File.Create (path);
2438 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2440 // if (stream != null)
2442 // DeleteFile (path);
2447 public void SetLastWriteTimeUtc_FileLock ()
2449 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2451 FileStream stream = null;
2453 stream = File.Create (path);
2455 File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2457 } catch (IOException ex) {
2458 // The process cannot access the file '...'
2459 // because it is being used by another process
2460 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2461 Assert.IsNull (ex.InnerException, "#3");
2462 Assert.IsNotNull (ex.Message, "#4");
2463 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2473 public void OpenAppend ()
2475 string fn = Path.GetTempFileName ();
2476 using (FileStream s = File.Open (fn, FileMode.Append)) {
2481 void Position (long value)
2483 using (FileStream fs = File.OpenRead (testfile)) {
2484 fs.Position = value;
2485 Assert.AreEqual (value, fs.Position, "Position");
2486 Assert.AreEqual (1, fs.Length, "Length");
2491 public void Position_Small ()
2493 Position (Int32.MaxValue);
2497 [Category ("LargeFileSupport")]
2498 public void Position_Large ()
2500 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2501 Position ((long) Int32.MaxValue + 1);
2504 void Seek (long value)
2506 using (FileStream fs = File.OpenRead (testfile)) {
2507 fs.Seek (value, SeekOrigin.Begin);
2508 Assert.AreEqual (value, fs.Position, "Position");
2509 Assert.AreEqual (1, fs.Length, "Length");
2514 public void Seek_Small ()
2516 Seek (Int32.MaxValue);
2520 [Category ("LargeFileSupport")]
2521 public void Seek_Large ()
2523 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2524 Seek ((long) Int32.MaxValue + 1);
2527 void LockUnlock (long value)
2529 using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2530 fs.Lock (value - 1, 1);
2531 fs.Unlock (value - 1, 1);
2534 fs.Unlock (0, value);
2539 public void Lock_Small ()
2541 LockUnlock ((long) Int32.MaxValue);
2545 [Category ("LargeFileSupport")]
2546 public void Lock_Large ()
2548 // note: already worked without HAVE_LARGE_FILE_SUPPORT
2549 LockUnlock ((long) Int32.MaxValue + 1);
2553 public void ReadWriteAllText ()
2555 // The MSDN docs said something about
2556 // not including a final new line. it looks
2557 // like that was not true. I'm not sure what
2558 // that was talking about
2559 read_all (string.Empty);
2568 read_all ("a\r\na");
2572 read_all ("\r\n\r\n");
2576 public void ReplaceTest ()
2578 string tmp = Path.Combine (TempFolder, "ReplaceTest");
2579 Directory.CreateDirectory (tmp);
2580 string origFile = Path.Combine (tmp, "origFile");
2581 string replaceFile = Path.Combine (tmp, "replaceFile");
2582 string backupFile = Path.Combine (tmp, "backupFile");
2584 using (StreamWriter sw = File.CreateText (origFile)) {
2585 sw.WriteLine ("origFile");
2587 using (StreamWriter sw = File.CreateText (replaceFile)) {
2588 sw.WriteLine ("replaceFile");
2590 using (StreamWriter sw = File.CreateText (backupFile)) {
2591 sw.WriteLine ("backupFile");
2594 File.Replace (origFile, replaceFile, backupFile);
2595 Assert.IsFalse (File.Exists (origFile), "#1");
2596 using (StreamReader sr = File.OpenText (replaceFile)) {
2597 string txt = sr.ReadLine ();
2598 Assert.AreEqual ("origFile", txt, "#2");
2600 using (StreamReader sr = File.OpenText (backupFile)) {
2601 string txt = sr.ReadLine ();
2602 Assert.AreEqual ("replaceFile", txt, "#3");
2606 static bool RunningOnUnix {
2608 int p = (int) Environment.OSVersion.Platform;
2609 return ((p == 4) || (p == 128) || (p == 6));
2613 void DeleteFile (string path)
2615 if (File.Exists (path))
2619 void DeleteDirectory (string path)
2621 if (Directory.Exists (path))
2622 Directory.Delete (path, true);
2625 void read_all (string s)
2627 string f = Path.GetTempFileName ();
2629 File.WriteAllText (f, s);
2630 string r = File.ReadAllText (f);
2631 Assert.AreEqual (s, r);
2637 void MoveTest (FileAccess acc, FileShare share, bool works)
2639 // use TEMP so since the default location (right along with the assemblies)
2640 // will get access denied when running under some environment (e.g. iOS devices)
2641 var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
2643 File.Delete (file + ".old");
2644 using (var v = File.Create (file)) { }
2646 using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2648 File.Move(file, file + ".old");
2650 Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
2651 } catch (IOException) {
2653 Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
2659 public void MoveTest ()
2661 MoveTest (FileAccess.Read, FileShare.None, false);
2662 MoveTest (FileAccess.Read, FileShare.Read, false);
2663 MoveTest (FileAccess.Read, FileShare.Write, false);
2664 MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2665 MoveTest (FileAccess.Read, FileShare.Delete, true);
2666 MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2667 MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2668 MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2669 MoveTest (FileAccess.Write, FileShare.None, false);
2670 MoveTest (FileAccess.Write, FileShare.Read, false);
2671 MoveTest (FileAccess.Write, FileShare.Write, false);
2672 MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2673 MoveTest (FileAccess.Write, FileShare.Delete, true);
2674 MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2675 MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2676 MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2677 MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2678 MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2679 MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2680 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2681 MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2682 MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2683 MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2684 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
2688 [DllImport ("libc", SetLastError=true)]
2689 public static extern int symlink (string oldpath, string newpath);
2691 [DllImport ("libc", SetLastError=true)]
2692 public static extern int rename (string oldpath, string newpath);
2694 /* This deserves some examplation:
2695 Calling File.Delete on a bad symlink will throw UnauthorizedAccessException so they are highlander files to managed code.
2696 The hack here is to break the cycle and make them dangling symlinks instead.
2698 static void DeleteBadSymLink (string path) {
2703 newpath = path + random.Next ();
2704 } while (File.Exists (newpath));
2707 rename (path, newpath);
2708 File.Delete (newpath);
2712 public void SymLinkLoop ()
2715 Assert.Ignore ("Symlink are hard on windows");
2716 string name1, name2;
2718 name1 = "first_file" + random.Next ();
2719 name2 = "second_file" + random.Next ();
2722 var path1 = Path.Combine (Path.GetTempPath (), name1);
2723 var path2 = Path.Combine (Path.GetTempPath (), name2);
2725 DeleteBadSymLink (path1);
2726 DeleteBadSymLink (path2);
2728 symlink (path1, path2);
2729 symlink (path2, path1);
2731 Assert.IsFalse (File.Exists (path1), "File.Exists must return false for symlink loops");
2734 using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
2735 Assert.Fail ("File.Exists must throw for symlink loops");
2737 } catch (UnauthorizedAccessException ex) {
2738 Assert.AreEqual (0x80070005u, (uint)ex.HResult, "Ensure HRESULT is correct");
2743 File.Delete (path1);
2744 } catch (UnauthorizedAccessException ex) {
2745 Assert.AreEqual (0x80070005u, (uint)ex.HResult, "Ensure HRESULT is correct");
2749 DeleteBadSymLink (path1);
2750 DeleteBadSymLink (path2);