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 GetAttributes_Archive ()
539 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
541 FileAttributes attrs;
543 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
544 File.Create (path).Close ();
546 attrs = File.GetAttributes (path);
547 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
549 attrs &= ~FileAttributes.Archive;
550 File.SetAttributes (path, attrs);
552 attrs = File.GetAttributes (path);
553 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
557 public void GetAttributes_Default_File ()
560 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
562 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
563 File.Create (path).Close ();
565 FileAttributes attrs = File.GetAttributes (path);
567 Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
568 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
569 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
570 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
571 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
572 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
576 public void GetAttributes_Default_Directory ()
578 FileAttributes attrs = File.GetAttributes (TempFolder);
580 Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
581 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
582 Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
583 Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
584 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
585 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
589 public void GetAttributes_Directory ()
591 FileAttributes attrs = File.GetAttributes (TempFolder);
593 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
595 attrs &= ~FileAttributes.Directory;
596 File.SetAttributes (TempFolder, attrs);
598 Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
600 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
601 File.Create (path).Close ();
603 attrs = File.GetAttributes (path);
604 attrs |= FileAttributes.Directory;
605 File.SetAttributes (path, attrs);
607 Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
611 public void GetAttributes_ReadOnly ()
613 FileAttributes attrs;
615 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
616 File.Create (path).Close ();
618 attrs = File.GetAttributes (path);
619 Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
622 attrs |= FileAttributes.ReadOnly;
623 File.SetAttributes (path, attrs);
625 attrs = File.GetAttributes (path);
626 Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
628 File.SetAttributes (path, FileAttributes.Normal);
633 public void GetAttributes_System ()
636 Assert.Ignore ("FileAttributes.System is not supported on Unix.");
638 FileAttributes attrs;
640 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
641 File.Create (path).Close ();
643 attrs = File.GetAttributes (path);
644 Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
646 attrs |= FileAttributes.System;
647 File.SetAttributes (path, FileAttributes.System);
649 attrs = File.GetAttributes (path);
650 Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
654 public void GetAttributes_Path_DoesNotExist ()
656 string path = Path.Combine (TempFolder, "GetAttributes.tmp");
658 File.GetAttributes (path);
660 } catch (FileNotFoundException ex) {
661 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
662 Assert.AreEqual (path, ex.FileName, "#3");
663 Assert.IsNull (ex.InnerException, "#4");
664 Assert.IsNotNull (ex.Message, "#5");
669 public void GetAttributes_Path_Empty ()
672 File.GetAttributes (string.Empty);
674 } catch (ArgumentException ex) {
675 // Empty file name is not legal
676 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
677 Assert.IsNull (ex.InnerException, "#3");
678 Assert.IsNotNull (ex.Message, "#4");
679 Assert.IsNull (ex.ParamName, "#5");
684 public void GetAttributes_Path_Null ()
687 File.GetAttributes (null);
689 } catch (ArgumentNullException ex) {
690 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
691 Assert.IsNull (ex.InnerException, "#3");
692 Assert.IsNotNull (ex.Message, "#4");
693 Assert.AreEqual ("path", ex.ParamName, "#5");
698 public void Move_SourceFileName_Null ()
701 File.Move (null, "b");
703 } catch (ArgumentNullException ex) {
704 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
705 Assert.IsNull (ex.InnerException, "#3");
706 Assert.IsNotNull (ex.Message, "#4");
707 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
712 public void Move_DestFileName_Null ()
715 File.Move ("a", null);
717 } catch (ArgumentNullException ex) {
718 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
719 Assert.IsNull (ex.InnerException, "#3");
720 Assert.IsNotNull (ex.Message, "#4");
721 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
726 public void Move_SourceFileName_Empty ()
729 File.Move (string.Empty, "b");
731 } catch (ArgumentException ex) {
732 // Empty file name is not legal
733 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
734 Assert.IsNull (ex.InnerException, "#3");
735 Assert.IsNotNull (ex.Message, "#4");
736 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
741 public void Move_DestFileName_Empty ()
744 File.Move ("a", string.Empty);
746 } catch (ArgumentException ex) {
747 // Empty file name is not legal
748 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
749 Assert.IsNull (ex.InnerException, "#3");
750 Assert.IsNotNull (ex.Message, "#4");
751 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
756 public void Move_SourceFileName_Whitespace ()
759 File.Move (" ", "b");
761 } catch (ArgumentException ex) {
762 // The path is not of a legal form
763 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
764 Assert.IsNull (ex.InnerException, "#3");
765 Assert.IsNotNull (ex.Message, "#4");
766 Assert.IsNull (ex.ParamName, "#5");
771 public void Move_DestFileName_Whitespace ()
774 File.Move ("a", " ");
776 } catch (ArgumentException ex) {
777 // The path is not of a legal form
778 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
779 Assert.IsNull (ex.InnerException, "#3");
780 Assert.IsNotNull (ex.Message, "#4");
781 Assert.IsNull (ex.ParamName, "#5");
786 public void Move_SourceFileName_DoesNotExist ()
788 string file = TempFolder + Path.DirectorySeparatorChar + "doesnotexist";
791 File.Move (file, "b");
793 } catch (FileNotFoundException ex) {
794 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
795 Assert.AreEqual (file, ex.FileName, "#3");
796 Assert.IsNull (ex.InnerException, "#4");
797 Assert.IsNotNull (ex.Message, "#5");
802 public void Move_DestFileName_DirectoryDoesNotExist ()
804 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
805 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
806 DeleteFile (sourceFile);
808 File.Create (sourceFile).Close ();
810 File.Move (sourceFile, destFile);
812 } catch (DirectoryNotFoundException ex) {
813 // Could not find a part of the path
814 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
815 Assert.IsNull (ex.InnerException, "#3");
816 Assert.IsNotNull (ex.Message, "#4");
817 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
820 DeleteFile (sourceFile);
825 public void Move_DestFileName_AlreadyExists ()
827 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
830 // move to same directory
831 File.Create (sourceFile).Close ();
833 File.Move (sourceFile, TempFolder);
835 } catch (IOException ex) {
836 // Cannot create a file when that file already exists
837 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
838 Assert.IsNull (ex.InnerException, "#A3");
839 Assert.IsNotNull (ex.Message, "#A4");
840 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
841 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
843 DeleteFile (sourceFile);
846 // move to exist file
847 File.Create (sourceFile).Close ();
848 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
849 File.Create (destFile).Close ();
851 File.Move (sourceFile, destFile);
853 } catch (IOException ex) {
854 // Cannot create a file when that file already exists
855 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
856 Assert.IsNull (ex.InnerException, "#B3");
857 Assert.IsNotNull (ex.Message, "#B4");
858 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
859 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
861 DeleteFile (sourceFile);
862 DeleteFile (destFile);
865 // move to existing directory
866 File.Create (sourceFile).Close ();
867 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
868 Directory.CreateDirectory (destFile);
870 File.Move (sourceFile, destFile);
872 } catch (IOException ex) {
873 // Cannot create a file when that file already exists
874 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
875 Assert.IsNull (ex.InnerException, "#C3");
876 Assert.IsNotNull (ex.Message, "#C4");
877 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
878 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
880 DeleteFile (sourceFile);
881 DeleteDirectory (destFile);
888 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
889 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
890 if (!File.Exists (bar)) {
891 FileStream f = File.Create(bar);
895 Assert.IsTrue (File.Exists (bar), "#1");
896 File.Move (bar, baz);
897 Assert.IsFalse (File.Exists (bar), "#2");
898 Assert.IsTrue (File.Exists (baz), "#3");
900 // Test moving of directories
901 string dir = Path.Combine (TempFolder, "dir");
902 string dir2 = Path.Combine (TempFolder, "dir2");
903 string dir_foo = Path.Combine (dir, "foo");
904 string dir2_foo = Path.Combine (dir2, "foo");
906 if (Directory.Exists (dir))
907 Directory.Delete (dir, true);
909 Directory.CreateDirectory (dir);
910 Directory.CreateDirectory (dir2);
911 File.Create (dir_foo).Close ();
912 File.Move (dir_foo, dir2_foo);
913 Assert.IsTrue (File.Exists (dir2_foo), "#4");
915 Directory.Delete (dir, true);
916 Directory.Delete (dir2, true);
917 DeleteFile (dir_foo);
918 DeleteFile (dir2_foo);
922 public void Move_FileLock ()
924 string sourceFile = Path.GetTempFileName ();
925 string destFile = Path.GetTempFileName ();
927 // source file locked
928 using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
930 File.Move (sourceFile, destFile);
932 } catch (IOException ex) {
933 // The process cannot access the file because
934 // it is being used by another process
935 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
936 Assert.IsNull (ex.InnerException, "#A3");
937 Assert.IsNotNull (ex.Message, "#A4");
941 // destination file locked
942 using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
944 File.Move (sourceFile, destFile);
946 } catch (IOException ex) {
947 // The process cannot access the file because
948 // it is being used by another process
949 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
950 Assert.IsNull (ex.InnerException, "#B3");
951 Assert.IsNotNull (ex.Message, "#B4");
960 FileStream stream = null;
962 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
964 if (!File.Exists (path))
965 stream = File.Create (path);
967 stream = File.Open (path, FileMode.Open);
977 if (!File.Exists (path))
978 File.Create (path).Close ();
980 stream = File.Open (path, FileMode.Open);
981 Assert.IsTrue (stream.CanRead, "#A1");
982 Assert.IsTrue (stream.CanSeek, "#A2");
983 Assert.IsTrue (stream.CanWrite, "#A3");
986 stream = File.Open (path, FileMode.Open, FileAccess.Write);
987 Assert.IsFalse (stream.CanRead, "#B1");
988 Assert.IsTrue (stream.CanSeek, "#B2");
989 Assert.IsTrue (stream.CanWrite, "#B3");
992 stream = File.Open (path, FileMode.Open, FileAccess.Read);
993 Assert.IsTrue (stream.CanRead, "#C1");
994 Assert.IsTrue (stream.CanSeek, "#C2");
995 Assert.IsFalse (stream.CanWrite, "#C3");
1005 /* Exception tests */
1006 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
1008 stream = File.Open (path, FileMode.Open);
1009 Assert.Fail ("#D1");
1010 } catch (FileNotFoundException ex) {
1011 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
1012 Assert.AreEqual (path, ex.FileName, "#D3");
1013 Assert.IsNull (ex.InnerException, "#D4");
1014 Assert.IsNotNull (ex.Message, "#D5");
1023 public void Open_CreateNewMode_ReadAccess ()
1025 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1026 FileStream stream = null;
1028 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1030 } catch (ArgumentException ex) {
1031 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1032 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1033 Assert.IsNull (ex.InnerException, "#3");
1034 Assert.IsNotNull (ex.Message, "#4");
1035 Assert.IsNull (ex.ParamName, "#5");
1044 public void Open_AppendMode_ReadAccess ()
1046 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1047 FileStream s = null;
1048 if (!File.Exists (path))
1049 File.Create (path).Close ();
1051 s = File.Open (path, FileMode.Append, FileAccess.Read);
1053 } catch (ArgumentException ex) {
1054 // Combining FileMode: Append with FileAccess: Read is invalid
1055 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1056 Assert.IsNull (ex.InnerException, "#3");
1057 Assert.IsNotNull (ex.Message, "#4");
1058 Assert.IsNull (ex.ParamName, "#5");
1067 public void OpenRead ()
1069 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1070 if (!File.Exists (path))
1071 File.Create (path).Close ();
1072 FileStream stream = null;
1075 stream = File.OpenRead (path);
1076 Assert.IsTrue (stream.CanRead, "#1");
1077 Assert.IsTrue (stream.CanSeek, "#2");
1078 Assert.IsFalse (stream.CanWrite, "#3");
1087 public void OpenWrite ()
1089 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1090 if (!File.Exists (path))
1091 File.Create (path).Close ();
1092 FileStream stream = null;
1095 stream = File.OpenWrite (path);
1096 Assert.IsFalse (stream.CanRead, "#1");
1097 Assert.IsTrue (stream.CanSeek, "#2");
1098 Assert.IsTrue (stream.CanWrite, "#3");
1108 public void TestGetCreationTime ()
1110 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1114 File.Create (path).Close();
1115 DateTime time = File.GetCreationTime (path);
1116 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1123 public void CreationTime ()
1126 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1128 string path = Path.GetTempFileName ();
1130 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1131 DateTime time = File.GetCreationTime (path);
1132 Assert.AreEqual (2002, time.Year, "#A1");
1133 Assert.AreEqual (4, time.Month, "#A2");
1134 Assert.AreEqual (6, time.Day, "#A3");
1135 Assert.AreEqual (4, time.Hour, "#A4");
1136 Assert.AreEqual (4, time.Second, "#A5");
1138 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1139 Assert.AreEqual (2002, time.Year, "#B1");
1140 Assert.AreEqual (4, time.Month, "#B2");
1141 Assert.AreEqual (6, time.Day, "#B3");
1142 Assert.AreEqual (4, time.Hour, "#B4");
1143 Assert.AreEqual (4, time.Second, "#B5");
1145 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1146 time = File.GetCreationTimeUtc (path);
1147 Assert.AreEqual (2002, time.Year, "#C1");
1148 Assert.AreEqual (4, time.Month, "#C2");
1149 Assert.AreEqual (6, time.Day, "#C3");
1150 Assert.AreEqual (4, time.Hour, "#C4");
1151 Assert.AreEqual (4, time.Second, "#C5");
1153 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1154 Assert.AreEqual (2002, time.Year, "#D1");
1155 Assert.AreEqual (4, time.Month, "#D2");
1156 Assert.AreEqual (6, time.Day, "#D3");
1157 Assert.AreEqual (4, time.Hour, "#D4");
1158 Assert.AreEqual (4, time.Second, "#D5");
1165 public void LastAccessTime ()
1167 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1168 if (File.Exists (path))
1170 FileStream stream = null;
1172 stream = File.Create (path);
1175 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1176 DateTime time = File.GetLastAccessTime (path);
1177 Assert.AreEqual (2002, time.Year, "#A1");
1178 Assert.AreEqual (4, time.Month, "#A2");
1179 Assert.AreEqual (6, time.Day, "#A3");
1180 Assert.AreEqual (4, time.Hour, "#A4");
1181 Assert.AreEqual (4, time.Second, "#A5");
1183 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1184 Assert.AreEqual (2002, time.Year, "#B1");
1185 Assert.AreEqual (4, time.Month, "#B2");
1186 Assert.AreEqual (6, time.Day, "#B3");
1187 Assert.AreEqual (4, time.Hour, "#B4");
1188 Assert.AreEqual (4, time.Second, "#B5");
1190 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1191 time = File.GetLastAccessTimeUtc (path);
1192 Assert.AreEqual (2002, time.Year, "#C1");
1193 Assert.AreEqual (4, time.Month, "#C2");
1194 Assert.AreEqual (6, time.Day, "#C3");
1195 Assert.AreEqual (4, time.Hour, "#C4");
1196 Assert.AreEqual (4, time.Second, "#C5");
1198 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1199 Assert.AreEqual (2002, time.Year, "#D1");
1200 Assert.AreEqual (4, time.Month, "#D2");
1201 Assert.AreEqual (6, time.Day, "#D3");
1202 Assert.AreEqual (4, time.Hour, "#D4");
1203 Assert.AreEqual (4, time.Second, "#D5");
1212 public void LastWriteTime ()
1214 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1215 if (File.Exists (path))
1217 FileStream stream = null;
1219 stream = File.Create (path);
1222 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1223 DateTime time = File.GetLastWriteTime (path);
1224 Assert.AreEqual (2002, time.Year, "#A1");
1225 Assert.AreEqual (4, time.Month, "#A2");
1226 Assert.AreEqual (6, time.Day, "#A3");
1227 Assert.AreEqual (4, time.Hour, "#A4");
1228 Assert.AreEqual (4, time.Second, "#A5");
1230 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1231 Assert.AreEqual (2002, time.Year, "#B1");
1232 Assert.AreEqual (4, time.Month, "#B2");
1233 Assert.AreEqual (6, time.Day, "#B3");
1234 Assert.AreEqual (4, time.Hour, "#B4");
1235 Assert.AreEqual (4, time.Second, "#B5");
1237 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1238 time = File.GetLastWriteTimeUtc (path);
1239 Assert.AreEqual (2002, time.Year, "#C1");
1240 Assert.AreEqual (4, time.Month, "#C2");
1241 Assert.AreEqual (6, time.Day, "#C3");
1242 Assert.AreEqual (4, time.Hour, "#C4");
1243 Assert.AreEqual (4, time.Second, "#C5");
1245 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1246 Assert.AreEqual (2002, time.Year, "#D1");
1247 Assert.AreEqual (4, time.Month, "#D2");
1248 Assert.AreEqual (6, time.Day, "#D3");
1249 Assert.AreEqual (4, time.Hour, "#D4");
1250 Assert.AreEqual (4, time.Second, "#D5");
1259 public void GetCreationTime_Path_Null ()
1262 File.GetCreationTime (null as string);
1264 } catch (ArgumentNullException ex) {
1265 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1266 Assert.IsNull (ex.InnerException, "#3");
1267 Assert.IsNotNull (ex.Message, "#4");
1268 Assert.AreEqual ("path", ex.ParamName, "#5");
1273 public void GetCreationTime_Path_Empty ()
1276 File.GetCreationTime (string.Empty);
1278 } catch (ArgumentException ex) {
1279 // Empty file name is not legal
1280 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1281 Assert.IsNull (ex.InnerException, "#3");
1282 Assert.IsNotNull (ex.Message, "#4");
1283 Assert.IsNull (ex.ParamName, "#5");
1288 public void GetCreationTime_Path_DoesNotExist ()
1290 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1293 DateTime time = File.GetCreationTime (path);
1294 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1295 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1296 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1297 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1298 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1299 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1300 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1304 public void GetCreationTime_Path_Whitespace ()
1307 File.GetCreationTime (" ");
1309 } catch (ArgumentException ex) {
1310 // The path is not of a legal form
1311 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1312 Assert.IsNull (ex.InnerException, "#3");
1313 Assert.IsNotNull (ex.Message, "#4");
1314 Assert.IsNull (ex.ParamName, "#5");
1319 public void GetCreationTime_Path_InvalidPathChars ()
1322 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1324 } catch (ArgumentException ex) {
1325 // Illegal characters in path
1326 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1327 Assert.IsNull (ex.InnerException, "#3");
1328 Assert.IsNotNull (ex.Message, "#4");
1329 Assert.IsNull (ex.ParamName, "#5");
1334 public void GetCreationTimeUtc_Path_Null ()
1337 File.GetCreationTimeUtc (null as string);
1339 } catch (ArgumentNullException ex) {
1340 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1341 Assert.IsNull (ex.InnerException, "#3");
1342 Assert.IsNotNull (ex.Message, "#4");
1343 Assert.AreEqual ("path", ex.ParamName, "#5");
1348 public void GetCreationTimeUtc_Path_Empty ()
1351 File.GetCreationTimeUtc (string.Empty);
1353 } catch (ArgumentException ex) {
1354 // Empty file name is not legal
1355 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1356 Assert.IsNull (ex.InnerException, "#3");
1357 Assert.IsNotNull (ex.Message, "#4");
1358 Assert.IsNull (ex.ParamName, "#5");
1363 public void GetCreationTimeUtc_Path_DoesNotExist ()
1365 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1368 DateTime time = File.GetCreationTimeUtc (path);
1369 Assert.AreEqual (1601, time.Year, "#1");
1370 Assert.AreEqual (1, time.Month, "#2");
1371 Assert.AreEqual (1, time.Day, "#3");
1372 Assert.AreEqual (0, time.Hour, "#4");
1373 Assert.AreEqual (0, time.Second, "#5");
1374 Assert.AreEqual (0, time.Millisecond, "#6");
1378 public void GetCreationTimeUtc_Path_Whitespace ()
1381 File.GetCreationTimeUtc (" ");
1383 } catch (ArgumentException ex) {
1384 // The path is not of a legal form
1385 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1386 Assert.IsNull (ex.InnerException, "#3");
1387 Assert.IsNotNull (ex.Message, "#4");
1388 Assert.IsNull (ex.ParamName, "#5");
1393 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1396 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1398 } catch (ArgumentException ex) {
1399 // Illegal characters in path
1400 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1401 Assert.IsNull (ex.InnerException, "#3");
1402 Assert.IsNotNull (ex.Message, "#4");
1403 Assert.IsNull (ex.ParamName, "#5");
1408 public void GetLastAccessTime_Path_Null ()
1411 File.GetLastAccessTime (null as string);
1413 } catch (ArgumentNullException ex) {
1414 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1415 Assert.IsNull (ex.InnerException, "#3");
1416 Assert.IsNotNull (ex.Message, "#4");
1417 Assert.AreEqual ("path", ex.ParamName, "#5");
1422 public void GetLastAccessTime_Path_Empty ()
1425 File.GetLastAccessTime (string.Empty);
1427 } catch (ArgumentException ex) {
1428 // Empty file name is not legal
1429 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1430 Assert.IsNull (ex.InnerException, "#3");
1431 Assert.IsNotNull (ex.Message, "#4");
1432 Assert.IsNull (ex.ParamName, "#5");
1437 public void GetLastAccessTime_Path_DoesNotExist ()
1439 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1442 DateTime time = File.GetLastAccessTime (path);
1443 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1444 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1445 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1446 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1447 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1448 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1449 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1453 public void GetLastAccessTime_Path_Whitespace ()
1456 File.GetLastAccessTime (" ");
1458 } catch (ArgumentException ex) {
1459 // The path is not of a legal form
1460 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1461 Assert.IsNull (ex.InnerException, "#3");
1462 Assert.IsNotNull (ex.Message, "#4");
1463 Assert.IsNull (ex.ParamName, "#5");
1468 public void GetLastAccessTime_Path_InvalidPathChars ()
1471 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1473 } catch (ArgumentException ex) {
1474 // Illegal characters in path
1475 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1476 Assert.IsNull (ex.InnerException, "#3");
1477 Assert.IsNotNull (ex.Message, "#4");
1478 Assert.IsNull (ex.ParamName, "#5");
1483 public void GetLastAccessTimeUtc_Path_Null ()
1486 File.GetLastAccessTimeUtc (null as string);
1488 } catch (ArgumentNullException ex) {
1489 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1490 Assert.IsNull (ex.InnerException, "#3");
1491 Assert.IsNotNull (ex.Message, "#4");
1492 Assert.AreEqual ("path", ex.ParamName, "#5");
1497 public void GetLastAccessTimeUtc_Path_Empty ()
1500 File.GetLastAccessTimeUtc (string.Empty);
1502 } catch (ArgumentException ex) {
1503 // Empty file name is not legal
1504 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1505 Assert.IsNull (ex.InnerException, "#3");
1506 Assert.IsNotNull (ex.Message, "#4");
1507 Assert.IsNull (ex.ParamName, "#5");
1512 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1514 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1517 DateTime time = File.GetLastAccessTimeUtc (path);
1518 Assert.AreEqual (1601, time.Year, "#1");
1519 Assert.AreEqual (1, time.Month, "#2");
1520 Assert.AreEqual (1, time.Day, "#3");
1521 Assert.AreEqual (0, time.Hour, "#4");
1522 Assert.AreEqual (0, time.Second, "#5");
1523 Assert.AreEqual (0, time.Millisecond, "#6");
1527 public void GetLastAccessTimeUtc_Path_Whitespace ()
1530 File.GetLastAccessTimeUtc (" ");
1532 } catch (ArgumentException ex) {
1533 // The path is not of a legal form
1534 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1535 Assert.IsNull (ex.InnerException, "#3");
1536 Assert.IsNotNull (ex.Message, "#4");
1537 Assert.IsNull (ex.ParamName, "#5");
1542 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1545 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1547 } catch (ArgumentException ex) {
1548 // Illegal characters in path
1549 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1550 Assert.IsNull (ex.InnerException, "#3");
1551 Assert.IsNotNull (ex.Message, "#4");
1552 Assert.IsNull (ex.ParamName, "#5");
1557 public void GetLastWriteTime_Path_Null ()
1560 File.GetLastWriteTime (null as string);
1562 } catch (ArgumentNullException ex) {
1563 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1564 Assert.IsNull (ex.InnerException, "#3");
1565 Assert.IsNotNull (ex.Message, "#4");
1566 Assert.AreEqual ("path", ex.ParamName, "#5");
1571 public void GetLastWriteTime_Path_Empty ()
1574 File.GetLastWriteTime (string.Empty);
1576 } catch (ArgumentException ex) {
1577 // Empty file name is not legal
1578 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1579 Assert.IsNull (ex.InnerException, "#3");
1580 Assert.IsNotNull (ex.Message, "#4");
1581 Assert.IsNull (ex.ParamName, "#5");
1586 public void GetLastWriteTime_Path_DoesNotExist ()
1588 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1591 DateTime time = File.GetLastWriteTime (path);
1592 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1593 Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1594 Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1595 Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1596 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1597 Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1598 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1602 public void GetLastWriteTime_Path_Whitespace ()
1605 File.GetLastWriteTime (" ");
1607 } catch (ArgumentException ex) {
1608 // The path is not of a legal form
1609 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1610 Assert.IsNull (ex.InnerException, "#3");
1611 Assert.IsNotNull (ex.Message, "#4");
1612 Assert.IsNull (ex.ParamName, "#5");
1617 public void GetLastWriteTime_Path_InvalidPathChars ()
1620 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1622 } catch (ArgumentException ex) {
1623 // Illegal characters in path
1624 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1625 Assert.IsNull (ex.InnerException, "#3");
1626 Assert.IsNotNull (ex.Message, "#4");
1627 Assert.IsNull (ex.ParamName, "#5");
1632 public void GetLastWriteTimeUtc_Path_Null ()
1635 File.GetLastWriteTimeUtc (null as string);
1637 } catch (ArgumentNullException ex) {
1638 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1639 Assert.IsNull (ex.InnerException, "#3");
1640 Assert.IsNotNull (ex.Message, "#4");
1641 Assert.AreEqual ("path", ex.ParamName, "#5");
1646 public void GetLastWriteTimeUtc_Path_Empty ()
1649 File.GetLastWriteTimeUtc (string.Empty);
1651 } catch (ArgumentException ex) {
1652 // Empty file name is not legal
1653 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1654 Assert.IsNull (ex.InnerException, "#3");
1655 Assert.IsNotNull (ex.Message, "#4");
1656 Assert.IsNull (ex.ParamName, "#5");
1661 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1663 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1666 DateTime time = File.GetLastWriteTimeUtc (path);
1667 Assert.AreEqual (1601, time.Year, "#1");
1668 Assert.AreEqual (1, time.Month, "#2");
1669 Assert.AreEqual (1, time.Day, "#3");
1670 Assert.AreEqual (0, time.Hour, "#4");
1671 Assert.AreEqual (0, time.Second, "#5");
1672 Assert.AreEqual (0, time.Millisecond, "#6");
1676 public void GetLastWriteTimeUtc_Path_Whitespace ()
1679 File.GetLastWriteTimeUtc (" ");
1681 } catch (ArgumentException ex) {
1682 // The path is not of a legal form
1683 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1684 Assert.IsNull (ex.InnerException, "#3");
1685 Assert.IsNotNull (ex.Message, "#4");
1686 Assert.IsNull (ex.ParamName, "#5");
1691 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1694 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1696 } catch (ArgumentException ex) {
1697 // Illegal characters in path
1698 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1699 Assert.IsNull (ex.InnerException, "#3");
1700 Assert.IsNotNull (ex.Message, "#4");
1701 Assert.IsNull (ex.ParamName, "#5");
1706 public void FileStreamClose ()
1708 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1709 FileStream stream = null;
1711 stream = File.Create (path);
1721 // SetCreationTime and SetCreationTimeUtc exceptions
1724 public void SetCreationTime_Path_Null ()
1727 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1729 } catch (ArgumentNullException ex) {
1730 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1731 Assert.IsNull (ex.InnerException, "#3");
1732 Assert.IsNotNull (ex.Message, "#4");
1733 Assert.AreEqual ("path", ex.ParamName, "#5");
1738 public void SetCreationTime_Path_Empty ()
1741 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1743 } catch (ArgumentException ex) {
1744 // Empty file name is not legal
1745 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1746 Assert.IsNull (ex.InnerException, "#3");
1747 Assert.IsNotNull (ex.Message, "#4");
1748 Assert.IsNull (ex.ParamName, "#5");
1753 public void SetCreationTime_Path_Whitespace ()
1756 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1758 } catch (ArgumentException ex) {
1759 // The path is not of a legal form
1760 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1761 Assert.IsNull (ex.InnerException, "#3");
1762 Assert.IsNotNull (ex.Message, "#4");
1763 Assert.IsNull (ex.ParamName, "#5");
1768 public void SetCreationTime_Path_InvalidPathChars ()
1770 // On Unix there are no invalid path chars.
1771 if (Path.InvalidPathChars.Length > 1) {
1773 File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1774 new DateTime (2000, 12, 12, 11, 59, 59));
1776 } catch (ArgumentException ex) {
1777 // Illegal characters in path
1778 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1779 Assert.IsNull (ex.InnerException, "#3");
1780 Assert.IsNotNull (ex.Message, "#4");
1781 Assert.IsNull (ex.ParamName, "#5");
1787 public void SetCreationTime_Path_DoesNotExist ()
1789 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1793 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1795 } catch (FileNotFoundException ex) {
1796 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1797 Assert.AreEqual (path, ex.FileName, "#3");
1798 Assert.IsNull (ex.InnerException, "#4");
1799 Assert.IsNotNull (ex.Message, "#5");
1804 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1805 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1807 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1808 // FileStream stream = null;
1809 // DeleteFile (path);
1811 // stream = File.Create (path);
1813 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1815 // if (stream != null)
1817 // DeleteFile (path);
1822 public void SetCreationTime_FileLock ()
1824 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1826 FileStream stream = null;
1828 stream = File.Create (path);
1830 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1832 } catch (IOException ex) {
1833 // The process cannot access the file '...'
1834 // because it is being used by another process
1835 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1836 Assert.IsNull (ex.InnerException, "#3");
1837 Assert.IsNotNull (ex.Message, "#4");
1838 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1848 public void SetCreationTimeUtc_Path_Null ()
1851 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1853 } catch (ArgumentNullException ex) {
1854 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1855 Assert.IsNull (ex.InnerException, "#3");
1856 Assert.IsNotNull (ex.Message, "#4");
1857 Assert.AreEqual ("path", ex.ParamName, "#5");
1862 public void SetCreationTimeUtc_Path_Empty ()
1865 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1867 } catch (ArgumentException ex) {
1868 // Empty file name is not legal
1869 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1870 Assert.IsNull (ex.InnerException, "#3");
1871 Assert.IsNotNull (ex.Message, "#4");
1872 Assert.IsNull (ex.ParamName, "#5");
1877 public void SetCreationTimeUtc_Path_Whitespace ()
1880 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1882 } catch (ArgumentException ex) {
1883 // The path is not of a legal form
1884 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1885 Assert.IsNull (ex.InnerException, "#3");
1886 Assert.IsNotNull (ex.Message, "#4");
1887 Assert.IsNull (ex.ParamName, "#5");
1892 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1894 // On Unix there are no invalid path chars.
1895 if (Path.InvalidPathChars.Length > 1) {
1897 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1898 new DateTime (2000, 12, 12, 11, 59, 59));
1900 } catch (ArgumentException ex) {
1901 // Illegal characters in path
1902 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1903 Assert.IsNull (ex.InnerException, "#3");
1904 Assert.IsNotNull (ex.Message, "#4");
1905 Assert.IsNull (ex.ParamName, "#5");
1911 public void SetCreationTimeUtc_Path_DoesNotExist ()
1913 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1917 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1919 } catch (FileNotFoundException ex) {
1920 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1921 Assert.AreEqual (path, ex.FileName, "#3");
1922 Assert.IsNull (ex.InnerException, "#3");
1923 Assert.IsNotNull (ex.Message, "#4");
1928 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1929 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1931 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1932 // DeleteFile (path);
1933 // FileStream stream = null;
1935 // stream = File.Create (path);
1937 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1939 // if (stream != null)
1941 // DeleteFile (path);
1946 public void SetCreationTimeUtc_FileLock ()
1948 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1950 FileStream stream = null;
1952 stream = File.Create (path);
1954 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1956 } catch (IOException ex) {
1957 // The process cannot access the file "..."
1958 // because it is being used by another process
1959 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1960 Assert.IsNull (ex.InnerException, "#3");
1961 Assert.IsNotNull (ex.Message, "#4");
1962 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1971 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1974 public void SetLastAccessTime_Path_Null ()
1977 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1979 } catch (ArgumentNullException ex) {
1980 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1981 Assert.IsNull (ex.InnerException, "#3");
1982 Assert.IsNotNull (ex.Message, "#4");
1983 Assert.AreEqual ("path", ex.ParamName, "#5");
1988 public void SetLastAccessTime_Path_Empty ()
1991 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1993 } catch (ArgumentException ex) {
1994 // Empty file name is not legal
1995 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1996 Assert.IsNull (ex.InnerException, "#3");
1997 Assert.IsNotNull (ex.Message, "#4");
1998 Assert.IsNull (ex.ParamName, "#5");
2003 public void SetLastAccessTime_Path_Whitespace ()
2006 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2008 } catch (ArgumentException ex) {
2009 // The path is not of a legal form
2010 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2011 Assert.IsNull (ex.InnerException, "#3");
2012 Assert.IsNotNull (ex.Message, "#4");
2013 Assert.IsNull (ex.ParamName, "#5");
2018 public void SetLastAccessTime_Path_InvalidPathChars ()
2020 // On Unix there are no invalid path chars.
2021 if (Path.InvalidPathChars.Length > 1) {
2023 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2024 new DateTime (2000, 12, 12, 11, 59, 59));
2026 } catch (ArgumentException ex) {
2027 // Illegal characters in path
2028 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2029 Assert.IsNull (ex.InnerException, "#3");
2030 Assert.IsNotNull (ex.Message, "#4");
2031 Assert.IsNull (ex.ParamName, "#5");
2037 public void SetLastAccessTime_Path_DoesNotExist ()
2039 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2043 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2045 } catch (FileNotFoundException ex) {
2046 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2047 Assert.AreEqual (path, ex.FileName, "#3");
2048 Assert.IsNull (ex.InnerException, "#4");
2049 Assert.IsNotNull (ex.Message, "#5");
2054 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2055 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2057 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2058 // DeleteFile (path);
2059 // FileStream stream = null;
2061 // stream = File.Create (path);
2063 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2065 // if (stream != null)
2067 // DeleteFile (path);
2072 public void SetLastAccessTime_FileLock ()
2074 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2076 FileStream stream = null;
2078 stream = File.Create (path);
2080 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2082 } catch (IOException ex) {
2083 // The process cannot access the file "..."
2084 // because it is being used by another process
2085 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2086 Assert.IsNull (ex.InnerException, "#3");
2087 Assert.IsNotNull (ex.Message, "#4");
2088 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2098 public void SetLastAccessTimeUtc_Path_Null ()
2101 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2103 } catch (ArgumentNullException ex) {
2104 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2105 Assert.IsNull (ex.InnerException, "#3");
2106 Assert.IsNotNull (ex.Message, "#4");
2107 Assert.AreEqual ("path", ex.ParamName, "#5");
2112 public void SetCLastAccessTimeUtc_Path_Empty ()
2115 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2117 } catch (ArgumentException ex) {
2118 // Empty file name is not legal
2119 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2120 Assert.IsNull (ex.InnerException, "#3");
2121 Assert.IsNotNull (ex.Message, "#4");
2122 Assert.IsNull (ex.ParamName, "#5");
2127 public void SetLastAccessTimeUtc_Path_Whitespace ()
2130 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2132 } catch (ArgumentException ex) {
2133 // The path is not of a legal form
2134 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2135 Assert.IsNull (ex.InnerException, "#3");
2136 Assert.IsNotNull (ex.Message, "#4");
2137 Assert.IsNull (ex.ParamName, "#5");
2142 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2144 // On Unix there are no invalid path chars.
2145 if (Path.InvalidPathChars.Length > 1) {
2147 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2148 new DateTime (2000, 12, 12, 11, 59, 59));
2150 } catch (ArgumentException ex) {
2151 // Illegal characters in path
2152 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2153 Assert.IsNull (ex.InnerException, "#3");
2154 Assert.IsNotNull (ex.Message, "#4");
2155 Assert.IsNull (ex.ParamName, "#5");
2161 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2163 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2167 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2169 } catch (FileNotFoundException ex) {
2170 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2171 Assert.AreEqual (path, ex.FileName, "#3");
2172 Assert.IsNull (ex.InnerException, "#4");
2173 Assert.IsNotNull (ex.Message, "#5");
2178 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2179 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2181 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2182 // DeleteFile (path);
2183 // FileStream stream = null;
2185 // stream = File.Create (path);
2187 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2189 // if (stream != null)
2191 // DeleteFile (path);
2196 public void SetLastAccessTimeUtc_FileLock ()
2198 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2200 FileStream stream = null;
2202 stream = File.Create (path);
2204 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2206 } catch (IOException ex) {
2207 // The process cannot access the file "..."
2208 // because it is being used by another process
2209 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2210 Assert.IsNull (ex.InnerException, "#3");
2211 Assert.IsNotNull (ex.Message, "#4");
2212 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2221 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2224 public void SetLastWriteTime_Path_Null ()
2227 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2229 } catch (ArgumentNullException ex) {
2230 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2231 Assert.IsNull (ex.InnerException, "#3");
2232 Assert.IsNotNull (ex.Message, "#4");
2233 Assert.AreEqual ("path", ex.ParamName, "#5");
2238 public void SetLastWriteTime_Path_Empty ()
2241 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2243 } catch (ArgumentException ex) {
2244 // Empty file name is not legal
2245 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2246 Assert.IsNull (ex.InnerException, "#3");
2247 Assert.IsNotNull (ex.Message, "#4");
2248 Assert.IsNull (ex.ParamName, "#5");
2253 public void SetLastWriteTime_Path_Whitespace ()
2256 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2258 } catch (ArgumentException ex) {
2259 // The path is not of a legal form
2260 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2261 Assert.IsNull (ex.InnerException, "#3");
2262 Assert.IsNotNull (ex.Message, "#4");
2263 Assert.IsNull (ex.ParamName, "#5");
2268 public void SetLastWriteTime_Path_InvalidPathChars ()
2270 // On Unix there are no invalid path chars.
2271 if (Path.InvalidPathChars.Length > 1) {
2273 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2274 new DateTime (2000, 12, 12, 11, 59, 59));
2276 } catch (ArgumentException ex) {
2277 // Illegal characters in path
2278 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2279 Assert.IsNull (ex.InnerException, "#3");
2280 Assert.IsNotNull (ex.Message, "#4");
2281 Assert.IsNull (ex.ParamName, "#5");
2287 public void SetLastWriteTime_Path_DoesNotExist ()
2289 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2293 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2295 } catch (FileNotFoundException ex) {
2296 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2297 Assert.AreEqual (path, ex.FileName, "#3");
2298 Assert.IsNull (ex.InnerException, "#4");
2299 Assert.IsNotNull (ex.Message, "#5");
2304 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2305 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2307 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2308 // DeleteFile (path);
2309 // FileStream stream = null;
2311 // stream = File.Create (path);
2313 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2315 // if (stream != null)
2317 // DeleteFile (path);
2322 public void SetLastWriteTime_FileLock ()
2324 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2326 FileStream stream = null;
2328 stream = File.Create (path);
2330 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2332 } catch (IOException ex) {
2333 // The process cannot access the file '...'
2334 // because it is being used by another process
2335 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2336 Assert.IsNull (ex.InnerException, "#3");
2337 Assert.IsNotNull (ex.Message, "#4");
2338 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2348 public void SetLastWriteTimeUtc_Path_Null ()
2351 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2353 } catch (ArgumentNullException ex) {
2354 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2355 Assert.IsNull (ex.InnerException, "#3");
2356 Assert.IsNotNull (ex.Message, "#4");
2357 Assert.AreEqual ("path", ex.ParamName, "#5");
2362 public void SetLastWriteTimeUtc_Path_Empty ()
2365 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2367 } catch (ArgumentException ex) {
2368 // Empty file name is not legal
2369 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2370 Assert.IsNull (ex.InnerException, "#3");
2371 Assert.IsNotNull (ex.Message, "#4");
2372 Assert.IsNull (ex.ParamName, "#5");
2377 public void SetLastWriteTimeUtc_Path_Whitespace ()
2380 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
2382 } catch (ArgumentException ex) {
2383 // The path is not of a legal form
2384 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2385 Assert.IsNull (ex.InnerException, "#3");
2386 Assert.IsNotNull (ex.Message, "#4");
2387 Assert.IsNull (ex.ParamName, "#5");
2392 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2394 // On Unix there are no invalid path chars.
2395 if (Path.InvalidPathChars.Length > 1) {
2397 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2398 new DateTime (2000, 12, 12, 11, 59, 59));
2400 } catch (ArgumentException ex) {
2401 // Illegal characters in path
2402 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2403 Assert.IsNull (ex.InnerException, "#3");
2404 Assert.IsNotNull (ex.Message, "#4");
2405 Assert.IsNull (ex.ParamName, "#5");
2411 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2413 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2417 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2419 } catch (FileNotFoundException ex) {
2420 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2421 Assert.AreEqual (path, ex.FileName, "#3");
2422 Assert.IsNull (ex.InnerException, "#4");
2423 Assert.IsNotNull (ex.Message, "#5");
2428 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
2429 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2431 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2432 // DeleteFile (path);
2433 // FileStream stream = null;
2435 // stream = File.Create (path);
2437 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2439 // if (stream != null)
2441 // DeleteFile (path);
2446 public void SetLastWriteTimeUtc_FileLock ()
2448 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2450 FileStream stream = null;
2452 stream = File.Create (path);
2454 File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2456 } catch (IOException ex) {
2457 // The process cannot access the file '...'
2458 // because it is being used by another process
2459 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2460 Assert.IsNull (ex.InnerException, "#3");
2461 Assert.IsNotNull (ex.Message, "#4");
2462 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2472 public void OpenAppend ()
2474 string fn = Path.GetTempFileName ();
2475 using (FileStream s = File.Open (fn, FileMode.Append)) {
2480 void Position (long value)
2482 using (FileStream fs = File.OpenRead (testfile)) {
2483 fs.Position = value;
2484 Assert.AreEqual (value, fs.Position, "Position");
2485 Assert.AreEqual (1, fs.Length, "Length");
2490 public void Position_Small ()
2492 Position (Int32.MaxValue);
2496 [Category ("LargeFileSupport")]
2497 public void Position_Large ()
2499 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2500 Position ((long) Int32.MaxValue + 1);
2503 void Seek (long value)
2505 using (FileStream fs = File.OpenRead (testfile)) {
2506 fs.Seek (value, SeekOrigin.Begin);
2507 Assert.AreEqual (value, fs.Position, "Position");
2508 Assert.AreEqual (1, fs.Length, "Length");
2513 public void Seek_Small ()
2515 Seek (Int32.MaxValue);
2519 [Category ("LargeFileSupport")]
2520 public void Seek_Large ()
2522 // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2523 Seek ((long) Int32.MaxValue + 1);
2526 void LockUnlock (long value)
2528 using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2529 fs.Lock (value - 1, 1);
2530 fs.Unlock (value - 1, 1);
2533 fs.Unlock (0, value);
2538 public void Lock_Small ()
2540 LockUnlock ((long) Int32.MaxValue);
2544 [Category ("LargeFileSupport")]
2545 public void Lock_Large ()
2547 // note: already worked without HAVE_LARGE_FILE_SUPPORT
2548 LockUnlock ((long) Int32.MaxValue + 1);
2552 public void ReadWriteAllText ()
2554 // The MSDN docs said something about
2555 // not including a final new line. it looks
2556 // like that was not true. I'm not sure what
2557 // that was talking about
2558 read_all (string.Empty);
2567 read_all ("a\r\na");
2571 read_all ("\r\n\r\n");
2575 public void ReplaceTest ()
2577 string tmp = Path.Combine (TempFolder, "ReplaceTest");
2578 Directory.CreateDirectory (tmp);
2579 string origFile = Path.Combine (tmp, "origFile");
2580 string replaceFile = Path.Combine (tmp, "replaceFile");
2581 string backupFile = Path.Combine (tmp, "backupFile");
2583 using (StreamWriter sw = File.CreateText (origFile)) {
2584 sw.WriteLine ("origFile");
2586 using (StreamWriter sw = File.CreateText (replaceFile)) {
2587 sw.WriteLine ("replaceFile");
2589 using (StreamWriter sw = File.CreateText (backupFile)) {
2590 sw.WriteLine ("backupFile");
2593 File.Replace (origFile, replaceFile, backupFile);
2594 Assert.IsFalse (File.Exists (origFile), "#1");
2595 using (StreamReader sr = File.OpenText (replaceFile)) {
2596 string txt = sr.ReadLine ();
2597 Assert.AreEqual ("origFile", txt, "#2");
2599 using (StreamReader sr = File.OpenText (backupFile)) {
2600 string txt = sr.ReadLine ();
2601 Assert.AreEqual ("replaceFile", txt, "#3");
2605 static bool RunningOnUnix {
2607 int p = (int) Environment.OSVersion.Platform;
2608 return ((p == 4) || (p == 128) || (p == 6));
2612 void DeleteFile (string path)
2614 if (File.Exists (path))
2618 void DeleteDirectory (string path)
2620 if (Directory.Exists (path))
2621 Directory.Delete (path, true);
2624 void read_all (string s)
2626 string f = Path.GetTempFileName ();
2628 File.WriteAllText (f, s);
2629 string r = File.ReadAllText (f);
2630 Assert.AreEqual (s, r);
2636 void MoveTest (FileAccess acc, FileShare share, bool works)
2638 // use TEMP so since the default location (right along with the assemblies)
2639 // will get access denied when running under some environment (e.g. iOS devices)
2640 var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
2642 File.Delete (file + ".old");
2643 using (var v = File.Create (file)) { }
2645 using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2647 File.Move(file, file + ".old");
2649 Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
2650 } catch (IOException) {
2652 Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
2658 public void MoveTest ()
2660 MoveTest (FileAccess.Read, FileShare.None, false);
2661 MoveTest (FileAccess.Read, FileShare.Read, false);
2662 MoveTest (FileAccess.Read, FileShare.Write, false);
2663 MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2664 MoveTest (FileAccess.Read, FileShare.Delete, true);
2665 MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2666 MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2667 MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2668 MoveTest (FileAccess.Write, FileShare.None, false);
2669 MoveTest (FileAccess.Write, FileShare.Read, false);
2670 MoveTest (FileAccess.Write, FileShare.Write, false);
2671 MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2672 MoveTest (FileAccess.Write, FileShare.Delete, true);
2673 MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2674 MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2675 MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2676 MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2677 MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2678 MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2679 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2680 MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2681 MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2682 MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2683 MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
2687 [DllImport ("libc", SetLastError=true)]
2688 public static extern int symlink (string oldpath, string newpath);
2691 public void SymLinkLoop ()
2694 Assert.Ignore ("Symlink are hard on windows");
2696 var name1 = Path.GetRandomFileName ();
2697 var name2 = Path.GetRandomFileName ();
2699 var path1 = Path.Combine (Path.GetTempPath (), name1);
2700 var path2 = Path.Combine (Path.GetTempPath (), name2);
2702 File.Delete (path1);
2703 File.Delete (path2);
2706 symlink (path1, path2);
2707 symlink (path2, path1);
2709 Assert.IsTrue (File.Exists (path1), "File.Exists must return true for path1 symlink loop");
2710 Assert.IsTrue (File.Exists (path2), "File.Exists must return true for path2 symlink loop");
2713 using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
2714 Assert.Fail ("File.Open must throw for symlink loops");
2716 } catch (IOException ex) {
2717 Assert.AreEqual (0x80070781u, (uint)ex.HResult, "Ensure HRESULT is correct");
2720 File.Delete (path1); //Delete must not throw and must work
2721 Assert.IsFalse (File.Exists (path1), "File.Delete must delete symlink loops");
2725 File.Delete (path1);
2726 File.Delete (path2);
2727 } catch (IOException) {
2728 //Don't double fault any exception from the tests.