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");
33 DeleteDirectory (TempFolder);
34 Directory.CreateDirectory (TempFolder);
35 old_culture = Thread.CurrentThread.CurrentCulture;
36 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
40 public void TearDown ()
42 DeleteDirectory (TempFolder);
43 Thread.CurrentThread.CurrentCulture = old_culture;
50 public void FixtureSetUp ()
52 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
53 testfile = Path.Combine (path, "FileStreamTest.dat");
54 File.WriteAllText (testfile, "1");
58 public void FixtureTearDown ()
60 if (File.Exists (testfile))
61 File.Delete (testfile);
65 public void TestExists ()
68 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
70 Assert.IsFalse (File.Exists (null), "#1");
71 Assert.IsFalse (File.Exists (string.Empty), "#2");
72 Assert.IsFalse (File.Exists (" \t\t \t \n\t\n \n"), "#3");
74 s = File.Create (path);
76 Assert.IsTrue (File.Exists (path), "#4");
77 Assert.IsFalse (File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
86 public void Exists_InvalidFileName ()
88 Assert.IsFalse (File.Exists ("><|"), "#1");
89 Assert.IsFalse (File.Exists ("?*"), "#2");
93 public void Exists_InvalidDirectory ()
95 Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
99 public void Create_Path_Null ()
104 } catch (ArgumentNullException ex) {
105 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
106 Assert.IsNull (ex.InnerException, "#3");
107 Assert.IsNotNull (ex.Message, "#4");
108 Assert.AreEqual ("path", ex.ParamName, "#5");
113 public void Create_Path_Directory ()
115 string path = Path.Combine (TempFolder, "foo");
116 Directory.CreateDirectory (path);
120 } catch (UnauthorizedAccessException ex) {
121 // Access to the path '...' is denied
122 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
123 Assert.IsNull (ex.InnerException, "#3");
124 Assert.IsNotNull (ex.Message, "#4");
125 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
127 DeleteDirectory (path);
132 public void Create_Path_Empty ()
135 File.Create (string.Empty);
137 } catch (ArgumentException ex) {
138 // Empty file name is not legal
139 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
140 Assert.IsNull (ex.InnerException, "#3");
141 Assert.IsNotNull (ex.Message, "#4");
142 Assert.IsNull (ex.ParamName, "#5");
147 public void Create_Path_ReadOnly ()
149 string path = Path.Combine (TempFolder, "foo");
150 File.Create (path).Close ();
151 File.SetAttributes (path, FileAttributes.ReadOnly);
155 } catch (UnauthorizedAccessException ex) {
156 // Access to the path '...' is denied
157 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
158 Assert.IsNull (ex.InnerException, "#3");
159 Assert.IsNotNull (ex.Message, "#4");
160 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
162 File.SetAttributes (path, FileAttributes.Normal);
167 public void Create_Path_Whitespace ()
172 } catch (ArgumentException ex) {
173 // The path is not of a legal form
174 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
175 Assert.IsNull (ex.InnerException, "#3");
176 Assert.IsNotNull (ex.Message, "#4");
177 Assert.IsNull (ex.ParamName, "#5");
182 public void Create_Directory_DoesNotExist ()
184 FileStream stream = null;
185 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
188 stream = File.Create (path);
190 } catch (DirectoryNotFoundException ex) {
191 // Could not find a part of the path "..."
192 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
193 Assert.IsNull (ex.InnerException, "#3");
194 Assert.IsNotNull (ex.Message, "#4");
195 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
204 public void Create ()
206 FileStream stream = null;
209 /* positive test: create resources/foo */
210 path = TempFolder + Path.DirectorySeparatorChar + "foo";
213 stream = File.Create (path);
214 Assert.IsTrue (File.Exists (path), "#1");
224 /* positive test: repeat test above again to test for overwriting file */
225 path = TempFolder + Path.DirectorySeparatorChar + "foo";
227 stream = File.Create (path);
228 Assert.IsTrue (File.Exists (path), "#2");
238 public void Copy_SourceFileName_Null ()
241 File.Copy (null, "b");
243 } catch (ArgumentNullException ex) {
244 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
245 Assert.IsNull (ex.InnerException, "#3");
246 Assert.IsNotNull (ex.Message, "#4");
247 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
252 public void Copy_DestFileName_Null ()
255 File.Copy ("a", null);
257 } catch (ArgumentNullException ex) {
258 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
259 Assert.IsNull (ex.InnerException, "#3");
260 Assert.IsNotNull (ex.Message, "#4");
261 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
266 public void Copy_SourceFileName_Empty ()
269 File.Copy (string.Empty, "b");
271 } catch (ArgumentException ex) {
272 // Empty file name is not legal
273 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
274 Assert.IsNull (ex.InnerException, "#3");
275 Assert.IsNotNull (ex.Message, "#4");
276 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
281 public void Copy_DestFileName_Empty ()
284 File.Copy ("a", string.Empty);
286 } catch (ArgumentException ex) {
287 // Empty file name is not legal
288 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
289 Assert.IsNull (ex.InnerException, "#3");
290 Assert.IsNotNull (ex.Message, "#4");
291 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
296 public void Copy_SourceFileName_Whitespace ()
299 File.Copy (" ", "b");
301 } catch (ArgumentException ex) {
302 // The path is not of a legal form
303 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
304 Assert.IsNull (ex.InnerException, "#3");
305 Assert.IsNotNull (ex.Message, "#4");
306 Assert.IsNull (ex.ParamName, "#5");
311 public void Copy_DestFileName_Whitespace ()
314 File.Copy ("a", " ");
316 } catch (ArgumentException ex) {
317 // The path is not of a legal form
318 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
319 Assert.IsNull (ex.InnerException, "#3");
320 Assert.IsNotNull (ex.Message, "#4");
321 Assert.IsNull (ex.ParamName, "#5");
326 public void Copy_SourceFileName_DoesNotExist ()
329 File.Copy ("doesnotexist", "b");
331 } catch (FileNotFoundException ex) {
332 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
333 Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
334 Assert.IsNull (ex.InnerException, "#4");
335 Assert.IsNotNull (ex.Message, "#5");
340 public void Copy_DestFileName_AlreadyExists ()
342 string source = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
343 string dest = TempFolder + Path.DirectorySeparatorChar + "bar";
347 File.Create (source).Close ();
348 File.Copy (source, dest);
350 File.Copy (source, dest);
352 } catch (IOException ex) {
353 // The file '...' already exists.
354 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
355 Assert.IsNull (ex.InnerException, "#3");
356 Assert.IsNotNull (ex.Message, "#4");
357 Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
366 public void Copy_SourceFileName_DestFileName_Same ()
368 string source = TempFolder + Path.DirectorySeparatorChar + "SameFile.txt";
372 File.Create (source).Close ();
374 File.Copy (source, source, true);
376 } catch (IOException ex) {
377 // process cannot access file ... because it is being used by another process
378 Assert.IsNull (ex.InnerException, "#2");
379 Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
389 string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
390 string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
391 /* positive test: copy resources/AFile.txt to resources/bar */
396 File.Create (path2).Close ();
397 File.Copy (path2, path1);
398 Assert.IsTrue (File.Exists (path2), "#A1");
399 Assert.IsTrue (File.Exists (path1), "#A2");
401 Assert.IsTrue (File.Exists (path1), "#B1");
402 File.Copy (path2, path1, true);
403 Assert.IsTrue (File.Exists (path2), "#B2");
404 Assert.IsTrue (File.Exists (path1), "#B3");
412 public void Delete_Path_Null ()
417 } catch (ArgumentNullException ex) {
418 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
419 Assert.IsNull (ex.InnerException, "#3");
420 Assert.IsNotNull (ex.Message, "#4");
421 Assert.AreEqual ("path", ex.ParamName, "#5");
426 public void Delete_Path_Empty ()
429 File.Delete (string.Empty);
431 } catch (ArgumentException ex) {
432 // Empty file name is not legal
433 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
434 Assert.IsNull (ex.InnerException, "#3");
435 Assert.IsNotNull (ex.Message, "#4");
436 Assert.IsNull (ex.ParamName, "#5");
441 public void Delete_Path_Whitespace ()
446 } catch (ArgumentException ex) {
447 // The path is not of a legal form
448 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
449 Assert.IsNull (ex.InnerException, "#3");
450 Assert.IsNotNull (ex.Message, "#4");
451 Assert.IsNull (ex.ParamName, "#5");
456 public void Delete_Directory_DoesNotExist ()
458 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
459 if (Directory.Exists (path))
460 Directory.Delete (path, true);
465 } catch (DirectoryNotFoundException ex) {
466 // Could not find a part of the path "..."
467 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
468 Assert.IsNull (ex.InnerException, "#3");
469 Assert.IsNotNull (ex.Message, "#4");
470 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
475 public void Delete ()
477 string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
478 DeleteFile (foopath);
480 File.Create (foopath).Close ();
481 File.Delete (foopath);
482 Assert.IsFalse (File.Exists (foopath));
483 File.Delete (foopath);
485 DeleteFile (foopath);
489 [Test] // bug #323389
490 [Category ("NotWorking")]
491 public void Delete_FileLock ()
493 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
495 FileStream stream = null;
497 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
501 } catch (IOException ex) {
502 // The process cannot access the file '...'
503 // because it is being used by another process
504 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
505 Assert.IsNull (ex.InnerException, "#3");
506 Assert.IsNotNull (ex.Message, "#4");
507 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
517 [ExpectedException (typeof(UnauthorizedAccessException))]
518 public void Delete_File_ReadOnly ()
521 Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
523 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
526 File.Create (path).Close ();
527 File.SetAttributes (path, FileAttributes.ReadOnly);
530 File.SetAttributes (path, FileAttributes.Normal);
536 public void Delete_NonExisting_NoException ()
538 File.Delete (Path.Combine (Directory.GetDirectoryRoot (Directory.GetCurrentDirectory ()), "monononexistingfile.dat"));
542 public void GetAttributes_Archive ()
545 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
547 FileAttributes attrs;
549 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
550 File.Create (path).Close ();
552 attrs = File.GetAttributes (path);
553 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
555 attrs &= ~FileAttributes.Archive;
556 File.SetAttributes (path, attrs);
558 attrs = File.GetAttributes (path);
559 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
563 public void GetAttributes_Default_File ()
566 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
568 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
569 File.Create (path).Close ();
571 FileAttributes attrs = File.GetAttributes (path);
573 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
574 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
575 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
576 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
577 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
578 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
582 public void GetAttributes_Default_Directory ()
584 FileAttributes attrs = File.GetAttributes (TempFolder);
586 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
587 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
588 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
589 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
590 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
591 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
595 public void GetAttributes_Directory ()
597 FileAttributes attrs = File.GetAttributes (TempFolder);
599 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
601 attrs &= ~FileAttributes.Directory;
602 File.SetAttributes (TempFolder, attrs);
604 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
606 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
607 File.Create (path).Close ();
609 attrs = File.GetAttributes (path);
610 attrs |= FileAttributes.Directory;
611 File.SetAttributes (path, attrs);
613 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
617 public void GetAttributes_ReadOnly ()
619 FileAttributes attrs;
621 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
622 File.Create (path).Close ();
624 attrs = File.GetAttributes (path);
625 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
628 attrs |= FileAttributes.ReadOnly;
629 File.SetAttributes (path, attrs);
631 attrs = File.GetAttributes (path);
632 Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
634 File.SetAttributes (path, FileAttributes.Normal);
639 public void GetAttributes_System ()
642 Assert.Ignore ("FileAttributes.System is not supported on Unix.");
644 FileAttributes attrs;
646 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
647 File.Create (path).Close ();
649 attrs = File.GetAttributes (path);
650 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
652 attrs |= FileAttributes.System;
653 File.SetAttributes (path, FileAttributes.System);
655 attrs = File.GetAttributes (path);
656 Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
660 public void GetAttributes_Path_DoesNotExist ()
662 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
664 File.GetAttributes (path);
666 } catch (FileNotFoundException ex) {
667 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
668 Assert.AreEqual (path, ex.FileName, "#3");
669 Assert.IsNull (ex.InnerException, "#4");
670 Assert.IsNotNull (ex.Message, "#5");
675 public void GetAttributes_Path_Empty ()
678 File.GetAttributes (string.Empty);
680 } catch (ArgumentException ex) {
681 // Empty file name is not legal
682 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
683 Assert.IsNull (ex.InnerException, "#3");
684 Assert.IsNotNull (ex.Message, "#4");
685 Assert.IsNull (ex.ParamName, "#5");
690 public void GetAttributes_Path_Null ()
693 File.GetAttributes (null);
695 } catch (ArgumentNullException ex) {
696 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
697 Assert.IsNull (ex.InnerException, "#3");
698 Assert.IsNotNull (ex.Message, "#4");
699 Assert.AreEqual ("path", ex.ParamName, "#5");
704 public void Move_SourceFileName_Null ()
707 File.Move (null, "b");
709 } catch (ArgumentNullException ex) {
710 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
711 Assert.IsNull (ex.InnerException, "#3");
712 Assert.IsNotNull (ex.Message, "#4");
713 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
718 public void Move_DestFileName_Null ()
721 File.Move ("a", null);
723 } catch (ArgumentNullException ex) {
724 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
725 Assert.IsNull (ex.InnerException, "#3");
726 Assert.IsNotNull (ex.Message, "#4");
727 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
732 public void Move_SourceFileName_Empty ()
735 File.Move (string.Empty, "b");
737 } catch (ArgumentException ex) {
738 // Empty file name is not legal
739 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
740 Assert.IsNull (ex.InnerException, "#3");
741 Assert.IsNotNull (ex.Message, "#4");
742 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
747 public void Move_DestFileName_Empty ()
750 File.Move ("a", string.Empty);
752 } catch (ArgumentException ex) {
753 // Empty file name is not legal
754 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
755 Assert.IsNull (ex.InnerException, "#3");
756 Assert.IsNotNull (ex.Message, "#4");
757 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
762 public void Move_SourceFileName_Whitespace ()
765 File.Move (" ", "b");
767 } catch (ArgumentException ex) {
768 // The path is not of a legal form
769 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
770 Assert.IsNull (ex.InnerException, "#3");
771 Assert.IsNotNull (ex.Message, "#4");
772 Assert.IsNull (ex.ParamName, "#5");
777 public void Move_DestFileName_Whitespace ()
780 File.Move ("a", " ");
782 } catch (ArgumentException ex) {
783 // The path is not of a legal form
784 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
785 Assert.IsNull (ex.InnerException, "#3");
786 Assert.IsNotNull (ex.Message, "#4");
787 Assert.IsNull (ex.ParamName, "#5");
792 public void Move_SourceFileName_DoesNotExist ()
794 string file = TempFolder + Path.DirectorySeparatorChar + "doesnotexist";
797 File.Move (file, "b");
799 } catch (FileNotFoundException ex) {
800 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
801 Assert.AreEqual (file, ex.FileName, "#3");
802 Assert.IsNull (ex.InnerException, "#4");
803 Assert.IsNotNull (ex.Message, "#5");
808 public void Move_DestFileName_DirectoryDoesNotExist ()
810 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
811 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
812 DeleteFile (sourceFile);
814 File.Create (sourceFile).Close ();
816 File.Move (sourceFile, destFile);
818 } catch (DirectoryNotFoundException ex) {
819 // Could not find a part of the path
820 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
821 Assert.IsNull (ex.InnerException, "#3");
822 Assert.IsNotNull (ex.Message, "#4");
823 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
826 DeleteFile (sourceFile);
831 public void Move_DestFileName_AlreadyExists ()
833 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
836 // move to same directory
837 File.Create (sourceFile).Close ();
839 File.Move (sourceFile, TempFolder);
841 } catch (IOException ex) {
842 // Cannot create a file when that file already exists
843 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
844 Assert.IsNull (ex.InnerException, "#A3");
845 Assert.IsNotNull (ex.Message, "#A4");
846 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
847 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
849 DeleteFile (sourceFile);
852 // move to exist file
853 File.Create (sourceFile).Close ();
854 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
855 File.Create (destFile).Close ();
857 File.Move (sourceFile, destFile);
859 } catch (IOException ex) {
860 // Cannot create a file when that file already exists
861 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
862 Assert.IsNull (ex.InnerException, "#B3");
863 Assert.IsNotNull (ex.Message, "#B4");
864 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
865 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
867 DeleteFile (sourceFile);
868 DeleteFile (destFile);
871 // move to existing directory
872 File.Create (sourceFile).Close ();
873 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
874 Directory.CreateDirectory (destFile);
876 File.Move (sourceFile, destFile);
878 } catch (IOException ex) {
879 // Cannot create a file when that file already exists
880 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
881 Assert.IsNull (ex.InnerException, "#C3");
882 Assert.IsNotNull (ex.Message, "#C4");
883 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
884 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
886 DeleteFile (sourceFile);
887 DeleteDirectory (destFile);
894 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
895 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
896 if (!File.Exists (bar)) {
897 FileStream f = File.Create(bar);
901 Assert.IsTrue (File.Exists (bar), "#1");
902 File.Move (bar, baz);
903 Assert.IsFalse (File.Exists (bar), "#2");
904 Assert.IsTrue (File.Exists (baz), "#3");
906 // Test moving of directories
907 string dir = Path.Combine (TempFolder, "dir");
908 string dir2 = Path.Combine (TempFolder, "dir2");
909 string dir_foo = Path.Combine (dir, "foo");
910 string dir2_foo = Path.Combine (dir2, "foo");
912 if (Directory.Exists (dir))
913 Directory.Delete (dir, true);
915 Directory.CreateDirectory (dir);
916 Directory.CreateDirectory (dir2);
917 File.Create (dir_foo).Close ();
918 File.Move (dir_foo, dir2_foo);
919 Assert.IsTrue (File.Exists (dir2_foo), "#4");
921 Directory.Delete (dir, true);
922 Directory.Delete (dir2, true);
923 DeleteFile (dir_foo);
924 DeleteFile (dir2_foo);
928 public void Move_FileLock ()
930 string sourceFile = Path.GetTempFileName ();
931 string destFile = Path.GetTempFileName ();
933 // source file locked
934 using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
936 File.Move (sourceFile, destFile);
938 } catch (IOException ex) {
939 // The process cannot access the file because
940 // it is being used by another process
941 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
942 Assert.IsNull (ex.InnerException, "#A3");
943 Assert.IsNotNull (ex.Message, "#A4");
947 // destination file locked
948 using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
950 File.Move (sourceFile, destFile);
952 } catch (IOException ex) {
953 // The process cannot access the file because
954 // it is being used by another process
955 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
956 Assert.IsNull (ex.InnerException, "#B3");
957 Assert.IsNotNull (ex.Message, "#B4");
966 FileStream stream = null;
968 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
970 if (!File.Exists (path))
971 stream = File.Create (path);
973 stream = File.Open (path, FileMode.Open);
983 if (!File.Exists (path))
984 File.Create (path).Close ();
986 stream = File.Open (path, FileMode.Open);
987 Assert.IsTrue (stream.CanRead, "#A1");
988 Assert.IsTrue (stream.CanSeek, "#A2");
989 Assert.IsTrue (stream.CanWrite, "#A3");
992 stream = File.Open (path, FileMode.Open, FileAccess.Write);
993 Assert.IsFalse (stream.CanRead, "#B1");
994 Assert.IsTrue (stream.CanSeek, "#B2");
995 Assert.IsTrue (stream.CanWrite, "#B3");
998 stream = File.Open (path, FileMode.Open, FileAccess.Read);
999 Assert.IsTrue (stream.CanRead, "#C1");
1000 Assert.IsTrue (stream.CanSeek, "#C2");
1001 Assert.IsFalse (stream.CanWrite, "#C3");
1011 /* Exception tests */
1012 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
1014 stream = File.Open (path, FileMode.Open);
1015 Assert.Fail ("#D1");
1016 } catch (FileNotFoundException ex) {
1017 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
1018 Assert.AreEqual (path, ex.FileName, "#D3");
1019 Assert.IsNull (ex.InnerException, "#D4");
1020 Assert.IsNotNull (ex.Message, "#D5");
1029 public void Open_CreateNewMode_ReadAccess ()
1031 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1032 FileStream stream = null;
1034 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1036 } catch (ArgumentException ex) {
1037 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1038 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1039 Assert.IsNull (ex.InnerException, "#3");
1040 Assert.IsNotNull (ex.Message, "#4");
1041 Assert.IsNull (ex.ParamName, "#5");
1050 public void Open_AppendMode_ReadAccess ()
1052 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1053 FileStream s = null;
1054 if (!File.Exists (path))
1055 File.Create (path).Close ();
1057 s = File.Open (path, FileMode.Append, FileAccess.Read);
1059 } catch (ArgumentException ex) {
1060 // Combining FileMode: Append with FileAccess: Read is invalid
1061 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1062 Assert.IsNull (ex.InnerException, "#3");
1063 Assert.IsNotNull (ex.Message, "#4");
1064 Assert.IsNull (ex.ParamName, "#5");
1073 public void OpenRead ()
1075 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1076 if (!File.Exists (path))
1077 File.Create (path).Close ();
1078 FileStream stream = null;
1081 stream = File.OpenRead (path);
1082 Assert.IsTrue (stream.CanRead, "#1");
1083 Assert.IsTrue (stream.CanSeek, "#2");
1084 Assert.IsFalse (stream.CanWrite, "#3");
1093 public void OpenWrite ()
1095 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1096 if (!File.Exists (path))
1097 File.Create (path).Close ();
1098 FileStream stream = null;
1101 stream = File.OpenWrite (path);
1102 Assert.IsFalse (stream.CanRead, "#1");
1103 Assert.IsTrue (stream.CanSeek, "#2");
1104 Assert.IsTrue (stream.CanWrite, "#3");
1114 public void TestGetCreationTime ()
1116 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1120 File.Create (path).Close();
1121 DateTime time = File.GetCreationTime (path);
1122 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1129 public void CreationTime ()
1132 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1134 string path = Path.GetTempFileName ();
1136 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1137 DateTime time = File.GetCreationTime (path);
1138 Assert.AreEqual (2002, time.Year, "#A1");
1139 Assert.AreEqual (4, time.Month, "#A2");
1140 Assert.AreEqual (6, time.Day, "#A3");
1141 Assert.AreEqual (4, time.Hour, "#A4");
1142 Assert.AreEqual (4, time.Second, "#A5");
1144 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1145 Assert.AreEqual (2002, time.Year, "#B1");
1146 Assert.AreEqual (4, time.Month, "#B2");
1147 Assert.AreEqual (6, time.Day, "#B3");
1148 Assert.AreEqual (4, time.Hour, "#B4");
1149 Assert.AreEqual (4, time.Second, "#B5");
1151 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1152 time = File.GetCreationTimeUtc (path);
1153 Assert.AreEqual (2002, time.Year, "#C1");
1154 Assert.AreEqual (4, time.Month, "#C2");
1155 Assert.AreEqual (6, time.Day, "#C3");
1156 Assert.AreEqual (4, time.Hour, "#C4");
1157 Assert.AreEqual (4, time.Second, "#C5");
1159 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1160 Assert.AreEqual (2002, time.Year, "#D1");
1161 Assert.AreEqual (4, time.Month, "#D2");
1162 Assert.AreEqual (6, time.Day, "#D3");
1163 Assert.AreEqual (4, time.Hour, "#D4");
1164 Assert.AreEqual (4, time.Second, "#D5");
1171 public void LastAccessTime ()
1173 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1174 if (File.Exists (path))
1176 FileStream stream = null;
1178 stream = File.Create (path);
1181 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1182 DateTime time = File.GetLastAccessTime (path);
1183 Assert.AreEqual (2002, time.Year, "#A1");
1184 Assert.AreEqual (4, time.Month, "#A2");
1185 Assert.AreEqual (6, time.Day, "#A3");
1186 Assert.AreEqual (4, time.Hour, "#A4");
1187 Assert.AreEqual (4, time.Second, "#A5");
1189 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1190 Assert.AreEqual (2002, time.Year, "#B1");
1191 Assert.AreEqual (4, time.Month, "#B2");
1192 Assert.AreEqual (6, time.Day, "#B3");
1193 Assert.AreEqual (4, time.Hour, "#B4");
1194 Assert.AreEqual (4, time.Second, "#B5");
1196 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1197 time = File.GetLastAccessTimeUtc (path);
1198 Assert.AreEqual (2002, time.Year, "#C1");
1199 Assert.AreEqual (4, time.Month, "#C2");
1200 Assert.AreEqual (6, time.Day, "#C3");
1201 Assert.AreEqual (4, time.Hour, "#C4");
1202 Assert.AreEqual (4, time.Second, "#C5");
1204 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1205 Assert.AreEqual (2002, time.Year, "#D1");
1206 Assert.AreEqual (4, time.Month, "#D2");
1207 Assert.AreEqual (6, time.Day, "#D3");
1208 Assert.AreEqual (4, time.Hour, "#D4");
1209 Assert.AreEqual (4, time.Second, "#D5");
1218 public void LastWriteTime ()
1220 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1221 if (File.Exists (path))
1223 FileStream stream = null;
1225 stream = File.Create (path);
1228 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1229 DateTime time = File.GetLastWriteTime (path);
1230 Assert.AreEqual (2002, time.Year, "#A1");
1231 Assert.AreEqual (4, time.Month, "#A2");
1232 Assert.AreEqual (6, time.Day, "#A3");
1233 Assert.AreEqual (4, time.Hour, "#A4");
1234 Assert.AreEqual (4, time.Second, "#A5");
1236 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1237 Assert.AreEqual (2002, time.Year, "#B1");
1238 Assert.AreEqual (4, time.Month, "#B2");
1239 Assert.AreEqual (6, time.Day, "#B3");
1240 Assert.AreEqual (4, time.Hour, "#B4");
1241 Assert.AreEqual (4, time.Second, "#B5");
1243 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1244 time = File.GetLastWriteTimeUtc (path);
1245 Assert.AreEqual (2002, time.Year, "#C1");
1246 Assert.AreEqual (4, time.Month, "#C2");
1247 Assert.AreEqual (6, time.Day, "#C3");
1248 Assert.AreEqual (4, time.Hour, "#C4");
1249 Assert.AreEqual (4, time.Second, "#C5");
1251 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1252 Assert.AreEqual (2002, time.Year, "#D1");
1253 Assert.AreEqual (4, time.Month, "#D2");
1254 Assert.AreEqual (6, time.Day, "#D3");
1255 Assert.AreEqual (4, time.Hour, "#D4");
1256 Assert.AreEqual (4, time.Second, "#D5");
1265 public void GetCreationTime_Path_Null ()
1268 File.GetCreationTime (null as string);
1270 } catch (ArgumentNullException ex) {
1271 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1272 Assert.IsNull (ex.InnerException, "#3");
1273 Assert.IsNotNull (ex.Message, "#4");
1274 Assert.AreEqual ("path", ex.ParamName, "#5");
1279 public void GetCreationTime_Path_Empty ()
1282 File.GetCreationTime (string.Empty);
1284 } catch (ArgumentException ex) {
1285 // Empty file name is not legal
1286 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1287 Assert.IsNull (ex.InnerException, "#3");
1288 Assert.IsNotNull (ex.Message, "#4");
1289 Assert.IsNull (ex.ParamName, "#5");
1294 public void GetCreationTime_Path_DoesNotExist ()
1296 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1299 DateTime time = File.GetCreationTime (path);
1300 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1301 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1302 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1303 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1304 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1305 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1306 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1310 public void GetCreationTime_Path_Whitespace ()
1313 File.GetCreationTime (" ");
1315 } catch (ArgumentException ex) {
1316 // The path is not of a legal form
1317 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1318 Assert.IsNull (ex.InnerException, "#3");
1319 Assert.IsNotNull (ex.Message, "#4");
1320 Assert.IsNull (ex.ParamName, "#5");
1325 public void GetCreationTime_Path_InvalidPathChars ()
1328 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1330 } catch (ArgumentException ex) {
1331 // Illegal characters in path
1332 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1333 Assert.IsNull (ex.InnerException, "#3");
1334 Assert.IsNotNull (ex.Message, "#4");
1335 Assert.IsNull (ex.ParamName, "#5");
1340 public void GetCreationTimeUtc_Path_Null ()
1343 File.GetCreationTimeUtc (null as string);
1345 } catch (ArgumentNullException ex) {
1346 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1347 Assert.IsNull (ex.InnerException, "#3");
1348 Assert.IsNotNull (ex.Message, "#4");
1349 Assert.AreEqual ("path", ex.ParamName, "#5");
1354 public void GetCreationTimeUtc_Path_Empty ()
1357 File.GetCreationTimeUtc (string.Empty);
1359 } catch (ArgumentException ex) {
1360 // Empty file name is not legal
1361 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1362 Assert.IsNull (ex.InnerException, "#3");
1363 Assert.IsNotNull (ex.Message, "#4");
1364 Assert.IsNull (ex.ParamName, "#5");
1369 public void GetCreationTimeUtc_Path_DoesNotExist ()
1371 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1374 DateTime time = File.GetCreationTimeUtc (path);
1375 Assert.AreEqual (1601, time.Year, "#1");
1376 Assert.AreEqual (1, time.Month, "#2");
1377 Assert.AreEqual (1, time.Day, "#3");
1378 Assert.AreEqual (0, time.Hour, "#4");
1379 Assert.AreEqual (0, time.Second, "#5");
1380 Assert.AreEqual (0, time.Millisecond, "#6");
1384 public void GetCreationTimeUtc_Path_Whitespace ()
1387 File.GetCreationTimeUtc (" ");
1389 } catch (ArgumentException ex) {
1390 // The path is not of a legal form
1391 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1392 Assert.IsNull (ex.InnerException, "#3");
1393 Assert.IsNotNull (ex.Message, "#4");
1394 Assert.IsNull (ex.ParamName, "#5");
1399 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1402 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1404 } catch (ArgumentException ex) {
1405 // Illegal characters in path
1406 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1407 Assert.IsNull (ex.InnerException, "#3");
1408 Assert.IsNotNull (ex.Message, "#4");
1409 Assert.IsNull (ex.ParamName, "#5");
1414 public void GetLastAccessTime_Path_Null ()
1417 File.GetLastAccessTime (null as string);
1419 } catch (ArgumentNullException ex) {
1420 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1421 Assert.IsNull (ex.InnerException, "#3");
1422 Assert.IsNotNull (ex.Message, "#4");
1423 Assert.AreEqual ("path", ex.ParamName, "#5");
1428 public void GetLastAccessTime_Path_Empty ()
1431 File.GetLastAccessTime (string.Empty);
1433 } catch (ArgumentException ex) {
1434 // Empty file name is not legal
1435 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1436 Assert.IsNull (ex.InnerException, "#3");
1437 Assert.IsNotNull (ex.Message, "#4");
1438 Assert.IsNull (ex.ParamName, "#5");
1443 public void GetLastAccessTime_Path_DoesNotExist ()
1445 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1448 DateTime time = File.GetLastAccessTime (path);
1449 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1450 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1451 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1452 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1453 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1454 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1455 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1459 public void GetLastAccessTime_Path_Whitespace ()
1462 File.GetLastAccessTime (" ");
1464 } catch (ArgumentException ex) {
1465 // The path is not of a legal form
1466 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1467 Assert.IsNull (ex.InnerException, "#3");
1468 Assert.IsNotNull (ex.Message, "#4");
1469 Assert.IsNull (ex.ParamName, "#5");
1474 public void GetLastAccessTime_Path_InvalidPathChars ()
1477 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1479 } catch (ArgumentException ex) {
1480 // Illegal characters in path
1481 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1482 Assert.IsNull (ex.InnerException, "#3");
1483 Assert.IsNotNull (ex.Message, "#4");
1484 Assert.IsNull (ex.ParamName, "#5");
1489 public void GetLastAccessTimeUtc_Path_Null ()
1492 File.GetLastAccessTimeUtc (null as string);
1494 } catch (ArgumentNullException ex) {
1495 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1496 Assert.IsNull (ex.InnerException, "#3");
1497 Assert.IsNotNull (ex.Message, "#4");
1498 Assert.AreEqual ("path", ex.ParamName, "#5");
1503 public void GetLastAccessTimeUtc_Path_Empty ()
1506 File.GetLastAccessTimeUtc (string.Empty);
1508 } catch (ArgumentException ex) {
1509 // Empty file name is not legal
1510 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1511 Assert.IsNull (ex.InnerException, "#3");
1512 Assert.IsNotNull (ex.Message, "#4");
1513 Assert.IsNull (ex.ParamName, "#5");
1518 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1520 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1523 DateTime time = File.GetLastAccessTimeUtc (path);
1524 Assert.AreEqual (1601, time.Year, "#1");
1525 Assert.AreEqual (1, time.Month, "#2");
1526 Assert.AreEqual (1, time.Day, "#3");
1527 Assert.AreEqual (0, time.Hour, "#4");
1528 Assert.AreEqual (0, time.Second, "#5");
1529 Assert.AreEqual (0, time.Millisecond, "#6");
1533 public void GetLastAccessTimeUtc_Path_Whitespace ()
1536 File.GetLastAccessTimeUtc (" ");
1538 } catch (ArgumentException ex) {
1539 // The path is not of a legal form
1540 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1541 Assert.IsNull (ex.InnerException, "#3");
1542 Assert.IsNotNull (ex.Message, "#4");
1543 Assert.IsNull (ex.ParamName, "#5");
1548 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1551 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1553 } catch (ArgumentException ex) {
1554 // Illegal characters in path
1555 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1556 Assert.IsNull (ex.InnerException, "#3");
1557 Assert.IsNotNull (ex.Message, "#4");
1558 Assert.IsNull (ex.ParamName, "#5");
1563 public void GetLastWriteTime_Path_Null ()
1566 File.GetLastWriteTime (null as string);
1568 } catch (ArgumentNullException ex) {
1569 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1570 Assert.IsNull (ex.InnerException, "#3");
1571 Assert.IsNotNull (ex.Message, "#4");
1572 Assert.AreEqual ("path", ex.ParamName, "#5");
1577 public void GetLastWriteTime_Path_Empty ()
1580 File.GetLastWriteTime (string.Empty);
1582 } catch (ArgumentException ex) {
1583 // Empty file name is not legal
1584 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1585 Assert.IsNull (ex.InnerException, "#3");
1586 Assert.IsNotNull (ex.Message, "#4");
1587 Assert.IsNull (ex.ParamName, "#5");
1592 public void GetLastWriteTime_Path_DoesNotExist ()
1594 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1597 DateTime time = File.GetLastWriteTime (path);
1598 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1599 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1600 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1601 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1602 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1603 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1604 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1608 public void GetLastWriteTime_Path_Whitespace ()
1611 File.GetLastWriteTime (" ");
1613 } catch (ArgumentException ex) {
1614 // The path is not of a legal form
1615 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1616 Assert.IsNull (ex.InnerException, "#3");
1617 Assert.IsNotNull (ex.Message, "#4");
1618 Assert.IsNull (ex.ParamName, "#5");
1623 public void GetLastWriteTime_Path_InvalidPathChars ()
1626 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1628 } catch (ArgumentException ex) {
1629 // Illegal characters in path
1630 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1631 Assert.IsNull (ex.InnerException, "#3");
1632 Assert.IsNotNull (ex.Message, "#4");
1633 Assert.IsNull (ex.ParamName, "#5");
1638 public void GetLastWriteTimeUtc_Path_Null ()
1641 File.GetLastWriteTimeUtc (null as string);
1643 } catch (ArgumentNullException ex) {
1644 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1645 Assert.IsNull (ex.InnerException, "#3");
1646 Assert.IsNotNull (ex.Message, "#4");
1647 Assert.AreEqual ("path", ex.ParamName, "#5");
1652 public void GetLastWriteTimeUtc_Path_Empty ()
1655 File.GetLastWriteTimeUtc (string.Empty);
1657 } catch (ArgumentException ex) {
1658 // Empty file name is not legal
1659 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1660 Assert.IsNull (ex.InnerException, "#3");
1661 Assert.IsNotNull (ex.Message, "#4");
1662 Assert.IsNull (ex.ParamName, "#5");
1667 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1669 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1672 DateTime time = File.GetLastWriteTimeUtc (path);
1673 Assert.AreEqual (1601, time.Year, "#1");
1674 Assert.AreEqual (1, time.Month, "#2");
1675 Assert.AreEqual (1, time.Day, "#3");
1676 Assert.AreEqual (0, time.Hour, "#4");
1677 Assert.AreEqual (0, time.Second, "#5");
1678 Assert.AreEqual (0, time.Millisecond, "#6");
1682 public void GetLastWriteTimeUtc_Path_Whitespace ()
1685 File.GetLastWriteTimeUtc (" ");
1687 } catch (ArgumentException ex) {
1688 // The path is not of a legal form
1689 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1690 Assert.IsNull (ex.InnerException, "#3");
1691 Assert.IsNotNull (ex.Message, "#4");
1692 Assert.IsNull (ex.ParamName, "#5");
1697 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1700 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1702 } catch (ArgumentException ex) {
1703 // Illegal characters in path
1704 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1705 Assert.IsNull (ex.InnerException, "#3");
1706 Assert.IsNotNull (ex.Message, "#4");
1707 Assert.IsNull (ex.ParamName, "#5");
1712 public void FileStreamClose ()
1714 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1715 FileStream stream = null;
1717 stream = File.Create (path);
1727 // SetCreationTime and SetCreationTimeUtc exceptions
1730 public void SetCreationTime_Path_Null ()
1733 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1735 } catch (ArgumentNullException ex) {
1736 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1737 Assert.IsNull (ex.InnerException, "#3");
1738 Assert.IsNotNull (ex.Message, "#4");
1739 Assert.AreEqual ("path", ex.ParamName, "#5");
1744 public void SetCreationTime_Path_Empty ()
1747 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1749 } catch (ArgumentException ex) {
1750 // Empty file name is not legal
1751 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1752 Assert.IsNull (ex.InnerException, "#3");
1753 Assert.IsNotNull (ex.Message, "#4");
1754 Assert.IsNull (ex.ParamName, "#5");
1759 public void SetCreationTime_Path_Whitespace ()
1762 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1764 } catch (ArgumentException ex) {
1765 // The path is not of a legal form
1766 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1767 Assert.IsNull (ex.InnerException, "#3");
1768 Assert.IsNotNull (ex.Message, "#4");
1769 Assert.IsNull (ex.ParamName, "#5");
1774 public void SetCreationTime_Path_InvalidPathChars ()
1776 // On Unix there are no invalid path chars.
1777 if (Path.InvalidPathChars.Length > 1) {
1779 File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1780 new DateTime (2000, 12, 12, 11, 59, 59));
1782 } catch (ArgumentException ex) {
1783 // Illegal characters in path
1784 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1785 Assert.IsNull (ex.InnerException, "#3");
1786 Assert.IsNotNull (ex.Message, "#4");
1787 Assert.IsNull (ex.ParamName, "#5");
1793 public void SetCreationTime_Path_DoesNotExist ()
1795 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1799 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1801 } catch (FileNotFoundException ex) {
1802 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1803 Assert.AreEqual (path, ex.FileName, "#3");
1804 Assert.IsNull (ex.InnerException, "#4");
1805 Assert.IsNotNull (ex.Message, "#5");
1810 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1811 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1813 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1814 // FileStream stream = null;
1815 // DeleteFile (path);
1817 // stream = File.Create (path);
1819 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1821 // if (stream != null)
1823 // DeleteFile (path);
1828 public void SetCreationTime_FileLock ()
1830 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1832 FileStream stream = null;
1834 stream = File.Create (path);
1836 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1838 } catch (IOException ex) {
1839 // The process cannot access the file '...'
1840 // because it is being used by another process
1841 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1842 Assert.IsNull (ex.InnerException, "#3");
1843 Assert.IsNotNull (ex.Message, "#4");
1844 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1854 public void SetCreationTimeUtc_Path_Null ()
1857 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1859 } catch (ArgumentNullException ex) {
1860 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1861 Assert.IsNull (ex.InnerException, "#3");
1862 Assert.IsNotNull (ex.Message, "#4");
1863 Assert.AreEqual ("path", ex.ParamName, "#5");
1868 public void SetCreationTimeUtc_Path_Empty ()
1871 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1873 } catch (ArgumentException ex) {
1874 // Empty file name is not legal
1875 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1876 Assert.IsNull (ex.InnerException, "#3");
1877 Assert.IsNotNull (ex.Message, "#4");
1878 Assert.IsNull (ex.ParamName, "#5");
1883 public void SetCreationTimeUtc_Path_Whitespace ()
1886 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1888 } catch (ArgumentException ex) {
1889 // The path is not of a legal form
1890 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1891 Assert.IsNull (ex.InnerException, "#3");
1892 Assert.IsNotNull (ex.Message, "#4");
1893 Assert.IsNull (ex.ParamName, "#5");
1898 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1900 // On Unix there are no invalid path chars.
1901 if (Path.InvalidPathChars.Length > 1) {
1903 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1904 new DateTime (2000, 12, 12, 11, 59, 59));
1906 } catch (ArgumentException ex) {
1907 // Illegal characters in path
1908 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1909 Assert.IsNull (ex.InnerException, "#3");
1910 Assert.IsNotNull (ex.Message, "#4");
1911 Assert.IsNull (ex.ParamName, "#5");
1917 public void SetCreationTimeUtc_Path_DoesNotExist ()
1919 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1923 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1925 } catch (FileNotFoundException ex) {
1926 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1927 Assert.AreEqual (path, ex.FileName, "#3");
1928 Assert.IsNull (ex.InnerException, "#3");
1929 Assert.IsNotNull (ex.Message, "#4");
1934 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1935 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1937 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1938 // DeleteFile (path);
1939 // FileStream stream = null;
1941 // stream = File.Create (path);
1943 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1945 // if (stream != null)
1947 // DeleteFile (path);
1952 public void SetCreationTimeUtc_FileLock ()
1954 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1956 FileStream stream = null;
1958 stream = File.Create (path);
1960 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1962 } catch (IOException ex) {
1963 // The process cannot access the file "..."
1964 // because it is being used by another process
1965 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1966 Assert.IsNull (ex.InnerException, "#3");
1967 Assert.IsNotNull (ex.Message, "#4");
1968 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1977 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1980 public void SetLastAccessTime_Path_Null ()
1983 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1985 } catch (ArgumentNullException ex) {
1986 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1987 Assert.IsNull (ex.InnerException, "#3");
1988 Assert.IsNotNull (ex.Message, "#4");
1989 Assert.AreEqual ("path", ex.ParamName, "#5");
1994 public void SetLastAccessTime_Path_Empty ()
1997 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1999 } catch (ArgumentException ex) {
2000 // Empty file name is not legal
2001 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2002 Assert.IsNull (ex.InnerException, "#3");
2003 Assert.IsNotNull (ex.Message, "#4");
2004 Assert.IsNull (ex.ParamName, "#5");
2009 public void SetLastAccessTime_Path_Whitespace ()
2012 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2014 } catch (ArgumentException ex) {
2015 // The path is not of a legal form
2016 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2017 Assert.IsNull (ex.InnerException, "#3");
2018 Assert.IsNotNull (ex.Message, "#4");
2019 Assert.IsNull (ex.ParamName, "#5");
2024 public void SetLastAccessTime_Path_InvalidPathChars ()
2026 // On Unix there are no invalid path chars.
2027 if (Path.InvalidPathChars.Length > 1) {
2029 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2030 new DateTime (2000, 12, 12, 11, 59, 59));
2032 } catch (ArgumentException ex) {
2033 // Illegal characters in path
2034 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2035 Assert.IsNull (ex.InnerException, "#3");
2036 Assert.IsNotNull (ex.Message, "#4");
2037 Assert.IsNull (ex.ParamName, "#5");
2043 public void SetLastAccessTime_Path_DoesNotExist ()
2045 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2049 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2051 } catch (FileNotFoundException ex) {
2052 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2053 Assert.AreEqual (path, ex.FileName, "#3");
2054 Assert.IsNull (ex.InnerException, "#4");
2055 Assert.IsNotNull (ex.Message, "#5");
2060 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2061 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2063 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2064 // DeleteFile (path);
2065 // FileStream stream = null;
2067 // stream = File.Create (path);
2069 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2071 // if (stream != null)
2073 // DeleteFile (path);
2078 public void SetLastAccessTime_FileLock ()
2080 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2082 FileStream stream = null;
2084 stream = File.Create (path);
2086 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2088 } catch (IOException ex) {
2089 // The process cannot access the file "..."
2090 // because it is being used by another process
2091 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2092 Assert.IsNull (ex.InnerException, "#3");
2093 Assert.IsNotNull (ex.Message, "#4");
2094 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2104 public void SetLastAccessTimeUtc_Path_Null ()
2107 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2109 } catch (ArgumentNullException ex) {
2110 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2111 Assert.IsNull (ex.InnerException, "#3");
2112 Assert.IsNotNull (ex.Message, "#4");
2113 Assert.AreEqual ("path", ex.ParamName, "#5");
2118 public void SetCLastAccessTimeUtc_Path_Empty ()
2121 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2123 } catch (ArgumentException ex) {
2124 // Empty file name is not legal
2125 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2126 Assert.IsNull (ex.InnerException, "#3");
2127 Assert.IsNotNull (ex.Message, "#4");
2128 Assert.IsNull (ex.ParamName, "#5");
2133 public void SetLastAccessTimeUtc_Path_Whitespace ()
2136 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2138 } catch (ArgumentException ex) {
2139 // The path is not of a legal form
2140 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2141 Assert.IsNull (ex.InnerException, "#3");
2142 Assert.IsNotNull (ex.Message, "#4");
2143 Assert.IsNull (ex.ParamName, "#5");
2148 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2150 // On Unix there are no invalid path chars.
2151 if (Path.InvalidPathChars.Length > 1) {
2153 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2154 new DateTime (2000, 12, 12, 11, 59, 59));
2156 } catch (ArgumentException ex) {
2157 // Illegal characters in path
2158 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2159 Assert.IsNull (ex.InnerException, "#3");
2160 Assert.IsNotNull (ex.Message, "#4");
2161 Assert.IsNull (ex.ParamName, "#5");
2167 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2169 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2173 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2175 } catch (FileNotFoundException ex) {
2176 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2177 Assert.AreEqual (path, ex.FileName, "#3");
2178 Assert.IsNull (ex.InnerException, "#4");
2179 Assert.IsNotNull (ex.Message, "#5");
2184 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2185 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2187 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2188 // DeleteFile (path);
2189 // FileStream stream = null;
2191 // stream = File.Create (path);
2193 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2195 // if (stream != null)
2197 // DeleteFile (path);
2202 public void SetLastAccessTimeUtc_FileLock ()
2204 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2206 FileStream stream = null;
2208 stream = File.Create (path);
2210 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2212 } catch (IOException ex) {
2213 // The process cannot access the file "..."
2214 // because it is being used by another process
2215 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2216 Assert.IsNull (ex.InnerException, "#3");
2217 Assert.IsNotNull (ex.Message, "#4");
2218 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2227 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2230 public void SetLastWriteTime_Path_Null ()
2233 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2235 } catch (ArgumentNullException ex) {
2236 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2237 Assert.IsNull (ex.InnerException, "#3");
2238 Assert.IsNotNull (ex.Message, "#4");
2239 Assert.AreEqual ("path", ex.ParamName, "#5");
2244 public void SetLastWriteTime_Path_Empty ()
2247 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2249 } catch (ArgumentException ex) {
2250 // Empty file name is not legal
2251 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2252 Assert.IsNull (ex.InnerException, "#3");
2253 Assert.IsNotNull (ex.Message, "#4");
2254 Assert.IsNull (ex.ParamName, "#5");
2259 public void SetLastWriteTime_Path_Whitespace ()
2262 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2264 } catch (ArgumentException ex) {
2265 // The path is not of a legal form
2266 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2267 Assert.IsNull (ex.InnerException, "#3");
2268 Assert.IsNotNull (ex.Message, "#4");
2269 Assert.IsNull (ex.ParamName, "#5");
2274 public void SetLastWriteTime_Path_InvalidPathChars ()
2276 // On Unix there are no invalid path chars.
2277 if (Path.InvalidPathChars.Length > 1) {
2279 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2280 new DateTime (2000, 12, 12, 11, 59, 59));
2282 } catch (ArgumentException ex) {
2283 // Illegal characters in path
2284 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2285 Assert.IsNull (ex.InnerException, "#3");
2286 Assert.IsNotNull (ex.Message, "#4");
2287 Assert.IsNull (ex.ParamName, "#5");
2293 public void SetLastWriteTime_Path_DoesNotExist ()
2295 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2299 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2301 } catch (FileNotFoundException ex) {
2302 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2303 Assert.AreEqual (path, ex.FileName, "#3");
2304 Assert.IsNull (ex.InnerException, "#4");
2305 Assert.IsNotNull (ex.Message, "#5");
2310 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2311 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2313 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2314 // DeleteFile (path);
2315 // FileStream stream = null;
2317 // stream = File.Create (path);
2319 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2321 // if (stream != null)
2323 // DeleteFile (path);
2328 public void SetLastWriteTime_FileLock ()
2330 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2332 FileStream stream = null;
2334 stream = File.Create (path);
2336 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2338 } catch (IOException ex) {
2339 // The process cannot access the file '...'
2340 // because it is being used by another process
2341 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2342 Assert.IsNull (ex.InnerException, "#3");
2343 Assert.IsNotNull (ex.Message, "#4");
2344 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2354 public void SetLastWriteTimeUtc_Path_Null ()
2357 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2359 } catch (ArgumentNullException ex) {
2360 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2361 Assert.IsNull (ex.InnerException, "#3");
2362 Assert.IsNotNull (ex.Message, "#4");
2363 Assert.AreEqual ("path", ex.ParamName, "#5");
2368 public void SetLastWriteTimeUtc_Path_Empty ()
2371 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2373 } catch (ArgumentException ex) {
2374 // Empty file name is not legal
2375 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2376 Assert.IsNull (ex.InnerException, "#3");
2377 Assert.IsNotNull (ex.Message, "#4");
2378 Assert.IsNull (ex.ParamName, "#5");
2383 public void SetLastWriteTimeUtc_Path_Whitespace ()
2386 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2388 } catch (ArgumentException ex) {
2389 // The path is not of a legal form
2390 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2391 Assert.IsNull (ex.InnerException, "#3");
2392 Assert.IsNotNull (ex.Message, "#4");
2393 Assert.IsNull (ex.ParamName, "#5");
2398 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2400 // On Unix there are no invalid path chars.
2401 if (Path.InvalidPathChars.Length > 1) {
2403 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2404 new DateTime (2000, 12, 12, 11, 59, 59));
2406 } catch (ArgumentException ex) {
2407 // Illegal characters in path
2408 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2409 Assert.IsNull (ex.InnerException, "#3");
2410 Assert.IsNotNull (ex.Message, "#4");
2411 Assert.IsNull (ex.ParamName, "#5");
2417 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2419 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2423 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2425 } catch (FileNotFoundException ex) {
2426 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2427 Assert.AreEqual (path, ex.FileName, "#3");
2428 Assert.IsNull (ex.InnerException, "#4");
2429 Assert.IsNotNull (ex.Message, "#5");
2434 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2435 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2437 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2438 // DeleteFile (path);
2439 // FileStream stream = null;
2441 // stream = File.Create (path);
2443 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2445 // if (stream != null)
2447 // DeleteFile (path);
2452 public void SetLastWriteTimeUtc_FileLock ()
2454 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2456 FileStream stream = null;
2458 stream = File.Create (path);
2460 File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2462 } catch (IOException ex) {
2463 // The process cannot access the file '...'
2464 // because it is being used by another process
2465 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2466 Assert.IsNull (ex.InnerException, "#3");
2467 Assert.IsNotNull (ex.Message, "#4");
2468 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2478 public void OpenAppend ()
2480 string fn = Path.GetTempFileName ();
2481 using (FileStream s = File.Open (fn, FileMode.Append)) {
2486 void Position (long value)
2488 using (FileStream fs = File.OpenRead (testfile)) {
2489 fs.Position = value;
2490 Assert.AreEqual (value, fs.Position, "Position");
2491 Assert.AreEqual (1, fs.Length, "Length");
2496 public void Position_Small ()
2498 Position (Int32.MaxValue);
2502 [Category ("LargeFileSupport")]
2503 public void Position_Large ()
2505 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2506 Position ((long) Int32.MaxValue + 1);
2509 void Seek (long value)
2511 using (FileStream fs = File.OpenRead (testfile)) {
2512 fs.Seek (value, SeekOrigin.Begin);
2513 Assert.AreEqual (value, fs.Position, "Position");
2514 Assert.AreEqual (1, fs.Length, "Length");
2519 public void Seek_Small ()
2521 Seek (Int32.MaxValue);
2525 [Category ("LargeFileSupport")]
2526 public void Seek_Large ()
2528 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2529 Seek ((long) Int32.MaxValue + 1);
2532 void LockUnlock (long value)
2534 using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2535 fs.Lock (value - 1, 1);
2536 fs.Unlock (value - 1, 1);
2539 fs.Unlock (0, value);
2544 public void Lock_Small ()
2546 LockUnlock ((long) Int32.MaxValue);
2550 [Category ("LargeFileSupport")]
2551 public void Lock_Large ()
2553 // note: already worked without HAVE_LARGE_FILE_SUPPORT
2554 LockUnlock ((long) Int32.MaxValue + 1);
2558 public void ReadWriteAllText ()
2560 // The MSDN docs said something about
2561 // not including a final new line. it looks
2562 // like that was not true. I'm not sure what
2563 // that was talking about
2564 read_all (string.Empty);
2573 read_all ("a\r\na");
2577 read_all ("\r\n\r\n");
2581 public void ReplaceTest ()
2583 string tmp = Path.Combine (TempFolder, "ReplaceTest");
2584 Directory.CreateDirectory (tmp);
2585 string origFile = Path.Combine (tmp, "origFile");
2586 string replaceFile = Path.Combine (tmp, "replaceFile");
2587 string backupFile = Path.Combine (tmp, "backupFile");
2589 using (StreamWriter sw = File.CreateText (origFile)) {
2590 sw.WriteLine ("origFile");
2592 using (StreamWriter sw = File.CreateText (replaceFile)) {
2593 sw.WriteLine ("replaceFile");
2595 using (StreamWriter sw = File.CreateText (backupFile)) {
2596 sw.WriteLine ("backupFile");
2599 File.Replace (origFile, replaceFile, backupFile);
2600 Assert.IsFalse (File.Exists (origFile), "#1");
2601 using (StreamReader sr = File.OpenText (replaceFile)) {
2602 string txt = sr.ReadLine ();
2603 Assert.AreEqual ("origFile", txt, "#2");
2605 using (StreamReader sr = File.OpenText (backupFile)) {
2606 string txt = sr.ReadLine ();
2607 Assert.AreEqual ("replaceFile", txt, "#3");
2611 static bool RunningOnUnix {
2613 int p = (int) Environment.OSVersion.Platform;
2614 return ((p == 4) || (p == 128) || (p == 6));
2618 void DeleteFile (string path)
2620 if (File.Exists (path))
2624 void DeleteDirectory (string path)
2626 if (Directory.Exists (path))
2627 Directory.Delete (path, true);
2630 void read_all (string s)
2632 string f = Path.GetTempFileName ();
2634 File.WriteAllText (f, s);
2635 string r = File.ReadAllText (f);
2636 Assert.AreEqual (s, r);
2642 void MoveTest (FileAccess acc, FileShare share, bool works)
2644 // use TEMP so since the default location (right along with the assemblies)
2645 // will get access denied when running under some environment (e.g. iOS devices)
2646 var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
2648 File.Delete (file + ".old");
2649 using (var v = File.Create (file)) { }
2651 using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2653 File.Move(file, file + ".old");
2655 Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
2656 } catch (IOException) {
2658 Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
2664 public void MoveTest ()
2666 MoveTest (FileAccess.Read, FileShare.None, false);
2667 MoveTest (FileAccess.Read, FileShare.Read, false);
2668 MoveTest (FileAccess.Read, FileShare.Write, false);
2669 MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2670 MoveTest (FileAccess.Read, FileShare.Delete, true);
2671 MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2672 MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2673 MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2674 MoveTest (FileAccess.Write, FileShare.None, false);
2675 MoveTest (FileAccess.Write, FileShare.Read, false);
2676 MoveTest (FileAccess.Write, FileShare.Write, false);
2677 MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2678 MoveTest (FileAccess.Write, FileShare.Delete, true);
2679 MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2680 MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2681 MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2682 MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2683 MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2684 MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2685 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2686 MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2687 MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2688 MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2689 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
2693 [DllImport ("libc", SetLastError=true)]
2694 public static extern int symlink (string oldpath, string newpath);
2697 public void SymLinkLoop ()
2700 Assert.Ignore ("Symlink are hard on windows");
2702 var name1 = Path.GetRandomFileName ();
2703 var name2 = Path.GetRandomFileName ();
2705 var path1 = Path.Combine (Path.GetTempPath (), name1);
2706 var path2 = Path.Combine (Path.GetTempPath (), name2);
2708 File.Delete (path1);
2709 File.Delete (path2);
2712 symlink (path1, path2);
2713 symlink (path2, path1);
2715 Assert.IsTrue (File.Exists (path1), "File.Exists must return true for path1 symlink loop");
2716 Assert.IsTrue (File.Exists (path2), "File.Exists must return true for path2 symlink loop");
2719 using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
2720 Assert.Fail ("File.Open must throw for symlink loops");
2722 } catch (IOException ex) {
2723 Assert.AreEqual (0x80070781u, (uint)ex.HResult, "Ensure HRESULT is correct");
2726 File.Delete (path1); //Delete must not throw and must work
2727 Assert.IsFalse (File.Exists (path1), "File.Delete must delete symlink loops");
2731 File.Delete (path1);
2732 File.Delete (path2);
2733 } catch (IOException) {
2734 //Don't double fault any exception from the tests.