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