[corlib] Address reviewer comments to new test.
[mono.git] / mcs / class / corlib / Test / System.IO / FileTest.cs
1 //
2 // FileTest.cs: Test cases for System.IO.File
3 //
4 // Author: 
5 //     Duncan Mak (duncan@ximian.com)
6 //     Ville Palo (vi64pa@kolumbus.fi)
7 //
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 //
10 // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
11 //
12
13 using System;
14 using System.IO;
15 using System.Globalization;
16 using System.Threading;
17 using System.Runtime.InteropServices;
18
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.IO
23 {
24         [TestFixture]
25         public class FileTest
26         {
27                 CultureInfo old_culture;
28                 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
29                 static Random random = new Random ();
30
31                 [SetUp]
32                 public void SetUp ()
33                 {
34                         DeleteDirectory (TempFolder);
35                         Directory.CreateDirectory (TempFolder);
36                         old_culture = Thread.CurrentThread.CurrentCulture;
37                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
38                 }
39
40                 [TearDown]
41                 public void TearDown ()
42                 {
43                         DeleteDirectory (TempFolder);
44                         Thread.CurrentThread.CurrentCulture = old_culture;
45                 }
46
47                 string path;
48                 string testfile;
49
50                 [TestFixtureSetUp]
51                 public void FixtureSetUp ()
52                 {
53                         path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
54                         testfile = Path.Combine (path, "FileStreamTest.dat");
55                         File.WriteAllText (testfile, "1");
56                 }
57
58                 [TestFixtureTearDown]
59                 public void FixtureTearDown ()
60                 {
61                         if (File.Exists (testfile))
62                                 File.Delete (testfile);                 
63                 }
64
65                 [Test]
66                 public void TestExists ()
67                 {
68                         FileStream s = null;
69                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
70                         try {
71                                 Assert.IsFalse (File.Exists (null), "#1");
72                                 Assert.IsFalse (File.Exists (string.Empty), "#2");
73                                 Assert.IsFalse (File.Exists ("  \t\t  \t \n\t\n \n"), "#3");
74                                 DeleteFile (path);
75                                 s = File.Create (path);
76                                 s.Close ();
77                                 Assert.IsTrue (File.Exists (path), "#4");
78                                 Assert.IsFalse (File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
79                         } finally {
80                                 if (s != null)
81                                         s.Close ();
82                                 DeleteFile (path);
83                         }
84                 }
85
86                 [Test]
87                 public void Exists_InvalidFileName () 
88                 {
89                         Assert.IsFalse (File.Exists ("><|"), "#1");
90                         Assert.IsFalse (File.Exists ("?*"), "#2");
91                 }
92
93                 [Test]
94                 public void Exists_InvalidDirectory () 
95                 {
96                         Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
97                 }
98
99                 [Test]
100                 public void Create_Path_Null ()
101                 {
102                         try {
103                                 File.Create (null);
104                                 Assert.Fail ("#1");
105                         } catch (ArgumentNullException ex) {
106                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
107                                 Assert.IsNull (ex.InnerException, "#3");
108                                 Assert.IsNotNull (ex.Message, "#4");
109                                 Assert.AreEqual ("path", ex.ParamName, "#5");
110                         }
111                 }
112
113                 [Test]
114                 public void Create_Path_Directory ()
115                 {
116                         string path = Path.Combine (TempFolder, "foo");
117                         Directory.CreateDirectory (path);
118                         try {
119                                 File.Create (path);
120                                 Assert.Fail ("#1");
121                         } catch (UnauthorizedAccessException ex) {
122                                 // Access to the path '...' is denied
123                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
124                                 Assert.IsNull (ex.InnerException, "#3");
125                                 Assert.IsNotNull (ex.Message, "#4");
126                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
127                         } finally {
128                                 DeleteDirectory (path);
129                         }
130                 }
131
132                 [Test]
133                 public void Create_Path_Empty ()
134                 {
135                         try {
136                                 File.Create (string.Empty);
137                                 Assert.Fail ("#1");
138                         } catch (ArgumentException ex) {
139                                 // Empty file name is not legal
140                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
141                                 Assert.IsNull (ex.InnerException, "#3");
142                                 Assert.IsNotNull (ex.Message, "#4");
143                                 Assert.IsNull (ex.ParamName, "#5");
144                         }
145                 }
146
147                 [Test]
148                 public void Create_Path_ReadOnly ()
149                 {
150                         string path = Path.Combine (TempFolder, "foo");
151                         File.Create (path).Close ();
152                         File.SetAttributes (path, FileAttributes.ReadOnly);
153                         try {
154                                 File.Create (path);
155                                 Assert.Fail ("#1");
156                         } catch (UnauthorizedAccessException ex) {
157                                 // Access to the path '...' is denied
158                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
159                                 Assert.IsNull (ex.InnerException, "#3");
160                                 Assert.IsNotNull (ex.Message, "#4");
161                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
162                         } finally {
163                                 File.SetAttributes (path, FileAttributes.Normal);
164                         }
165                 }
166
167                 [Test]
168                 public void Create_Path_Whitespace ()
169                 {
170                         try {
171                                 File.Create (" ");
172                                 Assert.Fail ("#1");
173                         } catch (ArgumentException ex) {
174                                 // The path is not of a legal form
175                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
176                                 Assert.IsNull (ex.InnerException, "#3");
177                                 Assert.IsNotNull (ex.Message, "#4");
178                                 Assert.IsNull (ex.ParamName, "#5");
179                         }
180                 }
181
182                 [Test]
183                 public void Create_Directory_DoesNotExist ()
184                 {
185                         FileStream stream = null;
186                         string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
187                         
188                         try {
189                                 stream = File.Create (path);
190                                 Assert.Fail ("#1");
191                         } catch (DirectoryNotFoundException ex) {
192                                 // Could not find a part of the path "..."
193                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
194                                 Assert.IsNull (ex.InnerException, "#3");
195                                 Assert.IsNotNull (ex.Message, "#4");
196                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
197                         } finally {
198                                 if (stream != null)
199                                         stream.Close ();
200                                 DeleteFile (path);
201                         }
202                 }
203
204                 [Test]
205                 public void Create ()
206                 {
207                         FileStream stream = null;
208                         string path = null;
209
210                         /* positive test: create resources/foo */
211                         path = TempFolder + Path.DirectorySeparatorChar + "foo";
212                         try {
213
214                                 stream = File.Create (path);
215                                 Assert.IsTrue (File.Exists (path), "#1");
216                                 stream.Close ();
217                         } finally {
218                                 if (stream != null)
219                                         stream.Close ();
220                                 DeleteFile (path);
221                         }
222
223                         stream = null;
224
225                         /* positive test: repeat test above again to test for overwriting file */
226                         path = TempFolder + Path.DirectorySeparatorChar + "foo";
227                         try {
228                                 stream = File.Create (path);
229                                 Assert.IsTrue (File.Exists (path), "#2");
230                                 stream.Close ();
231                         } finally {
232                                 if (stream != null)
233                                         stream.Close ();
234                                 DeleteFile (path);
235                         }
236                 }
237
238                 [Test]
239                 public void Copy_SourceFileName_Null ()
240                 {
241                         try {
242                                 File.Copy (null, "b");
243                                 Assert.Fail ("#1");
244                         } catch (ArgumentNullException ex) {
245                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
246                                 Assert.IsNull (ex.InnerException, "#3");
247                                 Assert.IsNotNull (ex.Message, "#4");
248                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
249                         }
250                 }
251
252                 [Test]
253                 public void Copy_DestFileName_Null ()
254                 {
255                         try {
256                                 File.Copy ("a", null);
257                                 Assert.Fail ("#1");
258                         } catch (ArgumentNullException ex) {
259                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
260                                 Assert.IsNull (ex.InnerException, "#3");
261                                 Assert.IsNotNull (ex.Message, "#4");
262                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
263                         }
264                 }
265
266                 [Test]
267                 public void Copy_SourceFileName_Empty ()
268                 {
269                         try {
270                                 File.Copy (string.Empty, "b");
271                                 Assert.Fail ("#1");
272                         } catch (ArgumentException ex) {
273                                 // Empty file name is not legal
274                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
275                                 Assert.IsNull (ex.InnerException, "#3");
276                                 Assert.IsNotNull (ex.Message, "#4");
277                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
278                         }
279                 }
280
281                 [Test]
282                 public void Copy_DestFileName_Empty ()
283                 {
284                         try {
285                                 File.Copy ("a", string.Empty);
286                                 Assert.Fail ("#1");
287                         } catch (ArgumentException ex) {
288                                 // Empty file name is not legal
289                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
290                                 Assert.IsNull (ex.InnerException, "#3");
291                                 Assert.IsNotNull (ex.Message, "#4");
292                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
293                         }
294                 }
295
296                 [Test]
297                 public void Copy_SourceFileName_Whitespace ()
298                 {
299                         try {
300                                 File.Copy (" ", "b");
301                                 Assert.Fail ("#1");
302                         } catch (ArgumentException ex) {
303                                 // The path is not of a legal form
304                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
305                                 Assert.IsNull (ex.InnerException, "#3");
306                                 Assert.IsNotNull (ex.Message, "#4");
307                                 Assert.IsNull (ex.ParamName, "#5");
308                         }
309                 }
310
311                 [Test]
312                 public void Copy_DestFileName_Whitespace ()
313                 {
314                         try {
315                                 File.Copy ("a", " ");
316                                 Assert.Fail ("#1");
317                         } catch (ArgumentException ex) {
318                                 // The path is not of a legal form
319                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
320                                 Assert.IsNull (ex.InnerException, "#3");
321                                 Assert.IsNotNull (ex.Message, "#4");
322                                 Assert.IsNull (ex.ParamName, "#5");
323                         }
324                 }
325
326                 [Test]
327                 public void Copy_SourceFileName_DoesNotExist ()
328                 {
329                         try {
330                                 File.Copy ("doesnotexist", "b");
331                                 Assert.Fail ("#1");
332                         } catch (FileNotFoundException ex) {
333                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
334                                 Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
335                                 Assert.IsNull (ex.InnerException, "#4");
336                                 Assert.IsNotNull (ex.Message, "#5");
337                         }
338                 }
339
340                 [Test]
341                 public void Copy_DestFileName_AlreadyExists ()
342                 {
343                         string source = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
344                         string dest = TempFolder + Path.DirectorySeparatorChar + "bar";
345                         DeleteFile (source);
346                         DeleteFile (dest);
347                         try {
348                                 File.Create (source).Close ();
349                                 File.Copy (source, dest);
350                                 try {
351                                         File.Copy (source, dest);
352                                         Assert.Fail ("#1");
353                                 } catch (IOException ex) {
354                                         // The file '...' already exists.
355                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
356                                         Assert.IsNull (ex.InnerException, "#3");
357                                         Assert.IsNotNull (ex.Message, "#4");
358                                         Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
359                                 }
360                         } finally {
361                                 DeleteFile (dest);
362                                 DeleteFile (source);
363                         }
364                 }
365
366                 [Test]
367                 public void Copy_SourceFileName_DestFileName_Same ()
368                 {
369                         string source = TempFolder + Path.DirectorySeparatorChar + "SameFile.txt";
370                         DeleteFile (source);
371                         try {
372                                 // new empty file
373                                 File.Create (source).Close ();
374                                 try {
375                                         File.Copy (source, source, true);
376                                         Assert.Fail ("#1");
377                                 } catch (IOException ex) {
378                                         // process cannot access file ... because it is being used by another process
379                                         Assert.IsNull (ex.InnerException, "#2");
380                                         Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
381                                 }
382                         } finally {
383                                 DeleteFile (source);
384                         }
385                 }
386
387                 [Test]
388                 public void Copy ()
389                 {
390                         string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
391                         string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
392                         /* positive test: copy resources/AFile.txt to resources/bar */
393                         try {
394                                 DeleteFile (path1);
395                                 DeleteFile (path2);
396
397                                 File.Create (path2).Close ();
398                                 File.Copy (path2, path1);
399                                 Assert.IsTrue (File.Exists (path2), "#A1");
400                                 Assert.IsTrue (File.Exists (path1), "#A2");
401
402                                 Assert.IsTrue (File.Exists (path1), "#B1");
403                                 File.Copy (path2, path1, true);
404                                 Assert.IsTrue (File.Exists (path2), "#B2");
405                                 Assert.IsTrue (File.Exists (path1), "#B3");
406                         } finally {
407                                 DeleteFile (path1);
408                                 DeleteFile (path2);
409                         }
410                 }
411
412                 [Test]
413                 public void Delete_Path_Null ()
414                 {
415                         try {
416                                 File.Delete (null);
417                                 Assert.Fail ("#1");
418                         } catch (ArgumentNullException ex) {
419                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
420                                 Assert.IsNull (ex.InnerException, "#3");
421                                 Assert.IsNotNull (ex.Message, "#4");
422                                 Assert.AreEqual ("path", ex.ParamName, "#5");
423                         }
424                 }
425
426                 [Test]
427                 public void Delete_Path_Empty ()
428                 {
429                         try {
430                                 File.Delete (string.Empty);
431                                 Assert.Fail ("#1");
432                         } catch (ArgumentException ex) {
433                                 // Empty file name is not legal
434                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
435                                 Assert.IsNull (ex.InnerException, "#3");
436                                 Assert.IsNotNull (ex.Message, "#4");
437                                 Assert.IsNull (ex.ParamName, "#5");
438                         }
439                 }
440
441                 [Test]
442                 public void Delete_Path_Whitespace ()
443                 {
444                         try {
445                                 File.Delete (" ");
446                                 Assert.Fail ("#1");
447                         } catch (ArgumentException ex) {
448                                 // The path is not of a legal form
449                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
450                                 Assert.IsNull (ex.InnerException, "#3");
451                                 Assert.IsNotNull (ex.Message, "#4");
452                                 Assert.IsNull (ex.ParamName, "#5");
453                         }
454                 }
455
456                 [Test]
457                 public void Delete_Directory_DoesNotExist ()
458                 {
459                         string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
460                         if (Directory.Exists (path))
461                                 Directory.Delete (path, true);
462
463                         try {
464                                 File.Delete (path);
465                                 Assert.Fail ("#1");
466                         } catch (DirectoryNotFoundException ex) {
467                                 // Could not find a part of the path "..."
468                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
469                                 Assert.IsNull (ex.InnerException, "#3");
470                                 Assert.IsNotNull (ex.Message, "#4");
471                                 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
472                         }
473                 }
474
475                 [Test]
476                 public void Delete ()
477                 {
478                         string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
479                         DeleteFile (foopath);
480                         try {
481                                 File.Create (foopath).Close ();
482                                 File.Delete (foopath);
483                                 Assert.IsFalse (File.Exists (foopath));
484                                 File.Delete (foopath);
485                         } finally {
486                                 DeleteFile (foopath);
487                         }
488                 }
489
490                 [Test] // bug #323389
491                 [Category ("NotWorking")]
492                 public void Delete_FileLock ()
493                 {
494                         string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
495                         DeleteFile (path);
496                         FileStream stream = null;
497                         try {
498                                 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
499                                 try {
500                                         File.Delete (path);
501                                         Assert.Fail ("#1");
502                                 } catch (IOException ex) {
503                                         // The process cannot access the file '...'
504                                         // because it is being used by another process
505                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
506                                         Assert.IsNull (ex.InnerException, "#3");
507                                         Assert.IsNotNull (ex.Message, "#4");
508                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
509                                 }
510                         } finally {
511                                 if (stream != null)
512                                         stream.Close ();
513                                 DeleteFile (path);
514                         }
515                 }
516
517                 [Test]
518                 [ExpectedException (typeof(UnauthorizedAccessException))]
519                 public void Delete_File_ReadOnly ()
520                 {
521                         if (RunningOnUnix)
522                                 Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
523
524                         string path = TempFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
525                         DeleteFile (path);
526                         try {
527                                 File.Create (path).Close ();
528                                 File.SetAttributes (path, FileAttributes.ReadOnly);
529                                 File.Delete (path);
530                         } finally {
531                                 File.SetAttributes (path, FileAttributes.Normal);
532                                 DeleteFile (path);
533                         }
534                 }
535
536                 [Test]
537                 public void GetAttributes_Archive ()
538                 {
539                         if (RunningOnUnix)
540                                 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
541
542                         FileAttributes attrs;
543
544                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
545                         File.Create (path).Close ();
546
547                         attrs = File.GetAttributes (path);
548                         Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
549
550                         attrs &= ~FileAttributes.Archive;
551                         File.SetAttributes (path, attrs);
552
553                         attrs = File.GetAttributes (path);
554                         Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
555                 }
556
557                 [Test]
558                 public void GetAttributes_Default_File ()
559                 {
560                         if (RunningOnUnix)
561                                 Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
562
563                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
564                         File.Create (path).Close ();
565
566                         FileAttributes attrs = File.GetAttributes (path);
567
568                         Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
569                         Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
570                         Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
571                         Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
572                         Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
573                         Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
574                 }
575
576                 [Test]
577                 public void GetAttributes_Default_Directory ()
578                 {
579                         FileAttributes attrs = File.GetAttributes (TempFolder);
580
581                         Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
582                         Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
583                         Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
584                         Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
585                         Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
586                         Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
587                 }
588
589                 [Test]
590                 public void GetAttributes_Directory ()
591                 {
592                         FileAttributes attrs = File.GetAttributes (TempFolder);
593
594                         Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
595
596                         attrs &= ~FileAttributes.Directory;
597                         File.SetAttributes (TempFolder, attrs);
598
599                         Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
600
601                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
602                         File.Create (path).Close ();
603
604                         attrs = File.GetAttributes (path);
605                         attrs |= FileAttributes.Directory;
606                         File.SetAttributes (path, attrs);
607
608                         Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
609                 }
610
611                 [Test]
612                 public void GetAttributes_ReadOnly ()
613                 {
614                         FileAttributes attrs;
615
616                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
617                         File.Create (path).Close ();
618
619                         attrs = File.GetAttributes (path);
620                         Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
621
622                         try {
623                                 attrs |= FileAttributes.ReadOnly;
624                                 File.SetAttributes (path, attrs);
625
626                                 attrs = File.GetAttributes (path);
627                                 Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
628                         } finally {
629                                 File.SetAttributes (path, FileAttributes.Normal);
630                         }
631                 }
632
633                 [Test]
634                 public void GetAttributes_System ()
635                 {
636                         if (RunningOnUnix)
637                                 Assert.Ignore ("FileAttributes.System is not supported on Unix.");
638
639                         FileAttributes attrs;
640
641                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
642                         File.Create (path).Close ();
643
644                         attrs = File.GetAttributes (path);
645                         Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
646
647                         attrs |= FileAttributes.System;
648                         File.SetAttributes (path, FileAttributes.System);
649
650                         attrs = File.GetAttributes (path);
651                         Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
652                 }
653
654                 [Test]
655                 public void GetAttributes_Path_DoesNotExist ()
656                 {
657                         string path = Path.Combine (TempFolder, "GetAttributes.tmp");
658                         try {
659                                 File.GetAttributes (path);
660                                 Assert.Fail ("#1");
661                         } catch (FileNotFoundException ex) {
662                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
663                                 Assert.AreEqual (path, ex.FileName, "#3");
664                                 Assert.IsNull (ex.InnerException, "#4");
665                                 Assert.IsNotNull (ex.Message, "#5");
666                         }
667                 }
668
669                 [Test]
670                 public void GetAttributes_Path_Empty ()
671                 {
672                         try {
673                                 File.GetAttributes (string.Empty);
674                                 Assert.Fail ("#1");
675                         } catch (ArgumentException ex) {
676                                 // Empty file name is not legal
677                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
678                                 Assert.IsNull (ex.InnerException, "#3");
679                                 Assert.IsNotNull (ex.Message, "#4");
680                                 Assert.IsNull (ex.ParamName, "#5");
681                         }
682                 }
683
684                 [Test]
685                 public void GetAttributes_Path_Null ()
686                 {
687                         try {
688                                 File.GetAttributes (null);
689                                 Assert.Fail ("#1");
690                         } catch (ArgumentNullException ex) {
691                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
692                                 Assert.IsNull (ex.InnerException, "#3");
693                                 Assert.IsNotNull (ex.Message, "#4");
694                                 Assert.AreEqual ("path", ex.ParamName, "#5");
695                         }
696                 }
697
698                 [Test]
699                 public void Move_SourceFileName_Null ()
700                 {
701                         try {
702                                 File.Move (null, "b");
703                                 Assert.Fail ("#1");
704                         } catch (ArgumentNullException ex) {
705                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
706                                 Assert.IsNull (ex.InnerException, "#3");
707                                 Assert.IsNotNull (ex.Message, "#4");
708                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
709                         }
710                 }
711
712                 [Test]
713                 public void Move_DestFileName_Null ()
714                 {
715                         try {
716                                 File.Move ("a", null);
717                                 Assert.Fail ("#1");
718                         } catch (ArgumentNullException ex) {
719                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
720                                 Assert.IsNull (ex.InnerException, "#3");
721                                 Assert.IsNotNull (ex.Message, "#4");
722                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
723                         }
724                 }
725
726                 [Test]
727                 public void Move_SourceFileName_Empty ()
728                 {
729                         try {
730                                 File.Move (string.Empty, "b");
731                                 Assert.Fail ("#1");
732                         } catch (ArgumentException ex) {
733                                 // Empty file name is not legal
734                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
735                                 Assert.IsNull (ex.InnerException, "#3");
736                                 Assert.IsNotNull (ex.Message, "#4");
737                                 Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
738                         }
739                 }
740
741                 [Test]
742                 public void Move_DestFileName_Empty ()
743                 {
744                         try {
745                                 File.Move ("a", string.Empty);
746                                 Assert.Fail ("#1");
747                         } catch (ArgumentException ex) {
748                                 // Empty file name is not legal
749                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
750                                 Assert.IsNull (ex.InnerException, "#3");
751                                 Assert.IsNotNull (ex.Message, "#4");
752                                 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
753                         }
754                 }
755
756                 [Test]
757                 public void Move_SourceFileName_Whitespace ()
758                 {
759                         try {
760                                 File.Move (" ", "b");
761                                 Assert.Fail ("#1");
762                         } catch (ArgumentException ex) {
763                                 // The path is not of a legal form
764                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
765                                 Assert.IsNull (ex.InnerException, "#3");
766                                 Assert.IsNotNull (ex.Message, "#4");
767                                 Assert.IsNull (ex.ParamName, "#5");
768                         }
769                 }
770
771                 [Test]
772                 public void Move_DestFileName_Whitespace ()
773                 {
774                         try {
775                                 File.Move ("a", " ");
776                                 Assert.Fail ("#1");
777                         } catch (ArgumentException ex) {
778                                 // The path is not of a legal form
779                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
780                                 Assert.IsNull (ex.InnerException, "#3");
781                                 Assert.IsNotNull (ex.Message, "#4");
782                                 Assert.IsNull (ex.ParamName, "#5");
783                         }
784                 }
785
786                 [Test]
787                 public void Move_SourceFileName_DoesNotExist ()
788                 {
789                         string file = TempFolder + Path.DirectorySeparatorChar + "doesnotexist";
790                         DeleteFile (file);
791                         try {
792                                 File.Move (file, "b");
793                                 Assert.Fail ("#1");
794                         } catch (FileNotFoundException ex) {
795                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
796                                 Assert.AreEqual (file, ex.FileName, "#3");
797                                 Assert.IsNull (ex.InnerException, "#4");
798                                 Assert.IsNotNull (ex.Message, "#5");
799                         }
800                 }
801
802                 [Test]
803                 public void Move_DestFileName_DirectoryDoesNotExist ()
804                 {
805                         string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
806                         string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
807                         DeleteFile (sourceFile);
808                         try {
809                                 File.Create (sourceFile).Close ();
810                                 try {
811                                         File.Move (sourceFile, destFile);
812                                         Assert.Fail ("#1");
813                                 } catch (DirectoryNotFoundException ex) {
814                                         // Could not find a part of the path
815                                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
816                                         Assert.IsNull (ex.InnerException, "#3");
817                                         Assert.IsNotNull (ex.Message, "#4");
818                                         Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
819                                 }
820                         } finally {
821                                 DeleteFile (sourceFile);
822                         }
823                 }
824
825                 [Test]
826                 public void Move_DestFileName_AlreadyExists ()
827                 {
828                         string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
829                         string destFile;
830
831                         // move to same directory
832                         File.Create (sourceFile).Close ();
833                         try {
834                                 File.Move (sourceFile, TempFolder);
835                                 Assert.Fail ("#A1");
836                         } catch (IOException ex) {
837                                 // Cannot create a file when that file already exists
838                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
839                                 Assert.IsNull (ex.InnerException, "#A3");
840                                 Assert.IsNotNull (ex.Message, "#A4");
841                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
842                                 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
843                         } finally {
844                                 DeleteFile (sourceFile);
845                         }
846
847                         // move to exist file
848                         File.Create (sourceFile).Close ();
849                         destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
850                         File.Create (destFile).Close ();
851                         try {
852                                 File.Move (sourceFile, destFile);
853                                 Assert.Fail ("#B1");
854                         } catch (IOException ex) {
855                                 // Cannot create a file when that file already exists
856                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
857                                 Assert.IsNull (ex.InnerException, "#B3");
858                                 Assert.IsNotNull (ex.Message, "#B4");
859                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
860                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
861                         } finally {
862                                 DeleteFile (sourceFile);
863                                 DeleteFile (destFile);
864                         }
865
866                         // move to existing directory
867                         File.Create (sourceFile).Close ();
868                         destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
869                         Directory.CreateDirectory (destFile);
870                         try {
871                                 File.Move (sourceFile, destFile);
872                                 Assert.Fail ("#C1");
873                         } catch (IOException ex) {
874                                 // Cannot create a file when that file already exists
875                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
876                                 Assert.IsNull (ex.InnerException, "#C3");
877                                 Assert.IsNotNull (ex.Message, "#C4");
878                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
879                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
880                         } finally {
881                                 DeleteFile (sourceFile);
882                                 DeleteDirectory (destFile);
883                         }
884                 }
885
886                 [Test]
887                 public void Move ()
888                 {
889                         string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
890                         string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
891                         if (!File.Exists (bar)) {
892                                 FileStream f = File.Create(bar);
893                                 f.Close();
894                         }
895                         
896                         Assert.IsTrue (File.Exists (bar), "#1");
897                         File.Move (bar, baz);
898                         Assert.IsFalse (File.Exists (bar), "#2");
899                         Assert.IsTrue (File.Exists (baz), "#3");
900
901                         // Test moving of directories
902                         string dir = Path.Combine (TempFolder, "dir");
903                         string dir2 = Path.Combine (TempFolder, "dir2");
904                         string dir_foo = Path.Combine (dir, "foo");
905                         string dir2_foo = Path.Combine (dir2, "foo");
906
907                         if (Directory.Exists (dir))
908                                 Directory.Delete (dir, true);
909
910                         Directory.CreateDirectory (dir);
911                         Directory.CreateDirectory (dir2);
912                         File.Create (dir_foo).Close ();
913                         File.Move (dir_foo, dir2_foo);
914                         Assert.IsTrue (File.Exists (dir2_foo), "#4");
915                         
916                         Directory.Delete (dir, true);
917                         Directory.Delete (dir2, true);
918                         DeleteFile (dir_foo);
919                         DeleteFile (dir2_foo);
920                 }
921
922                 [Test]
923                 public void Move_FileLock ()
924                 {
925                         string sourceFile = Path.GetTempFileName ();
926                         string destFile = Path.GetTempFileName ();
927
928                         // source file locked
929                         using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
930                                 try {
931                                         File.Move (sourceFile, destFile);
932                                         Assert.Fail ("#A1");
933                                 } catch (IOException ex) {
934                                         // The process cannot access the file because
935                                         // it is being used by another process
936                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
937                                         Assert.IsNull (ex.InnerException, "#A3");
938                                         Assert.IsNotNull (ex.Message, "#A4");
939                                 }
940                         }
941
942                         // destination file locked
943                         using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
944                                 try {
945                                         File.Move (sourceFile, destFile);
946                                         Assert.Fail ("#B1");
947                                 } catch (IOException ex) {
948                                         // The process cannot access the file because
949                                         // it is being used by another process
950                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
951                                         Assert.IsNull (ex.InnerException, "#B3");
952                                         Assert.IsNotNull (ex.Message, "#B4");
953                                 }
954                         }
955                 }
956
957                 [Test]
958                 public void Open ()
959                 {
960                         string path = null;
961                         FileStream stream = null;
962
963                         path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
964                         try {
965                                 if (!File.Exists (path))
966                                         stream = File.Create (path);
967                                 stream.Close ();
968                                 stream = File.Open (path, FileMode.Open);
969                                 stream.Close ();
970                         } finally {
971                                 if (stream != null)
972                                         stream.Close ();
973                                 DeleteFile (path);
974                         }
975
976                         stream = null;
977
978                         if (!File.Exists (path))
979                                 File.Create (path).Close ();
980                         try {
981                                 stream = File.Open (path, FileMode.Open);
982                                 Assert.IsTrue (stream.CanRead, "#A1");
983                                 Assert.IsTrue (stream.CanSeek, "#A2");
984                                 Assert.IsTrue (stream.CanWrite, "#A3");
985                                 stream.Close ();
986
987                                 stream = File.Open (path, FileMode.Open, FileAccess.Write);
988                                 Assert.IsFalse (stream.CanRead, "#B1");
989                                 Assert.IsTrue (stream.CanSeek, "#B2");
990                                 Assert.IsTrue (stream.CanWrite, "#B3");
991                                 stream.Close ();
992
993                                 stream = File.Open (path, FileMode.Open, FileAccess.Read);
994                                 Assert.IsTrue (stream.CanRead, "#C1");
995                                 Assert.IsTrue (stream.CanSeek, "#C2");
996                                 Assert.IsFalse (stream.CanWrite, "#C3");
997                                 stream.Close ();
998                         } finally {
999                                 if (stream != null)
1000                                         stream.Close ();
1001                                 DeleteFile (path);
1002                         }
1003
1004                         stream = null;
1005
1006                         /* Exception tests */
1007                         path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
1008                         try {
1009                                 stream = File.Open (path, FileMode.Open);
1010                                 Assert.Fail ("#D1");
1011                         } catch (FileNotFoundException ex) {
1012                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
1013                                 Assert.AreEqual (path, ex.FileName, "#D3");
1014                                 Assert.IsNull (ex.InnerException, "#D4");
1015                                 Assert.IsNotNull (ex.Message, "#D5");
1016                         } finally {
1017                                 if (stream != null)
1018                                         stream.Close ();
1019                                 DeleteFile (path);
1020                         }
1021                 }
1022
1023                 [Test]
1024                 public void Open_CreateNewMode_ReadAccess ()
1025                 {
1026                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1027                         FileStream stream = null;
1028                         try {
1029                                 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
1030                                 Assert.Fail ("#1");
1031                         } catch (ArgumentException ex) {
1032                                 // Combining FileMode: CreateNew with FileAccess: Read is invalid
1033                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1034                                 Assert.IsNull (ex.InnerException, "#3");
1035                                 Assert.IsNotNull (ex.Message, "#4");
1036                                 Assert.IsNull (ex.ParamName, "#5");
1037                         } finally {
1038                                 if (stream != null)
1039                                         stream.Close ();
1040                                 DeleteFile (path);
1041                         }
1042                 }
1043
1044                 [Test]
1045                 public void Open_AppendMode_ReadAccess ()
1046                 {
1047                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1048                         FileStream s = null;
1049                         if (!File.Exists (path))
1050                                 File.Create (path).Close ();
1051                         try {
1052                                 s = File.Open (path, FileMode.Append, FileAccess.Read);
1053                                 Assert.Fail ("#1");
1054                         } catch (ArgumentException ex) {
1055                                 // Combining FileMode: Append with FileAccess: Read is invalid
1056                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1057                                 Assert.IsNull (ex.InnerException, "#3");
1058                                 Assert.IsNotNull (ex.Message, "#4");
1059                                 Assert.IsNull (ex.ParamName, "#5");
1060                         } finally {
1061                                 if (s != null)
1062                                         s.Close ();
1063                                 DeleteFile (path);
1064                         }
1065                 }
1066
1067                 [Test]
1068                 public void OpenRead ()
1069                 {
1070                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1071                         if (!File.Exists (path))
1072                                 File.Create (path).Close ();
1073                         FileStream stream = null;
1074                         
1075                         try {
1076                                 stream = File.OpenRead (path);
1077                                 Assert.IsTrue (stream.CanRead, "#1");
1078                                 Assert.IsTrue (stream.CanSeek, "#2");
1079                                 Assert.IsFalse (stream.CanWrite, "#3");
1080                         } finally {
1081                                 if (stream != null)
1082                                         stream.Close ();
1083                                 DeleteFile (path);
1084                         }
1085                 }
1086
1087                 [Test]
1088                 public void OpenWrite ()
1089                 {
1090                         string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
1091                         if (!File.Exists (path))
1092                                 File.Create (path).Close ();
1093                         FileStream stream = null;
1094
1095                         try {
1096                                 stream = File.OpenWrite (path);
1097                                 Assert.IsFalse (stream.CanRead, "#1");
1098                                 Assert.IsTrue (stream.CanSeek, "#2");
1099                                 Assert.IsTrue (stream.CanWrite, "#3");
1100                                 stream.Close ();
1101                         } finally {
1102                                 if (stream != null)
1103                                         stream.Close ();
1104                                 DeleteFile (path);
1105                         }
1106                 }
1107
1108                 [Test]
1109                 public void TestGetCreationTime ()
1110                 {
1111                         string path = TempFolder + Path.DirectorySeparatorChar + "baz";
1112                         DeleteFile (path);
1113
1114                         try {
1115                                 File.Create (path).Close();
1116                                 DateTime time = File.GetCreationTime (path);
1117                                 Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
1118                         } finally {
1119                                 DeleteFile (path);
1120                         }
1121                 }
1122
1123                 [Test]
1124                 public void CreationTime ()
1125                 {
1126                         if (RunningOnUnix)
1127                                 Assert.Ignore ("Setting the creation time on Unix is not possible.");
1128
1129                         string path = Path.GetTempFileName ();
1130                         try {
1131                                 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1132                                 DateTime time = File.GetCreationTime (path);
1133                                 Assert.AreEqual (2002, time.Year, "#A1");
1134                                 Assert.AreEqual (4, time.Month, "#A2");
1135                                 Assert.AreEqual (6, time.Day, "#A3");
1136                                 Assert.AreEqual (4, time.Hour, "#A4");
1137                                 Assert.AreEqual (4, time.Second, "#A5");
1138
1139                                 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
1140                                 Assert.AreEqual (2002, time.Year, "#B1");
1141                                 Assert.AreEqual (4, time.Month, "#B2");
1142                                 Assert.AreEqual (6, time.Day, "#B3");
1143                                 Assert.AreEqual (4, time.Hour, "#B4");
1144                                 Assert.AreEqual (4, time.Second, "#B5");
1145
1146                                 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1147                                 time = File.GetCreationTimeUtc (path);
1148                                 Assert.AreEqual (2002, time.Year, "#C1");
1149                                 Assert.AreEqual (4, time.Month, "#C2");
1150                                 Assert.AreEqual (6, time.Day, "#C3");
1151                                 Assert.AreEqual (4, time.Hour, "#C4");
1152                                 Assert.AreEqual (4, time.Second, "#C5");
1153
1154                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
1155                                 Assert.AreEqual (2002, time.Year, "#D1");
1156                                 Assert.AreEqual (4, time.Month, "#D2");
1157                                 Assert.AreEqual (6, time.Day, "#D3");
1158                                 Assert.AreEqual (4, time.Hour, "#D4");
1159                                 Assert.AreEqual (4, time.Second, "#D5");
1160                         } finally {
1161                                 DeleteFile (path);
1162                         }
1163                 }
1164
1165                 [Test]
1166                 public void LastAccessTime ()
1167                 {
1168                         string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
1169                         if (File.Exists (path))
1170                                 File.Delete (path);
1171                         FileStream stream = null;
1172                         try {
1173                                 stream = File.Create (path);
1174                                 stream.Close ();
1175
1176                                 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1177                                 DateTime time = File.GetLastAccessTime (path);
1178                                 Assert.AreEqual (2002, time.Year, "#A1");
1179                                 Assert.AreEqual (4, time.Month, "#A2");
1180                                 Assert.AreEqual (6, time.Day, "#A3");
1181                                 Assert.AreEqual (4, time.Hour, "#A4");
1182                                 Assert.AreEqual (4, time.Second, "#A5");
1183
1184                                 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
1185                                 Assert.AreEqual (2002, time.Year, "#B1");
1186                                 Assert.AreEqual (4, time.Month, "#B2");
1187                                 Assert.AreEqual (6, time.Day, "#B3");
1188                                 Assert.AreEqual (4, time.Hour, "#B4");
1189                                 Assert.AreEqual (4, time.Second, "#B5");
1190
1191                                 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1192                                 time = File.GetLastAccessTimeUtc (path);
1193                                 Assert.AreEqual (2002, time.Year, "#C1");
1194                                 Assert.AreEqual (4, time.Month, "#C2");
1195                                 Assert.AreEqual (6, time.Day, "#C3");
1196                                 Assert.AreEqual (4, time.Hour, "#C4");
1197                                 Assert.AreEqual (4, time.Second, "#C5");
1198
1199                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
1200                                 Assert.AreEqual (2002, time.Year, "#D1");
1201                                 Assert.AreEqual (4, time.Month, "#D2");
1202                                 Assert.AreEqual (6, time.Day, "#D3");
1203                                 Assert.AreEqual (4, time.Hour, "#D4");
1204                                 Assert.AreEqual (4, time.Second, "#D5");
1205                         } finally {
1206                                 if (stream != null)
1207                                         stream.Close ();
1208                                 DeleteFile (path);
1209                         }
1210                 }
1211
1212                 [Test]
1213                 public void LastWriteTime ()
1214                 {
1215                         string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
1216                         if (File.Exists (path))
1217                                 File.Delete (path);
1218                         FileStream stream = null;
1219                         try {
1220                                 stream = File.Create (path);
1221                                 stream.Close ();
1222
1223                                 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
1224                                 DateTime time = File.GetLastWriteTime (path);
1225                                 Assert.AreEqual (2002, time.Year, "#A1");
1226                                 Assert.AreEqual (4, time.Month, "#A2");
1227                                 Assert.AreEqual (6, time.Day, "#A3");
1228                                 Assert.AreEqual (4, time.Hour, "#A4");
1229                                 Assert.AreEqual (4, time.Second, "#A5");
1230
1231                                 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
1232                                 Assert.AreEqual (2002, time.Year, "#B1");
1233                                 Assert.AreEqual (4, time.Month, "#B2");
1234                                 Assert.AreEqual (6, time.Day, "#B3");
1235                                 Assert.AreEqual (4, time.Hour, "#B4");
1236                                 Assert.AreEqual (4, time.Second, "#B5");
1237
1238                                 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
1239                                 time = File.GetLastWriteTimeUtc (path);
1240                                 Assert.AreEqual (2002, time.Year, "#C1");
1241                                 Assert.AreEqual (4, time.Month, "#C2");
1242                                 Assert.AreEqual (6, time.Day, "#C3");
1243                                 Assert.AreEqual (4, time.Hour, "#C4");
1244                                 Assert.AreEqual (4, time.Second, "#C5");
1245
1246                                 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
1247                                 Assert.AreEqual (2002, time.Year, "#D1");
1248                                 Assert.AreEqual (4, time.Month, "#D2");
1249                                 Assert.AreEqual (6, time.Day, "#D3");
1250                                 Assert.AreEqual (4, time.Hour, "#D4");
1251                                 Assert.AreEqual (4, time.Second, "#D5");
1252                         } finally {
1253                                 if (stream != null)
1254                                         stream.Close ();
1255                                 DeleteFile (path);
1256                         }
1257                 }
1258
1259                 [Test]
1260                 public void GetCreationTime_Path_Null ()
1261                 {
1262                         try {
1263                                 File.GetCreationTime (null as string);
1264                                 Assert.Fail ("#1");
1265                         } catch (ArgumentNullException ex) {
1266                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1267                                 Assert.IsNull (ex.InnerException, "#3");
1268                                 Assert.IsNotNull (ex.Message, "#4");
1269                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1270                         }
1271                 }
1272
1273                 [Test]
1274                 public void GetCreationTime_Path_Empty ()
1275                 {
1276                         try {
1277                                 File.GetCreationTime (string.Empty);
1278                                 Assert.Fail ("#1");
1279                         } catch (ArgumentException ex) {
1280                                 // Empty file name is not legal
1281                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1282                                 Assert.IsNull (ex.InnerException, "#3");
1283                                 Assert.IsNotNull (ex.Message, "#4");
1284                                 Assert.IsNull (ex.ParamName, "#5");
1285                         }
1286                 }
1287         
1288                 [Test]
1289                 public void GetCreationTime_Path_DoesNotExist ()
1290                 {
1291                         string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
1292                         DeleteFile (path);
1293
1294                         DateTime time = File.GetCreationTime (path);
1295                         DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1296                         Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1297                         Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1298                         Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1299                         Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1300                         Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1301                         Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1302                 }
1303
1304                 [Test]
1305                 public void GetCreationTime_Path_Whitespace ()
1306                 {
1307                         try {
1308                                 File.GetCreationTime ("    ");
1309                                 Assert.Fail ("#1");
1310                         } catch (ArgumentException ex) {
1311                                 // The path is not of a legal form
1312                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1313                                 Assert.IsNull (ex.InnerException, "#3");
1314                                 Assert.IsNotNull (ex.Message, "#4");
1315                                 Assert.IsNull (ex.ParamName, "#5");
1316                         }
1317                 }
1318
1319                 [Test]
1320                 public void GetCreationTime_Path_InvalidPathChars ()
1321                 {
1322                         try {
1323                                 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
1324                                 Assert.Fail ("#1");
1325                         } catch (ArgumentException ex) {
1326                                 // Illegal characters in path
1327                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1328                                 Assert.IsNull (ex.InnerException, "#3");
1329                                 Assert.IsNotNull (ex.Message, "#4");
1330                                 Assert.IsNull (ex.ParamName, "#5");
1331                         }
1332                 }
1333
1334                 [Test]
1335                 public void GetCreationTimeUtc_Path_Null ()
1336                 {
1337                         try {
1338                                 File.GetCreationTimeUtc (null as string);
1339                                 Assert.Fail ("#1");
1340                         } catch (ArgumentNullException ex) {
1341                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1342                                 Assert.IsNull (ex.InnerException, "#3");
1343                                 Assert.IsNotNull (ex.Message, "#4");
1344                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1345                         }
1346                 }
1347
1348                 [Test]
1349                 public void GetCreationTimeUtc_Path_Empty ()
1350                 {
1351                         try {
1352                                 File.GetCreationTimeUtc (string.Empty);
1353                                 Assert.Fail ("#1");
1354                         } catch (ArgumentException ex) {
1355                                 // Empty file name is not legal
1356                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1357                                 Assert.IsNull (ex.InnerException, "#3");
1358                                 Assert.IsNotNull (ex.Message, "#4");
1359                                 Assert.IsNull (ex.ParamName, "#5");
1360                         }
1361                 }
1362         
1363                 [Test]
1364                 public void GetCreationTimeUtc_Path_DoesNotExist ()
1365                 {
1366                         string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
1367                         DeleteFile (path);
1368
1369                         DateTime time = File.GetCreationTimeUtc (path);
1370                         Assert.AreEqual (1601, time.Year, "#1");
1371                         Assert.AreEqual (1, time.Month, "#2");
1372                         Assert.AreEqual (1, time.Day, "#3");
1373                         Assert.AreEqual (0, time.Hour, "#4");
1374                         Assert.AreEqual (0, time.Second, "#5");
1375                         Assert.AreEqual (0, time.Millisecond, "#6");
1376                 }
1377
1378                 [Test]
1379                 public void GetCreationTimeUtc_Path_Whitespace ()
1380                 {
1381                         try {
1382                                 File.GetCreationTimeUtc ("    ");
1383                                 Assert.Fail ("#1");
1384                         } catch (ArgumentException ex) {
1385                                 // The path is not of a legal form
1386                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1387                                 Assert.IsNull (ex.InnerException, "#3");
1388                                 Assert.IsNotNull (ex.Message, "#4");
1389                                 Assert.IsNull (ex.ParamName, "#5");
1390                         }
1391                 }
1392
1393                 [Test]
1394                 public void GetCreationTimeUtc_Path_InvalidPathChars ()
1395                 {
1396                         try {
1397                                 File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
1398                                 Assert.Fail ("#1");
1399                         } catch (ArgumentException ex) {
1400                                 // Illegal characters in path
1401                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1402                                 Assert.IsNull (ex.InnerException, "#3");
1403                                 Assert.IsNotNull (ex.Message, "#4");
1404                                 Assert.IsNull (ex.ParamName, "#5");
1405                         }
1406                 }
1407
1408                 [Test]
1409                 public void GetLastAccessTime_Path_Null ()
1410                 {
1411                         try {
1412                                 File.GetLastAccessTime (null as string);
1413                                 Assert.Fail ("#1");
1414                         } catch (ArgumentNullException ex) {
1415                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1416                                 Assert.IsNull (ex.InnerException, "#3");
1417                                 Assert.IsNotNull (ex.Message, "#4");
1418                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1419                         }
1420                 }
1421
1422                 [Test]
1423                 public void GetLastAccessTime_Path_Empty ()
1424                 {
1425                         try {
1426                                 File.GetLastAccessTime (string.Empty);
1427                                 Assert.Fail ("#1");
1428                         } catch (ArgumentException ex) {
1429                                 // Empty file name is not legal
1430                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1431                                 Assert.IsNull (ex.InnerException, "#3");
1432                                 Assert.IsNotNull (ex.Message, "#4");
1433                                 Assert.IsNull (ex.ParamName, "#5");
1434                         }
1435                 }
1436         
1437                 [Test]
1438                 public void GetLastAccessTime_Path_DoesNotExist ()
1439                 {
1440                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
1441                         DeleteFile (path);
1442
1443                         DateTime time = File.GetLastAccessTime (path);
1444                         DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1445                         Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1446                         Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1447                         Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1448                         Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1449                         Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1450                         Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1451                 }
1452
1453                 [Test]
1454                 public void GetLastAccessTime_Path_Whitespace ()
1455                 {
1456                         try {
1457                                 File.GetLastAccessTime ("    ");
1458                                 Assert.Fail ("#1");
1459                         } catch (ArgumentException ex) {
1460                                 // The path is not of a legal form
1461                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1462                                 Assert.IsNull (ex.InnerException, "#3");
1463                                 Assert.IsNotNull (ex.Message, "#4");
1464                                 Assert.IsNull (ex.ParamName, "#5");
1465                         }
1466                 }
1467
1468                 [Test]
1469                 public void GetLastAccessTime_Path_InvalidPathChars ()
1470                 {
1471                         try {
1472                                 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
1473                                 Assert.Fail ("#1");
1474                         } catch (ArgumentException ex) {
1475                                 // Illegal characters in path
1476                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1477                                 Assert.IsNull (ex.InnerException, "#3");
1478                                 Assert.IsNotNull (ex.Message, "#4");
1479                                 Assert.IsNull (ex.ParamName, "#5");
1480                         }
1481                 }
1482
1483                 [Test]
1484                 public void GetLastAccessTimeUtc_Path_Null ()
1485                 {
1486                         try {
1487                                 File.GetLastAccessTimeUtc (null as string);
1488                                 Assert.Fail ("#1");
1489                         } catch (ArgumentNullException ex) {
1490                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1491                                 Assert.IsNull (ex.InnerException, "#3");
1492                                 Assert.IsNotNull (ex.Message, "#4");
1493                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1494                         }
1495                 }
1496
1497                 [Test]
1498                 public void GetLastAccessTimeUtc_Path_Empty ()
1499                 {
1500                         try {
1501                                 File.GetLastAccessTimeUtc (string.Empty);
1502                                 Assert.Fail ("#1");
1503                         } catch (ArgumentException ex) {
1504                                 // Empty file name is not legal
1505                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1506                                 Assert.IsNull (ex.InnerException, "#3");
1507                                 Assert.IsNotNull (ex.Message, "#4");
1508                                 Assert.IsNull (ex.ParamName, "#5");
1509                         }
1510                 }
1511         
1512                 [Test]
1513                 public void GetLastAccessTimeUtc_Path_DoesNotExist ()
1514                 {
1515                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1516                         DeleteFile (path);
1517
1518                         DateTime time = File.GetLastAccessTimeUtc (path);
1519                         Assert.AreEqual (1601, time.Year, "#1");
1520                         Assert.AreEqual (1, time.Month, "#2");
1521                         Assert.AreEqual (1, time.Day, "#3");
1522                         Assert.AreEqual (0, time.Hour, "#4");
1523                         Assert.AreEqual (0, time.Second, "#5");
1524                         Assert.AreEqual (0, time.Millisecond, "#6");
1525                 }
1526
1527                 [Test]
1528                 public void GetLastAccessTimeUtc_Path_Whitespace ()
1529                 {
1530                         try {
1531                                 File.GetLastAccessTimeUtc ("    ");
1532                                 Assert.Fail ("#1");
1533                         } catch (ArgumentException ex) {
1534                                 // The path is not of a legal form
1535                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1536                                 Assert.IsNull (ex.InnerException, "#3");
1537                                 Assert.IsNotNull (ex.Message, "#4");
1538                                 Assert.IsNull (ex.ParamName, "#5");
1539                         }
1540                 }
1541
1542                 [Test]
1543                 public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
1544                 {
1545                         try {
1546                                 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
1547                                 Assert.Fail ("#1");
1548                         } catch (ArgumentException ex) {
1549                                 // Illegal characters in path
1550                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1551                                 Assert.IsNull (ex.InnerException, "#3");
1552                                 Assert.IsNotNull (ex.Message, "#4");
1553                                 Assert.IsNull (ex.ParamName, "#5");
1554                         }
1555                 }
1556
1557                 [Test]
1558                 public void GetLastWriteTime_Path_Null ()
1559                 {
1560                         try {
1561                                 File.GetLastWriteTime (null as string);
1562                                 Assert.Fail ("#1");
1563                         } catch (ArgumentNullException ex) {
1564                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1565                                 Assert.IsNull (ex.InnerException, "#3");
1566                                 Assert.IsNotNull (ex.Message, "#4");
1567                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1568                         }
1569                 }
1570
1571                 [Test]
1572                 public void GetLastWriteTime_Path_Empty ()
1573                 {
1574                         try {
1575                                 File.GetLastWriteTime (string.Empty);
1576                                 Assert.Fail ("#1");
1577                         } catch (ArgumentException ex) {
1578                                 // Empty file name is not legal
1579                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1580                                 Assert.IsNull (ex.InnerException, "#3");
1581                                 Assert.IsNotNull (ex.Message, "#4");
1582                                 Assert.IsNull (ex.ParamName, "#5");
1583                         }
1584                 }
1585         
1586                 [Test]
1587                 public void GetLastWriteTime_Path_DoesNotExist ()
1588                 {
1589                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
1590                         DeleteFile (path);
1591
1592                         DateTime time = File.GetLastWriteTime (path);
1593                         DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
1594                         Assert.AreEqual (expectedTime.Year, time.Year, "#1");
1595                         Assert.AreEqual (expectedTime.Month, time.Month, "#2");
1596                         Assert.AreEqual (expectedTime.Day, time.Day, "#3");
1597                         Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
1598                         Assert.AreEqual (expectedTime.Second, time.Second, "#5");
1599                         Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
1600                 }
1601
1602                 [Test]
1603                 public void GetLastWriteTime_Path_Whitespace ()
1604                 {
1605                         try {
1606                                 File.GetLastWriteTime ("    ");
1607                                 Assert.Fail ("#1");
1608                         } catch (ArgumentException ex) {
1609                                 // The path is not of a legal form
1610                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1611                                 Assert.IsNull (ex.InnerException, "#3");
1612                                 Assert.IsNotNull (ex.Message, "#4");
1613                                 Assert.IsNull (ex.ParamName, "#5");
1614                         }
1615                 }
1616
1617                 [Test]
1618                 public void GetLastWriteTime_Path_InvalidPathChars ()
1619                 {
1620                         try {
1621                                 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
1622                                 Assert.Fail ("#1");
1623                         } catch (ArgumentException ex) {
1624                                 // Illegal characters in path
1625                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1626                                 Assert.IsNull (ex.InnerException, "#3");
1627                                 Assert.IsNotNull (ex.Message, "#4");
1628                                 Assert.IsNull (ex.ParamName, "#5");
1629                         }
1630                 }
1631
1632                 [Test]
1633                 public void GetLastWriteTimeUtc_Path_Null ()
1634                 {
1635                         try {
1636                                 File.GetLastWriteTimeUtc (null as string);
1637                                 Assert.Fail ("#1");
1638                         } catch (ArgumentNullException ex) {
1639                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1640                                 Assert.IsNull (ex.InnerException, "#3");
1641                                 Assert.IsNotNull (ex.Message, "#4");
1642                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1643                         }
1644                 }
1645
1646                 [Test]
1647                 public void GetLastWriteTimeUtc_Path_Empty ()
1648                 {
1649                         try {
1650                                 File.GetLastWriteTimeUtc (string.Empty);
1651                                 Assert.Fail ("#1");
1652                         } catch (ArgumentException ex) {
1653                                 // Empty file name is not legal
1654                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1655                                 Assert.IsNull (ex.InnerException, "#3");
1656                                 Assert.IsNotNull (ex.Message, "#4");
1657                                 Assert.IsNull (ex.ParamName, "#5");
1658                         }
1659                 }
1660         
1661                 [Test]
1662                 public void GetLastWriteTimeUtc_Path_DoesNotExist ()
1663                 {
1664                         string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
1665                         DeleteFile (path);
1666
1667                         DateTime time = File.GetLastWriteTimeUtc (path);
1668                         Assert.AreEqual (1601, time.Year, "#1");
1669                         Assert.AreEqual (1, time.Month, "#2");
1670                         Assert.AreEqual (1, time.Day, "#3");
1671                         Assert.AreEqual (0, time.Hour, "#4");
1672                         Assert.AreEqual (0, time.Second, "#5");
1673                         Assert.AreEqual (0, time.Millisecond, "#6");
1674                 }
1675
1676                 [Test]
1677                 public void GetLastWriteTimeUtc_Path_Whitespace ()
1678                 {
1679                         try {
1680                                 File.GetLastWriteTimeUtc ("    ");
1681                                 Assert.Fail ("#1");
1682                         } catch (ArgumentException ex) {
1683                                 // The path is not of a legal form
1684                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1685                                 Assert.IsNull (ex.InnerException, "#3");
1686                                 Assert.IsNotNull (ex.Message, "#4");
1687                                 Assert.IsNull (ex.ParamName, "#5");
1688                         }
1689                 }
1690
1691                 [Test]
1692                 public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
1693                 {
1694                         try {
1695                                 File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
1696                                 Assert.Fail ("#1");
1697                         } catch (ArgumentException ex) {
1698                                 // Illegal characters in path
1699                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1700                                 Assert.IsNull (ex.InnerException, "#3");
1701                                 Assert.IsNotNull (ex.Message, "#4");
1702                                 Assert.IsNull (ex.ParamName, "#5");
1703                         }
1704                 }
1705
1706                 [Test]
1707                 public void FileStreamClose ()
1708                 {
1709                         string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1710                         FileStream stream = null;
1711                         try {
1712                                 stream = File.Create (path);
1713                                 stream.Close ();
1714                                 File.Delete (path);
1715                         } finally {
1716                                 if (stream != null)
1717                                         stream.Close ();
1718                                 DeleteFile (path);
1719                         }
1720                 }
1721                 
1722                 // SetCreationTime and SetCreationTimeUtc exceptions
1723
1724                 [Test]
1725                 public void SetCreationTime_Path_Null ()
1726                 {
1727                         try {
1728                                 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1729                                 Assert.Fail ("#1");
1730                         } catch (ArgumentNullException ex) {
1731                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1732                                 Assert.IsNull (ex.InnerException, "#3");
1733                                 Assert.IsNotNull (ex.Message, "#4");
1734                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1735                         }
1736                 }
1737
1738                 [Test]
1739                 public void SetCreationTime_Path_Empty ()
1740                 {
1741                         try {
1742                                 File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1743                                 Assert.Fail ("#1");
1744                         } catch (ArgumentException ex) {
1745                                 // Empty file name is not legal
1746                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1747                                 Assert.IsNull (ex.InnerException, "#3");
1748                                 Assert.IsNotNull (ex.Message, "#4");
1749                                 Assert.IsNull (ex.ParamName, "#5");
1750                         }
1751                 }
1752
1753                 [Test]
1754                 public void SetCreationTime_Path_Whitespace ()
1755                 {
1756                         try {
1757                                 File.SetCreationTime ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
1758                                 Assert.Fail ("#1");
1759                         } catch (ArgumentException ex) {
1760                                 // The path is not of a legal form
1761                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1762                                 Assert.IsNull (ex.InnerException, "#3");
1763                                 Assert.IsNotNull (ex.Message, "#4");
1764                                 Assert.IsNull (ex.ParamName, "#5");
1765                         }
1766                 }
1767
1768                 [Test]
1769                 public void SetCreationTime_Path_InvalidPathChars ()
1770                 {
1771                         // On Unix there are no invalid path chars.
1772                         if (Path.InvalidPathChars.Length > 1) {
1773                                 try {
1774                                         File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
1775                                                 new DateTime (2000, 12, 12, 11, 59, 59));
1776                                         Assert.Fail ("#1");
1777                                 } catch (ArgumentException ex) {
1778                                         // Illegal characters in path
1779                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1780                                         Assert.IsNull (ex.InnerException, "#3");
1781                                         Assert.IsNotNull (ex.Message, "#4");
1782                                         Assert.IsNull (ex.ParamName, "#5");
1783                                 }
1784                         }
1785                 }
1786
1787                 [Test]
1788                 public void SetCreationTime_Path_DoesNotExist ()
1789                 {
1790                         string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1791                         DeleteFile (path);
1792                         
1793                         try {
1794                                 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1795                                 Assert.Fail ("#1");
1796                         } catch (FileNotFoundException ex) {
1797                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1798                                 Assert.AreEqual (path, ex.FileName, "#3");
1799                                 Assert.IsNull (ex.InnerException, "#4");
1800                                 Assert.IsNotNull (ex.Message, "#5");
1801                         }
1802                 }
1803
1804 //              [Test]
1805 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
1806 //              public void SetCreationTimeArgumentOutOfRangeException1 ()
1807 //              {
1808 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1809 //                      FileStream stream = null;
1810 //                      DeleteFile (path);
1811 //                      try {
1812 //                              stream = File.Create (path);
1813 //                              stream.Close ();
1814 //                              File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1815 //                      } finally {
1816 //                              if (stream != null)
1817 //                                      stream.Close ();
1818 //                              DeleteFile (path);
1819 //                      }
1820 //              }
1821
1822                 [Test]
1823                 public void SetCreationTime_FileLock ()
1824                 {
1825                         string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1826                         DeleteFile (path);
1827                         FileStream stream = null;
1828                         try {
1829                                 stream = File.Create (path);
1830                                 try {
1831                                         File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1832                                         Assert.Fail ("#1");
1833                                 } catch (IOException ex) {
1834                                         // The process cannot access the file '...'
1835                                         // because it is being used by another process
1836                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1837                                         Assert.IsNull (ex.InnerException, "#3");
1838                                         Assert.IsNotNull (ex.Message, "#4");
1839                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1840                                 }
1841                         } finally {
1842                                 if (stream != null)
1843                                         stream.Close ();
1844                                 DeleteFile (path);
1845                         }
1846                 }
1847
1848                 [Test]
1849                 public void SetCreationTimeUtc_Path_Null ()
1850                 { 
1851                         try {
1852                                 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1853                                 Assert.Fail ("#1");
1854                         } catch (ArgumentNullException ex) {
1855                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1856                                 Assert.IsNull (ex.InnerException, "#3");
1857                                 Assert.IsNotNull (ex.Message, "#4");
1858                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1859                         }
1860                 }
1861
1862                 [Test]
1863                 public void SetCreationTimeUtc_Path_Empty ()
1864                 {
1865                         try {
1866                                 File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1867                                 Assert.Fail ("#1");
1868                         } catch (ArgumentException ex) {
1869                                 // Empty file name is not legal
1870                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1871                                 Assert.IsNull (ex.InnerException, "#3");
1872                                 Assert.IsNotNull (ex.Message, "#4");
1873                                 Assert.IsNull (ex.ParamName, "#5");
1874                         }
1875                 }
1876
1877                 [Test]
1878                 public void SetCreationTimeUtc_Path_Whitespace ()
1879                 {
1880                         try {
1881                                 File.SetCreationTimeUtc ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
1882                                 Assert.Fail ("#1");
1883                         } catch (ArgumentException ex) {
1884                                 // The path is not of a legal form
1885                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1886                                 Assert.IsNull (ex.InnerException, "#3");
1887                                 Assert.IsNotNull (ex.Message, "#4");
1888                                 Assert.IsNull (ex.ParamName, "#5");
1889                         }
1890                 }
1891
1892                 [Test]
1893                 public void SetCreationTimeUtc_Path_InvalidPathChars ()
1894                 {
1895                         // On Unix there are no invalid path chars.
1896                         if (Path.InvalidPathChars.Length > 1) {
1897                                 try {
1898                                         File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
1899                                                 new DateTime (2000, 12, 12, 11, 59, 59));
1900                                         Assert.Fail ("#1");
1901                                 } catch (ArgumentException ex) {
1902                                         // Illegal characters in path
1903                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1904                                         Assert.IsNull (ex.InnerException, "#3");
1905                                         Assert.IsNotNull (ex.Message, "#4");
1906                                         Assert.IsNull (ex.ParamName, "#5");
1907                                 }
1908                         }
1909                 }
1910
1911                 [Test]
1912                 public void SetCreationTimeUtc_Path_DoesNotExist ()
1913                 {
1914                         string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1915                         DeleteFile (path);
1916
1917                         try {
1918                                 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1919                                 Assert.Fail ("#1");
1920                         } catch (FileNotFoundException ex) {
1921                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1922                                 Assert.AreEqual (path, ex.FileName, "#3");
1923                                 Assert.IsNull (ex.InnerException, "#3");
1924                                 Assert.IsNotNull (ex.Message, "#4");
1925                         }
1926                 }
1927
1928 //              [Test]
1929 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
1930 //              public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1931 //              {
1932 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1933 //                      DeleteFile (path);
1934 //                      FileStream stream = null;
1935 //                      try {
1936 //                              stream = File.Create (path);
1937 //                              stream.Close ();
1938 //                              File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1939 //                      } finally {
1940 //                              if (stream != null)
1941 //                                      stream.Close();
1942 //                              DeleteFile (path);
1943 //                      }
1944 //              }
1945
1946                 [Test]
1947                 public void SetCreationTimeUtc_FileLock ()
1948                 {
1949                         string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1950                         DeleteFile (path);
1951                         FileStream stream = null;
1952                         try {
1953                                 stream = File.Create (path);
1954                                 try {
1955                                         File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1956                                         Assert.Fail ("#1");
1957                                 } catch (IOException ex) {
1958                                         // The process cannot access the file "..."
1959                                         // because it is being used by another process
1960                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1961                                         Assert.IsNull (ex.InnerException, "#3");
1962                                         Assert.IsNotNull (ex.Message, "#4");
1963                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
1964                                 }
1965                         } finally {
1966                                 if (stream != null)
1967                                         stream.Close ();
1968                                 DeleteFile (path);
1969                         }
1970                 }
1971
1972                 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1973
1974                 [Test]
1975                 public void SetLastAccessTime_Path_Null ()
1976                 {
1977                         try {
1978                                 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1979                                 Assert.Fail ("#1");
1980                         } catch (ArgumentNullException ex) {
1981                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1982                                 Assert.IsNull (ex.InnerException, "#3");
1983                                 Assert.IsNotNull (ex.Message, "#4");
1984                                 Assert.AreEqual ("path", ex.ParamName, "#5");
1985                         }
1986                 }
1987
1988                 [Test]
1989                 public void SetLastAccessTime_Path_Empty ()
1990                 {
1991                         try {
1992                                 File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
1993                                 Assert.Fail ("#1");
1994                         } catch (ArgumentException ex) {
1995                                 // Empty file name is not legal
1996                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1997                                 Assert.IsNull (ex.InnerException, "#3");
1998                                 Assert.IsNotNull (ex.Message, "#4");
1999                                 Assert.IsNull (ex.ParamName, "#5");
2000                         }
2001                 }
2002
2003                 [Test]
2004                 public void SetLastAccessTime_Path_Whitespace ()
2005                 {
2006                         try {
2007                                 File.SetLastAccessTime ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2008                                 Assert.Fail ("#1");
2009                         } catch (ArgumentException ex) {
2010                                 // The path is not of a legal form
2011                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2012                                 Assert.IsNull (ex.InnerException, "#3");
2013                                 Assert.IsNotNull (ex.Message, "#4");
2014                                 Assert.IsNull (ex.ParamName, "#5");
2015                         }
2016                 }
2017
2018                 [Test]
2019                 public void SetLastAccessTime_Path_InvalidPathChars ()
2020                 {
2021                         // On Unix there are no invalid path chars.
2022                         if (Path.InvalidPathChars.Length > 1) {
2023                                 try {
2024                                         File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
2025                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2026                                         Assert.Fail ("#1");
2027                                 } catch (ArgumentException ex) {
2028                                         // Illegal characters in path
2029                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2030                                         Assert.IsNull (ex.InnerException, "#3");
2031                                         Assert.IsNotNull (ex.Message, "#4");
2032                                         Assert.IsNull (ex.ParamName, "#5");
2033                                 }
2034                         }
2035                 }
2036
2037                 [Test]
2038                 public void SetLastAccessTime_Path_DoesNotExist ()
2039                 {
2040                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
2041                         DeleteFile (path);
2042
2043                         try {
2044                                 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2045                                 Assert.Fail ("#1");
2046                         } catch (FileNotFoundException ex) {
2047                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2048                                 Assert.AreEqual (path, ex.FileName, "#3");
2049                                 Assert.IsNull (ex.InnerException, "#4");
2050                                 Assert.IsNotNull (ex.Message, "#5");
2051                         }
2052                 }
2053
2054 //              [Test]
2055 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2056 //              public void SetLastAccessTimeArgumentOutOfRangeException1 ()
2057 //              {
2058 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
2059 //                      DeleteFile (path);
2060 //                      FileStream stream = null;
2061 //                      try {
2062 //                              stream = File.Create (path);
2063 //                              stream.Close ();
2064 //                              File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2065 //                      } finally {
2066 //                              if (stream != null)
2067 //                                      stream.Close ();
2068 //                              DeleteFile (path);
2069 //                      }
2070 //              }
2071
2072                 [Test]
2073                 public void SetLastAccessTime_FileLock ()
2074                 {
2075                         string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
2076                         DeleteFile (path);
2077                         FileStream stream = null;
2078                         try {
2079                                 stream = File.Create (path);
2080                                 try {
2081                                         File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2082                                         Assert.Fail ("#1");
2083                                 } catch (IOException ex) {
2084                                         // The process cannot access the file "..."
2085                                         // because it is being used by another process
2086                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2087                                         Assert.IsNull (ex.InnerException, "#3");
2088                                         Assert.IsNotNull (ex.Message, "#4");
2089                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2090                                 }
2091                         } finally {
2092                                 if (stream != null)
2093                                         stream.Close ();
2094                                 DeleteFile (path);
2095                         }
2096                 }
2097
2098                 [Test]
2099                 public void SetLastAccessTimeUtc_Path_Null ()
2100                 {
2101                         try {
2102                                 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2103                                 Assert.Fail ("#1");
2104                         } catch (ArgumentNullException ex) {
2105                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2106                                 Assert.IsNull (ex.InnerException, "#3");
2107                                 Assert.IsNotNull (ex.Message, "#4");
2108                                 Assert.AreEqual ("path", ex.ParamName, "#5");
2109                         }
2110                 }
2111
2112                 [Test]
2113                 public void SetCLastAccessTimeUtc_Path_Empty ()
2114                 {
2115                         try {
2116                                 File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2117                                 Assert.Fail ("#1");
2118                         } catch (ArgumentException ex) {
2119                                 // Empty file name is not legal
2120                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2121                                 Assert.IsNull (ex.InnerException, "#3");
2122                                 Assert.IsNotNull (ex.Message, "#4");
2123                                 Assert.IsNull (ex.ParamName, "#5");
2124                         }
2125                 }
2126
2127                 [Test]
2128                 public void SetLastAccessTimeUtc_Path_Whitespace ()
2129                 {
2130                         try {
2131                                 File.SetLastAccessTimeUtc ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2132                                 Assert.Fail ("#1");
2133                         } catch (ArgumentException ex) {
2134                                 // The path is not of a legal form
2135                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2136                                 Assert.IsNull (ex.InnerException, "#3");
2137                                 Assert.IsNotNull (ex.Message, "#4");
2138                                 Assert.IsNull (ex.ParamName, "#5");
2139                         }
2140                 }
2141
2142                 [Test]
2143                 public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
2144                 {
2145                         // On Unix there are no invalid path chars.
2146                         if (Path.InvalidPathChars.Length > 1) {
2147                                 try {
2148                                         File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
2149                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2150                                         Assert.Fail ("#1");
2151                                 } catch (ArgumentException ex) {
2152                                         // Illegal characters in path
2153                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2154                                         Assert.IsNull (ex.InnerException, "#3");
2155                                         Assert.IsNotNull (ex.Message, "#4");
2156                                         Assert.IsNull (ex.ParamName, "#5");
2157                                 }
2158                         }
2159                 }
2160
2161                 [Test]
2162                 public void SetLastAccessTimeUtc_Path_DoesNotExist ()
2163                 {
2164                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
2165                         DeleteFile (path);
2166
2167                         try {
2168                                 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2169                                 Assert.Fail ("#1");
2170                         } catch (FileNotFoundException ex) {
2171                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2172                                 Assert.AreEqual (path, ex.FileName, "#3");
2173                                 Assert.IsNull (ex.InnerException, "#4");
2174                                 Assert.IsNotNull (ex.Message, "#5");
2175                         }
2176                 }
2177
2178 //              [Test]
2179 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2180 //              public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
2181 //              {
2182 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
2183 //                      DeleteFile (path);
2184 //                      FileStream stream = null;
2185 //                      try {
2186 //                              stream = File.Create (path);
2187 //                              stream.Close ();
2188 //                              File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2189 //                      } finally {
2190 //                              if (stream != null)
2191 //                                      stream.Close ();
2192 //                              DeleteFile (path);
2193 //                      }
2194 //              }
2195
2196                 [Test]
2197                 public void SetLastAccessTimeUtc_FileLock ()
2198                 {
2199                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
2200                         DeleteFile (path);
2201                         FileStream stream = null;
2202                         try {
2203                                 stream = File.Create (path);
2204                                 try {
2205                                         File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2206                                         Assert.Fail ("#1");
2207                                 } catch (IOException ex) {
2208                                         // The process cannot access the file "..."
2209                                         // because it is being used by another process
2210                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2211                                         Assert.IsNull (ex.InnerException, "#3");
2212                                         Assert.IsNotNull (ex.Message, "#4");
2213                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2214                                 }
2215                         } finally {
2216                                 if (stream != null)
2217                                         stream.Close ();
2218                                 DeleteFile (path);
2219                         }
2220                 }
2221
2222                 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
2223
2224                 [Test]
2225                 public void SetLastWriteTime_Path_Null ()
2226                 {
2227                         try {
2228                                 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2229                                 Assert.Fail ("#1");
2230                         } catch (ArgumentNullException ex) {
2231                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2232                                 Assert.IsNull (ex.InnerException, "#3");
2233                                 Assert.IsNotNull (ex.Message, "#4");
2234                                 Assert.AreEqual ("path", ex.ParamName, "#5");
2235                         }
2236                 }
2237
2238                 [Test]
2239                 public void SetLastWriteTime_Path_Empty ()
2240                 {
2241                         try {
2242                                 File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2243                                 Assert.Fail ("#1");
2244                         } catch (ArgumentException ex) {
2245                                 // Empty file name is not legal
2246                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2247                                 Assert.IsNull (ex.InnerException, "#3");
2248                                 Assert.IsNotNull (ex.Message, "#4");
2249                                 Assert.IsNull (ex.ParamName, "#5");
2250                         }
2251                 }
2252
2253                 [Test]
2254                 public void SetLastWriteTime_Path_Whitespace ()
2255                 {
2256                         try {
2257                                 File.SetLastWriteTime ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2258                                 Assert.Fail ("#1");
2259                         } catch (ArgumentException ex) {
2260                                 // The path is not of a legal form
2261                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2262                                 Assert.IsNull (ex.InnerException, "#3");
2263                                 Assert.IsNotNull (ex.Message, "#4");
2264                                 Assert.IsNull (ex.ParamName, "#5");
2265                         }
2266                 }
2267
2268                 [Test]
2269                 public void SetLastWriteTime_Path_InvalidPathChars ()
2270                 {
2271                         // On Unix there are no invalid path chars.
2272                         if (Path.InvalidPathChars.Length > 1) {
2273                                 try {
2274                                         File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
2275                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2276                                         Assert.Fail ("#1");
2277                                 } catch (ArgumentException ex) {
2278                                         // Illegal characters in path
2279                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2280                                         Assert.IsNull (ex.InnerException, "#3");
2281                                         Assert.IsNotNull (ex.Message, "#4");
2282                                         Assert.IsNull (ex.ParamName, "#5");
2283                                 }
2284                         }
2285                 }
2286
2287                 [Test]
2288                 public void SetLastWriteTime_Path_DoesNotExist ()
2289                 {
2290                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
2291                         DeleteFile (path);
2292
2293                         try {
2294                                 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
2295                                 Assert.Fail ("#1");
2296                         } catch (FileNotFoundException ex) {
2297                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2298                                 Assert.AreEqual (path, ex.FileName, "#3");
2299                                 Assert.IsNull (ex.InnerException, "#4");
2300                                 Assert.IsNotNull (ex.Message, "#5");
2301                         }
2302                 }
2303
2304 //              [Test]
2305 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2306 //              public void SetLastWriteTimeArgumentOutOfRangeException1 ()
2307 //              {
2308 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
2309 //                      DeleteFile (path);
2310 //                      FileStream stream = null;
2311 //                      try {
2312 //                              stream = File.Create (path);
2313 //                              stream.Close ();
2314 //                              File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2315 //                      } finally {
2316 //                              if (stream != null)
2317 //                                      stream.Close ();
2318 //                              DeleteFile (path);
2319 //                      }
2320 //              }
2321
2322                 [Test]
2323                 public void SetLastWriteTime_FileLock ()
2324                 {
2325                         string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
2326                         DeleteFile (path);
2327                         FileStream stream = null;
2328                         try {
2329                                 stream = File.Create (path);
2330                                 try {
2331                                         File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
2332                                         Assert.Fail ("#1");
2333                                 } catch (IOException ex) {
2334                                         // The process cannot access the file '...'
2335                                         // because it is being used by another process
2336                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2337                                         Assert.IsNull (ex.InnerException, "#3");
2338                                         Assert.IsNotNull (ex.Message, "#4");
2339                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2340                                 }
2341                         } finally {
2342                                 if (stream != null)
2343                                         stream.Close ();
2344                                 DeleteFile (path);
2345                         }
2346                 }
2347
2348                 [Test]
2349                 public void SetLastWriteTimeUtc_Path_Null ()
2350                 {
2351                         try {
2352                                 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
2353                                 Assert.Fail ("#1");
2354                         } catch (ArgumentNullException ex) {
2355                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2356                                 Assert.IsNull (ex.InnerException, "#3");
2357                                 Assert.IsNotNull (ex.Message, "#4");
2358                                 Assert.AreEqual ("path", ex.ParamName, "#5");
2359                         }
2360                 }
2361
2362                 [Test]
2363                 public void SetLastWriteTimeUtc_Path_Empty ()
2364                 {
2365                         try {
2366                                 File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
2367                                 Assert.Fail ("#1");
2368                         } catch (ArgumentException ex) {
2369                                 // Empty file name is not legal
2370                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2371                                 Assert.IsNull (ex.InnerException, "#3");
2372                                 Assert.IsNotNull (ex.Message, "#4");
2373                                 Assert.IsNull (ex.ParamName, "#5");
2374                         }
2375                 }
2376
2377                 [Test]
2378                 public void SetLastWriteTimeUtc_Path_Whitespace ()
2379                 {
2380                         try {
2381                                 File.SetLastWriteTimeUtc ("     ", new DateTime (2000, 12, 12, 11, 59, 59));
2382                                 Assert.Fail ("#1");
2383                         } catch (ArgumentException ex) {
2384                                 // The path is not of a legal form
2385                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2386                                 Assert.IsNull (ex.InnerException, "#3");
2387                                 Assert.IsNotNull (ex.Message, "#4");
2388                                 Assert.IsNull (ex.ParamName, "#5");
2389                         }
2390                 }
2391
2392                 [Test]
2393                 public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
2394                 {
2395                         // On Unix there are no invalid path chars.
2396                         if (Path.InvalidPathChars.Length > 1) {
2397                                 try {
2398                                         File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
2399                                                 new DateTime (2000, 12, 12, 11, 59, 59));
2400                                         Assert.Fail ("#1");
2401                                 } catch (ArgumentException ex) {
2402                                         // Illegal characters in path
2403                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
2404                                         Assert.IsNull (ex.InnerException, "#3");
2405                                         Assert.IsNotNull (ex.Message, "#4");
2406                                         Assert.IsNull (ex.ParamName, "#5");
2407                                 }
2408                         }
2409                 }
2410
2411                 [Test]
2412                 public void SetLastWriteTimeUtc_Path_DoesNotExist ()
2413                 {
2414                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
2415                         DeleteFile (path);
2416
2417                         try {
2418                                 File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
2419                                 Assert.Fail ("#1");
2420                         } catch (FileNotFoundException ex) {
2421                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
2422                                 Assert.AreEqual (path, ex.FileName, "#3");
2423                                 Assert.IsNull (ex.InnerException, "#4");
2424                                 Assert.IsNotNull (ex.Message, "#5");
2425                         }
2426                 }
2427
2428 //              [Test]
2429 //              [ExpectedException(typeof (ArgumentOutOfRangeException))]
2430 //              public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
2431 //              {
2432 //                      string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
2433 //                      DeleteFile (path);
2434 //                      FileStream stream = null;
2435 //                      try {
2436 //                              stream = File.Create (path);
2437 //                              stream.Close ();
2438 //                              File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2439 //                      } finally {
2440 //                              if (stream != null)
2441 //                                      stream.Close ();
2442 //                              DeleteFile (path);
2443 //                      }
2444 //              }
2445
2446                 [Test]
2447                 public void SetLastWriteTimeUtc_FileLock ()
2448                 {
2449                         string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
2450                         DeleteFile (path);
2451                         FileStream stream = null;
2452                         try {
2453                                 stream = File.Create (path);
2454                                 try {
2455                                         File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
2456                                         Assert.Fail ("#1");
2457                                 } catch (IOException ex) {
2458                                         // The process cannot access the file '...'
2459                                         // because it is being used by another process
2460                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2461                                         Assert.IsNull (ex.InnerException, "#3");
2462                                         Assert.IsNotNull (ex.Message, "#4");
2463                                         Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
2464                                 }
2465                         } finally {
2466                                 if (stream != null)
2467                                         stream.Close ();
2468                                 DeleteFile (path);
2469                         }
2470                 }
2471                 
2472                 [Test]
2473                 public void OpenAppend ()
2474                 {
2475                         string fn = Path.GetTempFileName ();
2476                         using (FileStream s = File.Open (fn, FileMode.Append)) {
2477                         }
2478                         DeleteFile (fn);
2479                 }
2480
2481                         void Position (long value)
2482                 {
2483                         using (FileStream fs = File.OpenRead (testfile)) {
2484                                 fs.Position = value;
2485                                 Assert.AreEqual (value, fs.Position, "Position");
2486                                 Assert.AreEqual (1, fs.Length, "Length");
2487                         }
2488                 }
2489                 
2490                 [Test]
2491                 public void Position_Small ()
2492                 {
2493                         Position (Int32.MaxValue);
2494                 }
2495
2496                 [Test]
2497                 [Category ("LargeFileSupport")]
2498                 public void Position_Large ()
2499                 {
2500                         // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2501                         Position ((long) Int32.MaxValue + 1);
2502                 }
2503                 
2504                 void Seek (long value)
2505                 {
2506                         using (FileStream fs = File.OpenRead (testfile)) {
2507                                 fs.Seek (value, SeekOrigin.Begin);
2508                                 Assert.AreEqual (value, fs.Position, "Position");
2509                                 Assert.AreEqual (1, fs.Length, "Length");
2510                         }
2511                 }
2512                 
2513                 [Test]
2514                 public void Seek_Small ()
2515                 {
2516                         Seek (Int32.MaxValue);
2517                 }
2518
2519                 [Test]
2520                 [Category ("LargeFileSupport")]
2521                 public void Seek_Large ()
2522                 {
2523                         // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
2524                         Seek ((long) Int32.MaxValue + 1);
2525                 }
2526                 
2527                 void LockUnlock (long value)
2528                 {
2529                         using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
2530                                 fs.Lock (value - 1, 1);
2531                                 fs.Unlock (value - 1, 1);
2532                                 
2533                                 fs.Lock (0, value);
2534                                 fs.Unlock (0, value);
2535                         }
2536                 }
2537                 
2538                 [Test]
2539                 public void Lock_Small ()
2540                 {
2541                         LockUnlock ((long) Int32.MaxValue);
2542                 }
2543
2544                 [Test]
2545                 [Category ("LargeFileSupport")]
2546                 public void Lock_Large ()
2547                 {
2548                         // note: already worked without HAVE_LARGE_FILE_SUPPORT
2549                         LockUnlock ((long) Int32.MaxValue + 1);
2550                 }
2551         
2552                 [Test]
2553                 public void ReadWriteAllText ()
2554                 {
2555                         // The MSDN docs said something about
2556                         // not including a final new line. it looks
2557                         // like that was not true. I'm not sure what
2558                         // that was talking about
2559                         read_all (string.Empty);
2560                         read_all ("\r");
2561                         read_all ("\n");
2562                         read_all ("\r\n");
2563                         read_all ("a\r");
2564                         read_all ("a\n");
2565                         read_all ("a\r\n");
2566                         read_all ("a\ra");
2567                         read_all ("a\na");
2568                         read_all ("a\r\na");
2569                         read_all ("a");
2570                         read_all ("\r\r");
2571                         read_all ("\n\n");
2572                         read_all ("\r\n\r\n");
2573                 }
2574
2575                 [Test]
2576                 public void ReplaceTest ()
2577                 {
2578                         string tmp = Path.Combine (TempFolder, "ReplaceTest");
2579                         Directory.CreateDirectory (tmp);
2580                         string origFile = Path.Combine (tmp, "origFile");
2581                         string replaceFile = Path.Combine (tmp, "replaceFile");
2582                         string backupFile = Path.Combine (tmp, "backupFile");
2583
2584                         using (StreamWriter sw = File.CreateText (origFile)) {
2585                                 sw.WriteLine ("origFile");
2586                         }
2587                         using (StreamWriter sw = File.CreateText (replaceFile)) {
2588                                 sw.WriteLine ("replaceFile");
2589                         }
2590                         using (StreamWriter sw = File.CreateText (backupFile)) {
2591                                 sw.WriteLine ("backupFile");
2592                         }
2593
2594                         File.Replace (origFile, replaceFile, backupFile);
2595                         Assert.IsFalse (File.Exists (origFile), "#1");
2596                         using (StreamReader sr = File.OpenText (replaceFile)) {
2597                                 string txt = sr.ReadLine ();
2598                                 Assert.AreEqual ("origFile", txt, "#2");
2599                         }
2600                         using (StreamReader sr = File.OpenText (backupFile)) {
2601                                 string txt = sr.ReadLine ();
2602                                 Assert.AreEqual ("replaceFile", txt, "#3");
2603                         }
2604                 }
2605
2606                 static bool RunningOnUnix {
2607                         get {
2608                                 int p = (int) Environment.OSVersion.Platform;
2609                                 return ((p == 4) || (p == 128) || (p == 6));
2610                         }
2611                 }
2612
2613                 void DeleteFile (string path)
2614                 {
2615                         if (File.Exists (path))
2616                                 File.Delete (path);
2617                 }
2618
2619                 void DeleteDirectory (string path)
2620                 {
2621                         if (Directory.Exists (path))
2622                                 Directory.Delete (path, true);
2623                 }
2624
2625                 void read_all (string s)
2626                 {
2627                         string f = Path.GetTempFileName ();
2628                         try {
2629                                 File.WriteAllText (f, s);
2630                                 string r = File.ReadAllText (f);
2631                                 Assert.AreEqual (s, r);
2632                         } finally {
2633                                 DeleteFile (f);
2634                         }
2635                 }
2636
2637                 void MoveTest (FileAccess acc, FileShare share, bool works)
2638                 {
2639                         // use TEMP so since the default location (right along with the assemblies) 
2640                         // will get access denied when running under some environment (e.g. iOS devices)
2641                         var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
2642
2643                         File.Delete (file + ".old");
2644                         using (var v = File.Create (file)) { }
2645
2646                         using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
2647                                 try {
2648                                         File.Move(file, file + ".old");
2649                                         if (!works)
2650                                                 Assert.Fail ("Move with ({0}) and  ({1}) did not fail", acc, share);
2651                                 } catch (IOException) {
2652                                         if (works)
2653                                                 Assert.Fail ("Move with ({0}) and  ({1}) did fail", acc, share);
2654                                 }
2655                         }
2656                 }
2657
2658                 [Test]
2659                 public void MoveTest ()
2660                 {
2661                         MoveTest (FileAccess.Read, FileShare.None, false);
2662                         MoveTest (FileAccess.Read, FileShare.Read, false);
2663                         MoveTest (FileAccess.Read, FileShare.Write, false);
2664                         MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
2665                         MoveTest (FileAccess.Read, FileShare.Delete, true);
2666                         MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
2667                         MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
2668                         MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
2669                         MoveTest (FileAccess.Write, FileShare.None, false);
2670                         MoveTest (FileAccess.Write, FileShare.Read, false);
2671                         MoveTest (FileAccess.Write, FileShare.Write, false);
2672                         MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
2673                         MoveTest (FileAccess.Write, FileShare.Delete, true);
2674                         MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
2675                         MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
2676                         MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
2677                         MoveTest (FileAccess.ReadWrite, FileShare.None, false);
2678                         MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
2679                         MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
2680                         MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
2681                         MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
2682                         MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
2683                         MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
2684                         MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
2685                 }
2686
2687
2688                 [DllImport ("libc", SetLastError=true)]
2689                 public static extern int symlink (string oldpath, string newpath);
2690
2691                 [DllImport ("libc", SetLastError=true)]
2692                 public static extern int rename (string oldpath, string newpath);
2693
2694                 /* This deserves some examplation:
2695                 Calling File.Delete on a bad symlink will throw UnauthorizedAccessException so they are highlander files to managed code.
2696                 The hack here is to break the cycle and make them dangling symlinks instead.
2697                  */
2698                 static void DeleteBadSymLink (string path) {
2699                         string newpath;
2700
2701                         lock (random) {
2702                                 do {
2703                                         newpath = path + random.Next ();
2704                                 } while (File.Exists (newpath));
2705                         }
2706
2707                         rename (path, newpath);
2708                         File.Delete (newpath);
2709                 }
2710
2711                 [Test]
2712                 public void SymLinkLoop ()
2713                 {
2714                         if (!RunningOnUnix)
2715                                 Assert.Ignore ("Symlink are hard on windows");
2716                         string name1, name2;
2717                         lock (random) {
2718                                 name1 = "first_file" + random.Next ();
2719                                 name2 = "second_file" + random.Next ();
2720                         }
2721
2722                         var path1 = Path.Combine (Path.GetTempPath (), name1);
2723                         var path2 = Path.Combine (Path.GetTempPath (), name2);
2724
2725                         DeleteBadSymLink (path1);
2726                         DeleteBadSymLink (path2);
2727                         try {
2728                                 symlink (path1, path2);
2729                                 symlink (path2, path1);
2730
2731                                 Assert.IsFalse (File.Exists (path1), "File.Exists must return false for symlink loops");
2732
2733                                 try {
2734                                         using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
2735                                                 Assert.Fail ("File.Exists must throw for symlink loops");
2736                                         }
2737                                 } catch (UnauthorizedAccessException ex) {
2738                                         Assert.AreEqual (0x80070005u, (uint)ex.HResult, "Ensure HRESULT is correct");
2739                                 }
2740
2741
2742                                 try {
2743                                         File.Delete (path1);
2744                                 } catch (UnauthorizedAccessException ex) {
2745                                         Assert.AreEqual (0x80070005u, (uint)ex.HResult, "Ensure HRESULT is correct");
2746                                 }
2747
2748                         } finally {
2749                                 DeleteBadSymLink (path1);
2750                                 DeleteBadSymLink (path2);
2751
2752                         }
2753                 }
2754         }
2755 }