1 // 2 // System.IO.Directory 3 // 4 // Authors: 5 // Ville Palo (vi64pa@kolumbus.fi) 6 // 7 // (C) 2003 Ville Palo 8 // 9 // TODO: Find out why ArgumentOutOfRange tests does not release directories properly 10 // 11 12 using System; 13 using System.Collections.Generic; 14 using System.Diagnostics; 15 using System.Globalization; 16 using System.IO; 17 using System.Text; 18 using System.Threading; 19 20 #if !MOBILE 21 using Mono.Unix; 22 #endif 23 using NUnit.Framework; 24 25 namespace MonoTests.System.IO 26 { 27 28 [TestFixture] 29 public class DirectoryTest 30 { 31 static readonly string TempSubFolder = "MonoTests.System.IO.Tests"; 32 string TempFolder = Path.Combine (Path.GetTempPath (), TempSubFolder); 33 static readonly char DSC = Path.DirectorySeparatorChar; 34 35 [SetUp] SetUp()36 public void SetUp () 37 { 38 if (!Directory.Exists (TempFolder)) 39 Directory.CreateDirectory (TempFolder); 40 41 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US"); 42 } 43 44 [TearDown] TearDown()45 public void TearDown () 46 { 47 if (Directory.Exists (TempFolder)) 48 Directory.Delete (TempFolder, true); 49 } 50 #if !MOBILE 51 [Test] //BXC #12461 EnumerateFilesListSymlinks()52 public void EnumerateFilesListSymlinks () 53 { 54 if (!RunningOnUnix) 55 Assert.Ignore ("Not running on Unix."); 56 57 var afile = Path.Combine (TempFolder, "afile.src"); 58 var bfile = Path.Combine (TempFolder, "bfile.src"); 59 var cdir = Path.Combine (TempFolder, "cdir.src"); 60 61 File.AppendAllText (afile, "hello"); 62 var info = new UnixFileInfo (afile); 63 info.CreateSymbolicLink (bfile); 64 Directory.CreateDirectory (cdir); 65 66 var files0 = Directory.GetFiles (TempFolder, "*.src"); 67 Array.Sort (files0); 68 Assert.AreEqual (2, files0.Length, "#1"); 69 Assert.AreEqual (afile, files0 [0], "#2"); 70 Assert.AreEqual (bfile, files0 [1], "#3"); 71 72 var files1 = new List<string> (Directory.EnumerateFiles (TempFolder, "*.src")).ToArray (); 73 Array.Sort (files1); 74 Assert.AreEqual (2, files1.Length, "#1.b"); 75 Assert.AreEqual (afile, files1 [0], "#2.b"); 76 Assert.AreEqual (bfile, files1 [1], "#3.b"); 77 78 var files2 = Directory.GetFileSystemEntries (TempFolder, "*.src"); 79 Array.Sort (files2); 80 Assert.AreEqual (3, files2.Length, "#1.c"); 81 Assert.AreEqual (afile, files2 [0], "#2.c"); 82 Assert.AreEqual (bfile, files2 [1], "#3.c"); 83 Assert.AreEqual (cdir, files2 [2], "#4.c"); 84 85 var files3 = new List<string> (Directory.EnumerateFileSystemEntries (TempFolder, "*.src")).ToArray (); 86 Array.Sort (files3); 87 Assert.AreEqual (3, files3.Length, "#1.d"); 88 Assert.AreEqual (afile, files3 [0], "#2.d"); 89 Assert.AreEqual (bfile, files3 [1], "#3.d"); 90 Assert.AreEqual (cdir, files3 [2], "#4.d"); 91 } 92 #endif 93 [Test] CreateDirectory()94 public void CreateDirectory () 95 { 96 string path = TempFolder + DSC + "DirectoryTest.Test.1"; 97 DeleteDirectory (path); 98 try { 99 DirectoryInfo info = Directory.CreateDirectory (path); 100 Assert.IsTrue (info.Exists, "#1"); 101 Assert.AreEqual (".1", info.Extension, "#2"); 102 Assert.IsTrue (info.FullName.EndsWith ("DirectoryTest.Test.1"), "#3"); 103 Assert.AreEqual ("DirectoryTest.Test.1", info.Name, "#4"); 104 } finally { 105 DeleteDirectory (path); 106 } 107 } 108 109 /* Commented out: a directory named ":" is legal in unix 110 [Test] 111 public void CreateDirectoryNotSupportedException () 112 { 113 DeleteDirectory (":"); 114 try { 115 DirectoryInfo info = Directory.CreateDirectory (":"); 116 Assert.Fail ("#1"); 117 } catch (ArgumentException ex) { 118 // The path is not of a legal form 119 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 120 Assert.IsNull (ex.InnerException, "#3"); 121 Assert.IsNotNull (ex.Message, "#4"); 122 Assert.IsNull (ex.ParamName, "#5"); 123 } 124 DeleteDirectory (":"); 125 } 126 */ 127 128 [Test] CreateDirectory_Path_Null()129 public void CreateDirectory_Path_Null () 130 { 131 try { 132 Directory.CreateDirectory (null as string); 133 Assert.Fail ("#1"); 134 } catch (ArgumentNullException ex) { 135 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 136 Assert.IsNull (ex.InnerException, "#3"); 137 Assert.IsNotNull (ex.Message, "#4"); 138 Assert.AreEqual ("path", ex.ParamName, "#5"); 139 } 140 } 141 142 [Test] CreateDirectory_Path_Empty()143 public void CreateDirectory_Path_Empty () 144 { 145 try { 146 Directory.CreateDirectory (string.Empty); 147 Assert.Fail ("#1"); 148 } catch (ArgumentException ex) { 149 // Path cannot be the empty string or all whitespace 150 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 151 Assert.IsNull (ex.InnerException, "#3"); 152 Assert.IsNotNull (ex.Message, "#4"); 153 Assert.IsNull (ex.ParamName, "#5"); 154 } 155 } 156 157 [Test] CreateDirectory_Path_Whitespace()158 public void CreateDirectory_Path_Whitespace () 159 { 160 try { 161 Directory.CreateDirectory (" "); 162 Assert.Fail ("#1"); 163 } catch (ArgumentException ex) { 164 // The path is not of a legal form 165 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 166 Assert.IsNull (ex.InnerException, "#3"); 167 Assert.IsNotNull (ex.Message, "#4"); 168 Assert.IsNull (ex.ParamName, "#5"); 169 } 170 } 171 172 [Test] CreateDirectory_Path_InvalidChars()173 public void CreateDirectory_Path_InvalidChars () 174 { 175 string path = TempFolder + DSC + "DirectoryTest.Test"; 176 DeleteDirectory (path); 177 try { 178 path += '\x00'; 179 path += ".2"; 180 DirectoryInfo info = Directory.CreateDirectory (path); 181 Assert.Fail ("#1"); 182 } catch (ArgumentException ex) { 183 // The path contains illegal characters 184 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 185 Assert.IsNull (ex.InnerException, "#3"); 186 Assert.IsNotNull (ex.Message, "#4"); 187 Assert.IsNull (ex.ParamName, "#5"); 188 } finally { 189 DeleteDirectory (path); 190 } 191 } 192 193 [Test] CreateDirectoryAlreadyExists()194 public void CreateDirectoryAlreadyExists () 195 { 196 string path = TempFolder + DSC + "DirectoryTest.Test.Exists"; 197 DeleteDirectory (path); 198 try { 199 DirectoryInfo info1 = Directory.CreateDirectory (path); 200 DirectoryInfo info2 = Directory.CreateDirectory (path); 201 202 Assert.IsTrue (info2.Exists, "#1"); 203 Assert.IsTrue (info2.FullName.EndsWith ("DirectoryTest.Test.Exists"), "#2"); 204 Assert.AreEqual ("DirectoryTest.Test.Exists", info2.Name, "#3"); 205 } finally { 206 DeleteDirectory (path); 207 } 208 } 209 210 [Test] CreateDirectoryAlreadyExistsAsFile()211 public void CreateDirectoryAlreadyExistsAsFile () 212 { 213 string path = TempFolder + DSC + "DirectoryTest.Test.ExistsAsFile"; 214 DeleteDirectory (path); 215 DeleteFile (path); 216 try { 217 FileStream fstream = File.Create (path); 218 fstream.Close(); 219 220 DirectoryInfo dinfo = Directory.CreateDirectory (path); 221 Assert.Fail ("#1"); 222 } catch (IOException ex) { 223 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); 224 // exception message contains the path 225 Assert.IsTrue (ex.Message.Contains (path), "#3"); 226 Assert.IsNull (ex.InnerException, "#4"); 227 } finally { 228 DeleteDirectory (path); 229 DeleteFile (path); 230 } 231 } 232 233 [Test] CreateDirectoryRelativePath()234 public void CreateDirectoryRelativePath () 235 { 236 var path = Path.Combine (TempFolder, "relativepath", "not_this_folder"); 237 path = Path.Combine (path, ".."); 238 239 var res = Directory.CreateDirectory (path); 240 Assert.AreEqual ("relativepath", res.ToString (), "#1"); 241 Assert.IsTrue (Directory.Exists (Path.Combine (TempFolder, "relativepath")), "#2"); 242 } 243 244 [Test] Delete()245 public void Delete () 246 { 247 string path = TempFolder + DSC + "DirectoryTest.Test.Delete.1"; 248 DeleteDirectory (path); 249 try { 250 Directory.CreateDirectory (path); 251 Assert.IsTrue (Directory.Exists (path), "#1"); 252 253 Directory.CreateDirectory (path + DSC + "DirectoryTest.Test.Delete.1.2"); 254 Assert.IsTrue (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#2"); 255 256 Directory.Delete (path + DSC + "DirectoryTest.Test.Delete.1.2"); 257 Assert.IsFalse (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#3"); 258 Assert.IsTrue (Directory.Exists (path), "#4"); 259 260 Directory.Delete (path); 261 Assert.IsFalse (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#5"); 262 Assert.IsFalse (Directory.Exists (path), "#6"); 263 264 Directory.CreateDirectory (path); 265 Directory.CreateDirectory (path + DSC + "DirectoryTest.Test.Delete.1.2"); 266 Assert.IsTrue (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#7"); 267 Assert.IsTrue (Directory.Exists (path), "#8"); 268 269 Directory.Delete (path, true); 270 Assert.IsFalse (Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"), "#9"); 271 Assert.IsFalse (Directory.Exists (path), "#10"); 272 } finally { 273 DeleteDirectory (path); 274 } 275 } 276 277 [Test] 278 [ExpectedException(typeof(ArgumentException))] DeleteArgumentException()279 public void DeleteArgumentException () 280 { 281 Directory.Delete (string.Empty); 282 } 283 284 [Test] 285 [ExpectedException(typeof(ArgumentException))] DeleteArgumentException2()286 public void DeleteArgumentException2 () 287 { 288 Directory.Delete (" "); 289 } 290 291 [Test] 292 [ExpectedException(typeof(ArgumentException))] DeleteArgumentException3()293 public void DeleteArgumentException3 () 294 { 295 string path = TempFolder + DSC + "DirectoryTest.Test.4"; 296 DeleteDirectory (path); 297 298 path += Path.InvalidPathChars [0]; 299 Directory.Delete (path); 300 } 301 302 [Test] 303 [ExpectedException(typeof(ArgumentNullException))] DeleteArgumentNullException()304 public void DeleteArgumentNullException () 305 { 306 Directory.Delete (null as string); 307 } 308 309 [Test] 310 [ExpectedException(typeof(DirectoryNotFoundException))] DeleteDirectoryNotFoundException()311 public void DeleteDirectoryNotFoundException () 312 { 313 string path = TempFolder + DSC + "DirectoryTest.Test.5"; 314 DeleteDirectory (path); 315 316 Directory.Delete (path); 317 } 318 319 [Test] 320 [ExpectedException(typeof(IOException))] DeleteArgumentException4()321 public void DeleteArgumentException4 () 322 { 323 string path = TempFolder + DSC + "DirectoryTest.Test.6"; 324 DeleteDirectory (path); 325 FileStream s = null; 326 Directory.CreateDirectory (path); 327 try { 328 s = File.Create (path + DSC + "DirectoryTest.Test.6"); 329 Directory.Delete (path); 330 } finally { 331 if (s != null) 332 s.Close (); 333 DeleteDirectory (path); 334 }; 335 } 336 337 [Test] DeleteDirectoryOnExistingFileName()338 public void DeleteDirectoryOnExistingFileName () 339 { 340 string path = TempFolder + DSC + "DirectoryTest.Test.ExistsAsFile"; 341 DeleteDirectory (path); 342 DeleteFile (path); 343 try { 344 FileStream fstream = File.Create (path); 345 fstream.Close (); 346 347 Directory.Delete (path); 348 Assert.Fail ("#1"); 349 } 350 catch (IOException ex) { 351 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); 352 // exception message DOES NOT contains the path 353 Assert.IsFalse (ex.Message.IndexOf (path) >= 0, "#3"); 354 Assert.IsNull (ex.InnerException, "#4"); 355 } 356 finally { 357 DeleteDirectory (path); 358 DeleteFile (path); 359 } 360 } 361 362 [Test] Exists()363 public void Exists () 364 { 365 Assert.IsFalse (Directory.Exists (null as string)); 366 } 367 368 #if !MOBILE // We don't support yet the Process class. 369 [Test] // bug #78239 ExistsAccessDenied()370 public void ExistsAccessDenied () 371 { 372 if (!RunningOnUnix) 373 Assert.Ignore ("Not running on Unix."); // this test does not work on Windows. 374 375 string path = TempFolder + DSC + "ExistsAccessDenied"; 376 377 Directory.CreateDirectory (path); 378 global::Mono.Posix.Syscall.chmod (path, 0); 379 try { 380 Assert.IsFalse (Directory.Exists(path + DSC + "b")); 381 } finally { 382 global::Mono.Posix.Syscall.chmod (path, (global::Mono.Posix.FileMode) 755); 383 Directory.Delete (path); 384 } 385 } 386 #endif 387 388 [Test] 389 [ExpectedException(typeof(ArgumentNullException))] GetCreationTimeException1()390 public void GetCreationTimeException1 () 391 { 392 Directory.GetCreationTime (null as string); 393 } 394 395 [Test] 396 [ExpectedException(typeof(ArgumentException))] GetCreationTimeException2()397 public void GetCreationTimeException2 () 398 { 399 Directory.GetCreationTime (string.Empty); 400 } 401 402 [Test] GetCreationTimeException_NonExistingPath()403 public void GetCreationTimeException_NonExistingPath () 404 { 405 string path = TempFolder + DSC + "DirectoryTest.GetCreationTime.1"; 406 DeleteDirectory (path); 407 try { 408 DateTime time = Directory.GetCreationTime (path); 409 410 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime (); 411 Assert.AreEqual (expectedTime.Year, time.Year, "#1"); 412 Assert.AreEqual (expectedTime.Month, time.Month, "#2"); 413 Assert.AreEqual (expectedTime.Day, time.Day, "#3"); 414 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4"); 415 Assert.AreEqual (expectedTime.Second, time.Second, "#5"); 416 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6"); 417 } finally { 418 DeleteDirectory (path); 419 } 420 } 421 422 [Test] 423 [ExpectedException(typeof(ArgumentException))] GetCreationTimeException4()424 public void GetCreationTimeException4 () 425 { 426 Directory.GetCreationTime (" "); 427 } 428 429 [Test] 430 [ExpectedException(typeof(ArgumentException))] GetCreationTimeException5()431 public void GetCreationTimeException5 () 432 { 433 Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ()); 434 } 435 436 [Test] 437 [ExpectedException(typeof(ArgumentNullException))] GetCreationTimeUtcException1()438 public void GetCreationTimeUtcException1 () 439 { 440 Directory.GetCreationTimeUtc (null as string); 441 } 442 443 [Test] 444 [ExpectedException(typeof(ArgumentException))] GetCreationTimeUtcException2()445 public void GetCreationTimeUtcException2 () 446 { 447 Directory.GetCreationTimeUtc (string.Empty); 448 } 449 450 [Test] GetCreationTimeUtc_NonExistingPath()451 public void GetCreationTimeUtc_NonExistingPath () 452 { 453 string path = TempFolder + DSC + "DirectoryTest.GetCreationTimeUtc.1"; 454 DeleteDirectory (path); 455 456 try { 457 DateTime time = Directory.GetCreationTimeUtc (path); 458 459 Assert.AreEqual (1601, time.Year, "#1"); 460 Assert.AreEqual (1, time.Month, "#2"); 461 Assert.AreEqual (1, time.Day, "#3"); 462 Assert.AreEqual (0, time.Hour, "#4"); 463 Assert.AreEqual (0, time.Second, "#5"); 464 Assert.AreEqual (0, time.Millisecond, "#6"); 465 } finally { 466 DeleteDirectory (path); 467 } 468 } 469 470 [Test] 471 [ExpectedException(typeof(ArgumentException))] GetCreationTimeUtcException4()472 public void GetCreationTimeUtcException4 () 473 { 474 Directory.GetCreationTimeUtc (" "); 475 } 476 477 [Test] 478 [ExpectedException(typeof(ArgumentException))] GetCreationTimeUtcException5()479 public void GetCreationTimeUtcException5 () 480 { 481 Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ()); 482 } 483 484 [Test] 485 [ExpectedException(typeof(ArgumentNullException))] GetLastAccessTime_Null()486 public void GetLastAccessTime_Null () 487 { 488 Directory.GetLastAccessTime (null as string); 489 } 490 491 [Test] 492 [ExpectedException(typeof(ArgumentException))] GetLastAccessTimeException2()493 public void GetLastAccessTimeException2 () 494 { 495 Directory.GetLastAccessTime (string.Empty); 496 } 497 498 [Test] GetLastAccessTime_NonExistingPath()499 public void GetLastAccessTime_NonExistingPath () 500 { 501 string path = TempFolder + DSC + "DirectoryTest.GetLastAccessTime.1"; 502 DeleteDirectory (path); 503 504 try { 505 DateTime time = Directory.GetLastAccessTime (path); 506 507 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime (); 508 Assert.AreEqual (expectedTime.Year, time.Year, "#1"); 509 Assert.AreEqual (expectedTime.Month, time.Month, "#2"); 510 Assert.AreEqual (expectedTime.Day, time.Day, "#3"); 511 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4"); 512 Assert.AreEqual (expectedTime.Second, time.Second, "#5"); 513 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6"); 514 } finally { 515 DeleteDirectory (path); 516 } 517 } 518 519 [Test] 520 [ExpectedException(typeof(ArgumentException))] GetLastAccessTimeException4()521 public void GetLastAccessTimeException4 () 522 { 523 Directory.GetLastAccessTime (" "); 524 } 525 526 [Test] 527 [ExpectedException(typeof(ArgumentException))] GetLastAccessTimeException5()528 public void GetLastAccessTimeException5 () 529 { 530 Directory.GetLastAccessTime (Path.InvalidPathChars [0].ToString ()); 531 } 532 533 [Test] 534 [ExpectedException(typeof(ArgumentNullException))] GetLastAccessTimeUtc_Null()535 public void GetLastAccessTimeUtc_Null () 536 { 537 Directory.GetLastAccessTimeUtc (null as string); 538 } 539 540 [Test] 541 [ExpectedException(typeof(ArgumentException))] GetLastAccessTimeUtcException2()542 public void GetLastAccessTimeUtcException2 () 543 { 544 Directory.GetLastAccessTimeUtc (string.Empty); 545 } 546 547 [Test] GetLastAccessTimeUtc_NonExistingPath()548 public void GetLastAccessTimeUtc_NonExistingPath () 549 { 550 string path = TempFolder + DSC + "DirectoryTest.GetLastAccessTimeUtc.1"; 551 DeleteDirectory (path); 552 try { 553 DateTime time = Directory.GetLastAccessTimeUtc (path); 554 555 Assert.AreEqual (1601, time.Year, "#1"); 556 Assert.AreEqual (1, time.Month, "#2"); 557 Assert.AreEqual (1, time.Day, "#3"); 558 Assert.AreEqual (0, time.Hour, "#4"); 559 Assert.AreEqual (0, time.Second, "#5"); 560 Assert.AreEqual (0, time.Millisecond, "#6"); 561 } finally { 562 DeleteDirectory (path); 563 } 564 } 565 566 [Test] 567 [ExpectedException(typeof(ArgumentException))] GetLastAccessTimeUtcException4()568 public void GetLastAccessTimeUtcException4 () 569 { 570 Directory.GetLastAccessTimeUtc (" "); 571 } 572 573 [Test] 574 [ExpectedException(typeof(ArgumentException))] GetLastAccessTimeUtcException5()575 public void GetLastAccessTimeUtcException5 () 576 { 577 Directory.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ()); 578 } 579 580 [Test] 581 [ExpectedException(typeof(ArgumentNullException))] GetLastWriteTimeException1()582 public void GetLastWriteTimeException1 () 583 { 584 Directory.GetLastWriteTime (null as string); 585 } 586 587 [Test] 588 [ExpectedException(typeof(ArgumentException))] GetLastWriteTimeException2()589 public void GetLastWriteTimeException2 () 590 { 591 Directory.GetLastWriteTime (string.Empty); 592 } 593 594 [Test] GetLastWriteTime_NonExistingPath()595 public void GetLastWriteTime_NonExistingPath () 596 { 597 string path = TempFolder + DSC + "DirectoryTest.GetLastWriteTime.1"; 598 DeleteDirectory (path); 599 try { 600 DateTime time = Directory.GetLastWriteTime (path); 601 602 DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime (); 603 Assert.AreEqual (expectedTime.Year, time.Year, "#1"); 604 Assert.AreEqual (expectedTime.Month, time.Month, "#2"); 605 Assert.AreEqual (expectedTime.Day, time.Day, "#3"); 606 Assert.AreEqual (expectedTime.Hour, time.Hour, "#4"); 607 Assert.AreEqual (expectedTime.Second, time.Second, "#5"); 608 Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6"); 609 } finally { 610 DeleteDirectory (path); 611 } 612 } 613 614 [Test] 615 [ExpectedException(typeof(ArgumentException))] GetLastWriteTimeException4()616 public void GetLastWriteTimeException4 () 617 { 618 Directory.GetLastWriteTime (" "); 619 } 620 621 [Test] 622 [ExpectedException(typeof(ArgumentException))] GetLastWriteTimeException5()623 public void GetLastWriteTimeException5 () 624 { 625 Directory.GetLastWriteTime (Path.InvalidPathChars [0].ToString ()); 626 } 627 628 [Test] 629 [ExpectedException(typeof(ArgumentNullException))] GetLastWriteTimeUtcException1()630 public void GetLastWriteTimeUtcException1 () 631 { 632 Directory.GetLastWriteTimeUtc (null as string); 633 } 634 635 [Test] 636 [ExpectedException(typeof(ArgumentException))] GetLastWriteTimeUtcException2()637 public void GetLastWriteTimeUtcException2 () 638 { 639 Directory.GetLastWriteTimeUtc (string.Empty); 640 } 641 642 [Test] GetLastWriteTimeUtc_NonExistingPath()643 public void GetLastWriteTimeUtc_NonExistingPath () 644 { 645 string path = TempFolder + DSC + "DirectoryTest.GetLastWriteTimeUtc.1"; 646 DeleteDirectory (path); 647 try { 648 DateTime time = Directory.GetLastWriteTimeUtc (path); 649 650 Assert.AreEqual (1601, time.Year, "#1"); 651 Assert.AreEqual (1, time.Month, "#2"); 652 Assert.AreEqual (1, time.Day, "#3"); 653 Assert.AreEqual (0, time.Hour, "#4"); 654 Assert.AreEqual (0, time.Second, "#5"); 655 Assert.AreEqual (0, time.Millisecond, "#6"); 656 } finally { 657 DeleteDirectory (path); 658 } 659 660 } 661 662 [Test] 663 [ExpectedException(typeof(ArgumentException))] GetLastWriteTimeUtcException4()664 public void GetLastWriteTimeUtcException4 () 665 { 666 Directory.GetLastWriteTimeUtc (" "); 667 } 668 669 [Test] 670 [ExpectedException(typeof(ArgumentException))] GetLastWriteTimeUtcException5()671 public void GetLastWriteTimeUtcException5 () 672 { 673 Directory.GetLastWriteTimeUtc (Path.InvalidPathChars[0].ToString ()); 674 } 675 676 [Test] Move_DestDirName_Empty()677 public void Move_DestDirName_Empty () 678 { 679 try { 680 Directory.Move (TempFolder, string.Empty); 681 Assert.Fail ("#A1"); 682 } catch (ArgumentException ex) { 683 // Empty file name is not legal 684 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2"); 685 Assert.IsNull (ex.InnerException, "#A3"); 686 Assert.IsNotNull (ex.Message, "#A4"); 687 Assert.IsNotNull (ex.ParamName, "#A5"); 688 Assert.AreEqual ("destDirName", ex.ParamName, "#A6"); 689 } 690 691 try { 692 Directory.Move (TempFolder, " "); 693 Assert.Fail ("#B1"); 694 } catch (ArgumentException ex) { 695 // The path is not of a legal form 696 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2"); 697 Assert.IsNull (ex.InnerException, "#B3"); 698 Assert.IsNotNull (ex.Message, "#B4"); 699 } 700 } 701 702 [Test] Move_DestDirName_Null()703 public void Move_DestDirName_Null () 704 { 705 try { 706 Directory.Move (TempFolder, (string) null); 707 Assert.Fail ("#1"); 708 } catch (ArgumentNullException ex) { 709 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 710 Assert.IsNull (ex.InnerException, "#3"); 711 Assert.IsNotNull (ex.Message, "#4"); 712 Assert.IsNotNull (ex.ParamName, "#5"); 713 Assert.AreEqual ("destDirName", ex.ParamName, "#6"); 714 } 715 } 716 717 [Test] Move_SourceDirName_Empty()718 public void Move_SourceDirName_Empty () 719 { 720 try { 721 Directory.Move (string.Empty, TempFolder); 722 Assert.Fail ("#A1"); 723 } catch (ArgumentException ex) { 724 // Empty file name is not legal 725 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2"); 726 Assert.IsNull (ex.InnerException, "#A3"); 727 Assert.IsNotNull (ex.Message, "#A4"); 728 Assert.IsNotNull (ex.ParamName, "#A5"); 729 Assert.AreEqual ("sourceDirName", ex.ParamName, "#A6"); 730 } 731 732 try { 733 Directory.Move (" ", TempFolder); 734 Assert.Fail ("#B1"); 735 } catch (ArgumentException ex) { 736 // The path is not of a legal form 737 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2"); 738 Assert.IsNull (ex.InnerException, "#B3"); 739 Assert.IsNotNull (ex.Message, "#B4"); 740 } 741 } 742 743 [Test] Move_SourceDirName_Null()744 public void Move_SourceDirName_Null () 745 { 746 try { 747 Directory.Move ((string) null, TempFolder); 748 Assert.Fail ("#1"); 749 } catch (ArgumentNullException ex) { 750 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 751 Assert.IsNull (ex.InnerException, "#3"); 752 Assert.IsNotNull (ex.Message, "#4"); 753 Assert.IsNotNull (ex.ParamName, "#5"); 754 Assert.AreEqual ("sourceDirName", ex.ParamName, "#6"); 755 } 756 } 757 758 [Test] MoveDirectory()759 public void MoveDirectory () 760 { 761 string path = TempFolder + DSC + "DirectoryTest.Test.9"; 762 string path2 = TempFolder + DSC + "DirectoryTest.Test.10"; 763 DeleteDirectory (path); 764 DeleteDirectory (path2); 765 try { 766 Directory.CreateDirectory (path); 767 Directory.CreateDirectory (path + DSC + "dir"); 768 Assert.IsTrue (Directory.Exists (path + DSC + "dir"), "#1"); 769 770 Directory.Move (path, path2); 771 Assert.IsFalse (Directory.Exists (path + DSC + "dir"), "#2"); 772 Assert.IsTrue (Directory.Exists (path2 + DSC + "dir"), "#3"); 773 } finally { 774 DeleteDirectory (path); 775 DeleteDirectory (path2); 776 if (Directory.Exists (path2 + DSC + "dir")) 777 Directory.Delete (path2 + DSC + "dir", true); 778 } 779 } 780 781 [Test] 782 [ExpectedException (typeof (IOException))] MoveDirectory_Same()783 public void MoveDirectory_Same () 784 { 785 string path = TempFolder + DSC + "DirectoryTest.Test.8"; 786 DeleteDirectory (path); 787 try { 788 Directory.Move (path, path); 789 } finally { 790 DeleteDirectory (path); 791 } 792 } 793 794 [Test] MoveFile()795 public void MoveFile () 796 { 797 string tempFile1 = Path.Combine (TempFolder, "temp1.txt"); 798 string tempFile2 = Path.Combine (TempFolder, "temp2.txt"); 799 800 using (StreamWriter sw = File.CreateText (tempFile1)) { 801 sw.Write ("temp1"); 802 } 803 Assert.IsFalse (File.Exists (tempFile2), "#1"); 804 Directory.Move (tempFile1, tempFile2); 805 Assert.IsFalse (File.Exists (tempFile1), "#2"); 806 Assert.IsTrue (File.Exists (tempFile2), "#3"); 807 using (StreamReader sr = File.OpenText (tempFile2)) { 808 Assert.AreEqual ("temp1", sr.ReadToEnd (), "#4"); 809 } 810 } 811 812 [Test] MoveFile_DestDir_Exists()813 public void MoveFile_DestDir_Exists () 814 { 815 string tempFile = Path.Combine (TempFolder, "temp1.txt"); 816 string tempDir = Path.Combine (TempFolder, "temp2"); 817 818 using (StreamWriter sw = File.CreateText (tempFile)) { 819 sw.Write ("temp1"); 820 } 821 Directory.CreateDirectory (tempDir); 822 823 try { 824 Directory.Move (tempFile, tempDir); 825 Assert.Fail ("#A1"); 826 } catch (IOException ex) { 827 // Cannot create a file when that file already exists 828 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2"); 829 Assert.IsNull (ex.InnerException, "#A3"); 830 Assert.IsNotNull (ex.Message, "#A4"); 831 } 832 833 Assert.IsTrue (File.Exists (tempFile), "#B1"); 834 Assert.IsFalse (File.Exists (tempDir), "#B2"); 835 Assert.IsTrue (Directory.Exists (tempDir), "#B3"); 836 } 837 838 [Test] MoveFile_DestFile_Exists()839 public void MoveFile_DestFile_Exists () 840 { 841 string tempFile1 = Path.Combine (TempFolder, "temp1.txt"); 842 string tempFile2 = Path.Combine (TempFolder, "temp2.txt"); 843 844 using (StreamWriter sw = File.CreateText (tempFile1)) { 845 sw.Write ("temp1"); 846 } 847 using (StreamWriter sw = File.CreateText (tempFile2)) { 848 sw.Write ("temp2"); 849 } 850 851 try { 852 Directory.Move (tempFile1, tempFile2); 853 Assert.Fail ("#A1"); 854 } catch (IOException ex) { 855 // Cannot create a file when that file already exists 856 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2"); 857 Assert.IsNull (ex.InnerException, "#A3"); 858 Assert.IsNotNull (ex.Message, "#A4"); 859 } 860 861 Assert.IsTrue (File.Exists (tempFile1), "#B1"); 862 using (StreamReader sr = File.OpenText (tempFile1)) { 863 Assert.AreEqual ("temp1", sr.ReadToEnd (), "#B2"); 864 } 865 866 Assert.IsTrue (File.Exists (tempFile2), "#C1"); 867 using (StreamReader sr = File.OpenText (tempFile2)) { 868 Assert.AreEqual ("temp2", sr.ReadToEnd (), "#C2"); 869 } 870 } 871 872 [Test] MoveFile_Same()873 public void MoveFile_Same () 874 { 875 string tempFile = Path.Combine (TempFolder, "temp.txt"); 876 877 try { 878 Directory.Move (tempFile, tempFile); 879 Assert.Fail ("#1"); 880 } catch (IOException ex) { 881 // Source and destination path must be different 882 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); 883 Assert.IsNull (ex.InnerException, "#3"); 884 Assert.IsNotNull (ex.Message, "#4"); 885 } 886 } 887 888 [Test] 889 [ExpectedException(typeof(ArgumentException))] 890 [Ignore ("On IA64, causes nunit to abort due to bug #76388")] MoveException4()891 public void MoveException4 () 892 { 893 string path = TempFolder + DSC + "DirectoryTest.Test.13"; 894 path += Path.InvalidPathChars [0]; 895 string path2 = TempFolder + DSC + "DirectoryTest.Test.13"; 896 DeleteDirectory (path); 897 DeleteDirectory (path2); 898 try { 899 Directory.CreateDirectory (path2); 900 Directory.Move (path2, path); 901 } finally { 902 DeleteDirectory (path); 903 DeleteDirectory (path2); 904 } 905 } 906 907 [Test] 908 [ExpectedException(typeof(DirectoryNotFoundException))] MoveException5()909 public void MoveException5 () 910 { 911 string path = TempFolder + DSC + "DirectoryTest.Test.14"; 912 DeleteDirectory (path); 913 try { 914 Directory.Move (path, path + "Test.Test"); 915 } finally { 916 DeleteDirectory (path); 917 DeleteDirectory (path + "Test.Test"); 918 } 919 } 920 921 [Test] 922 [ExpectedException(typeof(IOException))] MoveDirectory_Dest_SubDir()923 public void MoveDirectory_Dest_SubDir () 924 { 925 string path = TempFolder + DSC + "DirectoryTest.Test.15"; 926 DeleteDirectory (path); 927 try { 928 Directory.CreateDirectory (path); 929 Directory.Move (path, path + DSC + "dir"); 930 } finally { 931 DeleteDirectory (path); 932 DeleteDirectory (path + DSC + "dir"); 933 } 934 } 935 936 [Test] 937 [ExpectedException (typeof (IOException))] MoveDirectory_Dest_Exists()938 public void MoveDirectory_Dest_Exists () 939 { 940 string path = TempFolder + DSC + "DirectoryTest.Test.16"; 941 string path2 = TempFolder + DSC + "DirectoryTest.Test.17"; 942 943 DeleteDirectory (path); 944 DeleteDirectory (path2); 945 try { 946 Directory.CreateDirectory (path); 947 Directory.CreateDirectory (path2); 948 Directory.Move (path, path2); 949 } finally { 950 DeleteDirectory (path); 951 DeleteDirectory (path2); 952 } 953 } 954 955 [Test] CreationTime()956 public void CreationTime () 957 { 958 if (RunningOnUnix) 959 Assert.Ignore ("Unix doesn't support CreationTime"); 960 961 string path = TempFolder + DSC + "DirectoryTest.CreationTime.1"; 962 DeleteDirectory (path); 963 964 try { 965 Directory.CreateDirectory (path); 966 Directory.SetCreationTime (path, new DateTime (2003, 6, 4, 6, 4, 0)); 967 968 DateTime time = Directory.GetCreationTime (path); 969 Assert.AreEqual (2003, time.Year, "#A1"); 970 Assert.AreEqual (6, time.Month, "#A2"); 971 Assert.AreEqual (4, time.Day, "#A3"); 972 Assert.AreEqual (6, time.Hour, "#A4"); 973 Assert.AreEqual (4, time.Minute, "#A5"); 974 Assert.AreEqual (0, time.Second, "#A6"); 975 976 time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetCreationTimeUtc (path)); 977 Assert.AreEqual (2003, time.Year, "#B1"); 978 Assert.AreEqual (6, time.Month, "#B2"); 979 Assert.AreEqual (4, time.Day, "#B3"); 980 Assert.AreEqual (6, time.Hour, "#B4"); 981 Assert.AreEqual (4, time.Minute, "#B5"); 982 Assert.AreEqual (0, time.Second, "#B6"); 983 984 Directory.SetCreationTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0)); 985 time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetCreationTime (path)); 986 Assert.AreEqual (2003, time.Year, "#C1"); 987 Assert.AreEqual (6, time.Month, "#C2"); 988 Assert.AreEqual (4, time.Day, "#C3"); 989 Assert.AreEqual (6, time.Hour, "#C4"); 990 Assert.AreEqual (4, time.Minute, "#C5"); 991 Assert.AreEqual (0, time.Second, "#C6"); 992 993 time = Directory.GetCreationTimeUtc (path); 994 Assert.AreEqual (2003, time.Year, "#D1"); 995 Assert.AreEqual (6, time.Month, "#D2"); 996 Assert.AreEqual (4, time.Day, "#D3"); 997 Assert.AreEqual (6, time.Hour, "#D4"); 998 Assert.AreEqual (4, time.Minute, "#D5"); 999 Assert.AreEqual (0, time.Second, "#D6"); 1000 } finally { 1001 DeleteDirectory (path); 1002 } 1003 } 1004 1005 [Test] LastAccessTime()1006 public void LastAccessTime () 1007 { 1008 string path = TempFolder + DSC + "DirectoryTest.AccessTime.1"; 1009 DeleteDirectory (path); 1010 1011 try { 1012 Directory.CreateDirectory (path); 1013 Directory.SetLastAccessTime (path, new DateTime (2003, 6, 4, 6, 4, 0)); 1014 1015 DateTime time = Directory.GetLastAccessTime (path); 1016 Assert.AreEqual (2003, time.Year, "#A1"); 1017 Assert.AreEqual (6, time.Month, "#A2"); 1018 Assert.AreEqual (4, time.Day, "#A3"); 1019 Assert.AreEqual (6, time.Hour, "#A4"); 1020 Assert.AreEqual (4, time.Minute, "#A5"); 1021 Assert.AreEqual (0, time.Second, "#A6"); 1022 1023 time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastAccessTimeUtc (path)); 1024 Assert.AreEqual (2003, time.Year, "#B1"); 1025 Assert.AreEqual (6, time.Month, "#B2"); 1026 Assert.AreEqual (4, time.Day, "#B3"); 1027 Assert.AreEqual (6, time.Hour, "#B4"); 1028 Assert.AreEqual (4, time.Minute, "#B5"); 1029 Assert.AreEqual (0, time.Second, "#B6"); 1030 1031 Directory.SetLastAccessTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0)); 1032 time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastAccessTime (path)); 1033 Assert.AreEqual (2003, time.Year, "#C1"); 1034 Assert.AreEqual (6, time.Month, "#C2"); 1035 Assert.AreEqual (4, time.Day, "#C3"); 1036 Assert.AreEqual (6, time.Hour, "#C4"); 1037 Assert.AreEqual (4, time.Minute, "#C5"); 1038 Assert.AreEqual (0, time.Second, "#C6"); 1039 1040 time = Directory.GetLastAccessTimeUtc (path); 1041 Assert.AreEqual (2003, time.Year, "#D1"); 1042 Assert.AreEqual (6, time.Month, "#D2"); 1043 Assert.AreEqual (4, time.Day, "#D3"); 1044 Assert.AreEqual (6, time.Hour, "#D4"); 1045 Assert.AreEqual (4, time.Minute, "#D5"); 1046 Assert.AreEqual (0, time.Second, "#D6"); 1047 } finally { 1048 DeleteDirectory (path); 1049 } 1050 } 1051 1052 [Test] LastWriteTime()1053 public void LastWriteTime () 1054 { 1055 string path = TempFolder + DSC + "DirectoryTest.WriteTime.1"; 1056 DeleteDirectory (path); 1057 1058 try { 1059 Directory.CreateDirectory (path); 1060 Directory.SetLastWriteTime (path, new DateTime (2003, 6, 4, 6, 4, 0)); 1061 1062 DateTime time = Directory.GetLastWriteTime (path); 1063 Assert.AreEqual (2003, time.Year, "#A1"); 1064 Assert.AreEqual (6, time.Month, "#A2"); 1065 Assert.AreEqual (4, time.Day, "#A3"); 1066 Assert.AreEqual (6, time.Hour, "#A4"); 1067 Assert.AreEqual (4, time.Minute, "#A5"); 1068 Assert.AreEqual (0, time.Second, "#A6"); 1069 1070 time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastWriteTimeUtc (path)); 1071 Assert.AreEqual (2003, time.Year, "#B1"); 1072 Assert.AreEqual (6, time.Month, "#B2"); 1073 Assert.AreEqual (4, time.Day, "#B3"); 1074 Assert.AreEqual (6, time.Hour, "#B4"); 1075 Assert.AreEqual (4, time.Minute, "#B5"); 1076 Assert.AreEqual (0, time.Second, "#B6"); 1077 1078 Directory.SetLastWriteTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0)); 1079 time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastWriteTime (path)); 1080 Assert.AreEqual (2003, time.Year, "#C1"); 1081 Assert.AreEqual (6, time.Month, "#C2"); 1082 Assert.AreEqual (4, time.Day, "#C3"); 1083 Assert.AreEqual (6, time.Hour, "#C4"); 1084 Assert.AreEqual (4, time.Minute, "#C5"); 1085 Assert.AreEqual (0, time.Second, "#C6"); 1086 1087 time = Directory.GetLastWriteTimeUtc (path); 1088 Assert.AreEqual (2003, time.Year, "#D1"); 1089 Assert.AreEqual (6, time.Month, "#D2"); 1090 Assert.AreEqual (4, time.Day, "#D3"); 1091 Assert.AreEqual (6, time.Hour, "#D4"); 1092 Assert.AreEqual (4, time.Minute, "#D5"); 1093 Assert.AreEqual (0, time.Second, "#D6"); 1094 } finally { 1095 DeleteDirectory (path); 1096 } 1097 } 1098 1099 [Test] 1100 [ExpectedException(typeof(ArgumentNullException))] SetLastWriteTimeException1()1101 public void SetLastWriteTimeException1 () 1102 { 1103 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1104 Directory.SetLastWriteTime (null as string, time); 1105 } 1106 1107 [Test] 1108 [ExpectedException(typeof(ArgumentException))] SetLastWriteTimeException2()1109 public void SetLastWriteTimeException2 () 1110 { 1111 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1112 Directory.SetLastWriteTime (string.Empty, time); 1113 } 1114 1115 [Test] 1116 [ExpectedException(typeof(FileNotFoundException))] SetLastWriteTimeException3()1117 public void SetLastWriteTimeException3 () 1118 { 1119 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1120 string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTime.2"; 1121 DeleteDirectory (path); 1122 try { 1123 Directory.SetLastWriteTime (path, time); 1124 } finally { 1125 DeleteDirectory (path); 1126 } 1127 } 1128 1129 [Test] 1130 [ExpectedException(typeof(ArgumentException))] SetLastWriteTimeException4()1131 public void SetLastWriteTimeException4 () 1132 { 1133 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1134 Directory.SetLastWriteTime (" ", time); 1135 } 1136 1137 [Test] 1138 [ExpectedException(typeof(ArgumentException))] SetLastWriteTimeException5()1139 public void SetLastWriteTimeException5 () 1140 { 1141 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1142 Directory.SetLastWriteTime (Path.InvalidPathChars [0].ToString (), time); 1143 } 1144 1145 // [Test] 1146 // [ExpectedException(typeof(ArgumentOutOfRangeException))] 1147 // public void SetLastWriteTimeException6 () 1148 // { 1149 // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2); 1150 // string path = TempFolder + Path.DirectorySeparatorChar + "DirectoryTest.SetLastWriteTime.1"; 1151 // 1152 // try { 1153 // if (!Directory.Exists (path)) 1154 // Directory.CreateDirectory (path); 1155 // 1156 // Directory.SetLastWriteTime (path, time); 1157 // } finally { 1158 // DeleteDirectory (path); 1159 // } 1160 // 1161 // } 1162 1163 [Test] 1164 [ExpectedException(typeof(ArgumentNullException))] SetLastWriteTimeUtcException1()1165 public void SetLastWriteTimeUtcException1 () 1166 { 1167 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1168 Directory.SetLastWriteTimeUtc (null as string, time); 1169 } 1170 1171 [Test] 1172 [ExpectedException(typeof(ArgumentException))] SetLastWriteTimeUtcException2()1173 public void SetLastWriteTimeUtcException2 () 1174 { 1175 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1176 Directory.SetLastWriteTimeUtc (string.Empty, time); 1177 } 1178 1179 [Test] 1180 [ExpectedException(typeof(FileNotFoundException))] SetLastWriteTimeUtcException3()1181 public void SetLastWriteTimeUtcException3 () 1182 { 1183 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1184 string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTimeUtc.2"; 1185 DeleteDirectory (path); 1186 try { 1187 Directory.SetLastWriteTimeUtc (path, time); 1188 } finally { 1189 DeleteDirectory (path); 1190 } 1191 } 1192 1193 [Test] 1194 [ExpectedException(typeof(ArgumentException))] SetLastWriteTimeUtcException4()1195 public void SetLastWriteTimeUtcException4 () 1196 { 1197 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1198 Directory.SetLastWriteTimeUtc (" ", time); 1199 } 1200 1201 [Test] 1202 [ExpectedException(typeof(ArgumentException))] SetLastWriteTimeUtcException5()1203 public void SetLastWriteTimeUtcException5 () 1204 { 1205 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1206 Directory.SetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString (), time); 1207 } 1208 1209 // [Test] 1210 // [ExpectedException(typeof(ArgumentOutOfRangeException))] 1211 // public void SetLastWriteTimeUtcException6 () 1212 // { 1213 // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2); 1214 // string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTimeUtc.1"; 1215 // 1216 // if (!Directory.Exists (path)) 1217 // Directory.CreateDirectory (path); 1218 // try { 1219 // Directory.SetLastWriteTimeUtc (path, time); 1220 // } finally { 1221 // DeleteDirectory (path); 1222 // } 1223 // } 1224 1225 [Test] 1226 [ExpectedException(typeof(ArgumentNullException))] SetLastAccessTimeException1()1227 public void SetLastAccessTimeException1 () 1228 { 1229 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1230 Directory.SetLastAccessTime (null as string, time); 1231 } 1232 1233 [Test] 1234 [ExpectedException(typeof(ArgumentException))] SetLastAccessTimeException2()1235 public void SetLastAccessTimeException2 () 1236 { 1237 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1238 Directory.SetLastAccessTime (string.Empty, time); 1239 } 1240 1241 [Test] 1242 [ExpectedException(typeof(FileNotFoundException))] SetLastAccessTimeException3()1243 public void SetLastAccessTimeException3 () 1244 { 1245 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1246 string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTime.2"; 1247 DeleteDirectory (path); 1248 try { 1249 Directory.SetLastAccessTime (path, time); 1250 } finally { 1251 DeleteDirectory (path); 1252 } 1253 } 1254 1255 [Test] 1256 [ExpectedException(typeof(ArgumentException))] SetLastAccessTimeException4()1257 public void SetLastAccessTimeException4 () 1258 { 1259 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1260 Directory.SetLastAccessTime (" ", time); 1261 } 1262 1263 [Test] 1264 [ExpectedException(typeof(ArgumentException))] SetLastAccessTimeException5()1265 public void SetLastAccessTimeException5 () 1266 { 1267 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1268 Directory.SetLastAccessTime (Path.InvalidPathChars [0].ToString (), time); 1269 } 1270 1271 // [Test] 1272 // [ExpectedException(typeof(ArgumentOutOfRangeException))] 1273 // public void SetLastAccessTimeException6 () 1274 // { 1275 // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2); 1276 // string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTime.1"; 1277 // 1278 // if (!Directory.Exists (path)) 1279 // Directory.CreateDirectory (path); 1280 // try { 1281 // Directory.SetLastAccessTime (path, time); 1282 // } finally { 1283 // DeleteDirectory (path); 1284 // } 1285 // 1286 // } 1287 1288 [Test] 1289 [ExpectedException(typeof(ArgumentNullException))] SetLastAccessTimeUtcException1()1290 public void SetLastAccessTimeUtcException1 () 1291 { 1292 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1293 Directory.SetLastAccessTimeUtc (null as string, time); 1294 } 1295 1296 [Test] 1297 [ExpectedException(typeof(ArgumentException))] SetLastAccessTimeUtcException2()1298 public void SetLastAccessTimeUtcException2 () 1299 { 1300 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1301 Directory.SetLastAccessTimeUtc (string.Empty, time); 1302 } 1303 1304 [Test] 1305 [ExpectedException(typeof(FileNotFoundException))] SetLastAccessTimeUtcException3()1306 public void SetLastAccessTimeUtcException3 () 1307 { 1308 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1309 string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.2"; 1310 DeleteDirectory (path); 1311 try { 1312 Directory.SetLastAccessTimeUtc (path, time); 1313 } finally { 1314 DeleteDirectory (path); 1315 } 1316 } 1317 1318 [Test] 1319 [ExpectedException(typeof(ArgumentException))] SetLastAccessTimeUtcException4()1320 public void SetLastAccessTimeUtcException4 () 1321 { 1322 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1323 Directory.SetLastAccessTimeUtc (" ", time); 1324 } 1325 1326 [Test] 1327 [ExpectedException(typeof(ArgumentException))] SetLastAccessTimeUtcException5()1328 public void SetLastAccessTimeUtcException5 () 1329 { 1330 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1331 Directory.SetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString (), time); 1332 } 1333 1334 // [Test] 1335 // [ExpectedException(typeof(ArgumentOutOfRangeException))] 1336 // public void SetLastAccessTimeUtcException6 () 1337 // { 1338 // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2); 1339 // string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.1"; 1340 // 1341 // if (!Directory.Exists (path)) 1342 // Directory.CreateDirectory (path); 1343 // try { 1344 // Directory.SetLastAccessTimeUtc (path, time); 1345 // } finally { 1346 // DeleteDirectory (path); 1347 // } 1348 // } 1349 1350 [Test] 1351 [ExpectedException(typeof(ArgumentNullException))] SetCreationTimeException1()1352 public void SetCreationTimeException1 () 1353 { 1354 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1355 Directory.SetCreationTime (null as string, time); 1356 } 1357 1358 [Test] 1359 [ExpectedException(typeof(ArgumentException))] SetCreationTimeException2()1360 public void SetCreationTimeException2 () 1361 { 1362 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1363 Directory.SetCreationTime (string.Empty, time); 1364 } 1365 1366 [Test] 1367 [ExpectedException(typeof(FileNotFoundException))] SetCreationTimeException3()1368 public void SetCreationTimeException3 () 1369 { 1370 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1371 string path = TempFolder + DSC + "DirectoryTest.SetCreationTime.2"; 1372 DeleteDirectory (path); 1373 1374 try { 1375 Directory.SetCreationTime (path, time); 1376 } finally { 1377 DeleteDirectory (path); 1378 } 1379 } 1380 1381 [Test] 1382 [ExpectedException(typeof(ArgumentException))] SetCreationTimeException4()1383 public void SetCreationTimeException4 () 1384 { 1385 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1386 Directory.SetCreationTime (" ", time); 1387 } 1388 1389 [Test] 1390 [ExpectedException(typeof(ArgumentException))] SetCreationTimeException5()1391 public void SetCreationTimeException5 () 1392 { 1393 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1394 Directory.SetCreationTime (Path.InvalidPathChars [0].ToString (), time); 1395 } 1396 1397 // [Test] 1398 // [ExpectedException(typeof(ArgumentOutOfRangeException))] 1399 // public void SetCreationTimeException6 () 1400 // { 1401 // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2); 1402 // string path = TempFolder + DSC + "DirectoryTest.SetCreationTime.1"; 1403 // 1404 // if (!Directory.Exists (path)) 1405 // Directory.CreateDirectory (path); 1406 // try { 1407 // Directory.SetCreationTime (path, time); 1408 // DeleteDirectory (path); 1409 // } finally { 1410 // DeleteDirectory (path); 1411 // } 1412 // 1413 // } 1414 1415 [Test] 1416 [ExpectedException(typeof(ArgumentNullException))] SetCreationTimeUtcException1()1417 public void SetCreationTimeUtcException1 () 1418 { 1419 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1420 Directory.SetCreationTimeUtc (null as string, time); 1421 } 1422 1423 [Test] 1424 [ExpectedException(typeof(ArgumentException))] SetCreationTimeUtcException2()1425 public void SetCreationTimeUtcException2 () 1426 { 1427 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1428 Directory.SetCreationTimeUtc (string.Empty, time); 1429 } 1430 1431 [Test] 1432 [ExpectedException(typeof(FileNotFoundException))] SetCreationTimeUtcException3()1433 public void SetCreationTimeUtcException3 () 1434 { 1435 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1436 string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.2"; 1437 DeleteDirectory (path); 1438 1439 try { 1440 Directory.SetCreationTimeUtc (path, time); 1441 DeleteDirectory (path); 1442 } finally { 1443 DeleteDirectory (path); 1444 } 1445 } 1446 1447 [Test] 1448 [ExpectedException(typeof(ArgumentException))] SetCreationTimeUtcException4()1449 public void SetCreationTimeUtcException4 () 1450 { 1451 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1452 Directory.SetCreationTimeUtc (" ", time); 1453 } 1454 1455 [Test] 1456 [ExpectedException(typeof(ArgumentException))] SetCreationTimeUtcException5()1457 public void SetCreationTimeUtcException5 () 1458 { 1459 DateTime time = new DateTime (2003, 4, 6, 6, 4, 2); 1460 Directory.SetCreationTimeUtc (Path.InvalidPathChars [0].ToString (), time); 1461 } 1462 1463 // [Test] 1464 // [ExpectedException(typeof(ArgumentOutOfRangeException))] 1465 // public void SetCreationTimeUtcException6 () 1466 // { 1467 // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2); 1468 // string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.1"; 1469 // 1470 // if (!Directory.Exists (path)) 1471 // Directory.CreateDirectory (path); 1472 // try { 1473 // Directory.SetCreationTimeUtc (path, time); 1474 // DeleteDirectory (path); 1475 // } finally { 1476 // DeleteDirectory (path); 1477 // } 1478 // } 1479 1480 [Test] GetDirectories()1481 public void GetDirectories () 1482 { 1483 string path = TempFolder; 1484 string DirPath = TempFolder + Path.DirectorySeparatorChar + ".GetDirectories"; 1485 DeleteDirectory (DirPath); 1486 1487 try { 1488 Directory.CreateDirectory (DirPath); 1489 1490 string [] dirs = Directory.GetDirectories (path); 1491 1492 foreach (string directory in dirs) { 1493 1494 if (directory == DirPath) 1495 return; 1496 } 1497 1498 Assert.Fail ("Directory Not Found"); 1499 } finally { 1500 DeleteDirectory (DirPath); 1501 } 1502 } 1503 1504 [Test] // bug #346123 GetDirectories_Backslash()1505 public void GetDirectories_Backslash () 1506 { 1507 if (!RunningOnUnix) 1508 // on Windows, backslash is used as directory separator 1509 Assert.Ignore ("Not running on Unix."); 1510 1511 string dir = Path.Combine (TempFolder, @"sub\dir"); 1512 Directory.CreateDirectory (dir); 1513 1514 Assert.IsTrue (Directory.Exists (dir), "#A1"); 1515 Assert.IsFalse (Directory.Exists (Path.Combine (TempFolder, "dir")), "#A2"); 1516 1517 string [] dirs = Directory.GetDirectories (TempFolder); 1518 Assert.AreEqual (1, dirs.Length, "#B1"); 1519 Assert.AreEqual (dir, dirs [0], "#B2"); 1520 } 1521 1522 [Test] GetParentOfRootDirectory()1523 public void GetParentOfRootDirectory () 1524 { 1525 DirectoryInfo info; 1526 1527 info = Directory.GetParent (Path.GetPathRoot (Path.GetTempPath ())); 1528 Assert.IsNull (info); 1529 } 1530 1531 [Test] GetDirectoryRoot()1532 public void GetDirectoryRoot () 1533 { 1534 if (RunningOnUnix) 1535 { 1536 string path = "/usr/lib"; 1537 Assert.AreEqual ("/", Directory.GetDirectoryRoot (path)); 1538 } 1539 else 1540 { 1541 Assert.Ignore ("TODO: no proper implementation on Windows."); 1542 string path = "C:\\Windows"; 1543 Assert.AreEqual ("C:\\", Directory.GetDirectoryRoot (path)); 1544 } 1545 } 1546 1547 [Test] GetFiles()1548 public void GetFiles () 1549 { 1550 string path = TempFolder; 1551 string DirPath = TempFolder + Path.DirectorySeparatorChar + ".GetFiles"; 1552 if (File.Exists (DirPath)) 1553 File.Delete (DirPath); 1554 1555 try { 1556 File.Create (DirPath).Close (); 1557 string [] files = Directory.GetFiles (TempFolder); 1558 foreach (string directory in files) { 1559 1560 if (directory == DirPath) 1561 return; 1562 } 1563 1564 Assert.Fail ("File Not Found"); 1565 } finally { 1566 if (File.Exists (DirPath)) 1567 File.Delete (DirPath); 1568 } 1569 } 1570 1571 [Test] // bug #346123 GetFiles_Backslash()1572 public void GetFiles_Backslash () 1573 { 1574 if (!RunningOnUnix) 1575 // on Windows, backslash is used as directory separator 1576 Assert.Ignore ("Not running on Unix."); 1577 1578 string file = Path.Combine (TempFolder, @"doc\temp1.file"); 1579 File.Create (file).Close (); 1580 1581 Assert.IsTrue (File.Exists (file), "#A1"); 1582 Assert.IsFalse (File.Exists (Path.Combine (TempFolder, "temp1.file")), "#A2"); 1583 1584 string [] files = Directory.GetFiles (TempFolder); 1585 Assert.AreEqual (1, files.Length, "#B1"); 1586 Assert.AreEqual (file, files [0], "#B2"); 1587 } 1588 1589 [Test] // bug #82212 and bug #325107 GetFiles_Pattern()1590 public void GetFiles_Pattern () 1591 { 1592 string [] files = Directory.GetFiles (TempFolder, "*.*"); 1593 Assert.IsNotNull (files, "#A1"); 1594 Assert.AreEqual (0, files.Length, "#A2"); 1595 1596 string tempFile1 = Path.Combine (TempFolder, "tempFile1"); 1597 File.Create (tempFile1).Close (); 1598 1599 files = Directory.GetFiles (TempFolder, "*.*"); 1600 Assert.IsNotNull (files, "#B1"); 1601 Assert.AreEqual (1, files.Length, "#B2"); 1602 Assert.AreEqual (tempFile1, files [0], "#B3"); 1603 1604 string tempFile2 = Path.Combine (TempFolder, "FileTemp2.tmp"); 1605 File.Create (tempFile2).Close (); 1606 1607 files = Directory.GetFiles (TempFolder, "*.*"); 1608 Assert.IsNotNull (files, "#C1"); 1609 Assert.AreEqual (2, files.Length, "#C2"); 1610 1611 files = Directory.GetFiles (TempFolder, "temp*.*"); 1612 Assert.IsNotNull (files, "#D1"); 1613 Assert.AreEqual (1, files.Length, "#D2"); 1614 Assert.AreEqual (tempFile1, files [0], "#D3"); 1615 1616 string tempFile3 = Path.Combine (TempFolder, "tempFile3.txt"); 1617 File.Create (tempFile3).Close (); 1618 1619 files = Directory.GetFiles (TempFolder, "*File*.*"); 1620 Assert.IsNotNull (files, "#E1"); 1621 Assert.AreEqual (3, files.Length, "#E2"); 1622 1623 files = Directory.GetFiles (TempFolder, "*File*.tmp"); 1624 Assert.IsNotNull (files, "#F1"); 1625 Assert.AreEqual (1, files.Length, "#F2"); 1626 Assert.AreEqual (tempFile2, files [0], "#F3"); 1627 1628 files = Directory.GetFiles (TempFolder, "*tempFile*"); 1629 Assert.IsNotNull (files, "#G1"); 1630 Assert.AreEqual (2, files.Length, "#G2"); 1631 1632 files = Directory.GetFiles (TempFolder, "*tempFile1"); 1633 Assert.IsNotNull (files, "#H1"); 1634 Assert.AreEqual (1, files.Length, "#H2"); 1635 Assert.AreEqual (tempFile1, files [0], "#H3"); 1636 1637 files = Directory.GetFiles (TempFolder, "*.txt"); 1638 Assert.IsNotNull (files, "#I1"); 1639 Assert.AreEqual (1, files.Length, "#I2"); 1640 Assert.AreEqual (tempFile3, files [0], "#I3"); 1641 1642 files = Directory.GetFiles (TempFolder, "*.t*"); 1643 Assert.IsNotNull (files, "#J1"); 1644 Assert.AreEqual (2, files.Length, "#J2"); 1645 1646 files = Directory.GetFiles (TempFolder, "temp*.*"); 1647 Assert.IsNotNull (files, "#K1"); 1648 Assert.AreEqual (2, files.Length, "#K2"); 1649 1650 File.Delete (tempFile1); 1651 1652 files = Directory.GetFiles (TempFolder, "temp*.*"); 1653 Assert.IsNotNull (files, "#L1"); 1654 Assert.AreEqual (1, files.Length, "#L2"); 1655 Assert.AreEqual (tempFile3, files [0], "#L3"); 1656 1657 files = Directory.GetFiles (TempFolder, ".*"); 1658 Assert.IsNotNull (files, "#M1"); 1659 Assert.AreEqual (0, files.Length, "#M2"); 1660 1661 string tempFile4 = Path.Combine (TempFolder, "tempFile4."); 1662 File.Create (tempFile4).Close (); 1663 1664 files = Directory.GetFiles (TempFolder, "temp*."); 1665 Assert.IsNotNull (files, "#N1"); 1666 Assert.AreEqual (1, files.Length, "#N2"); 1667 if (RunningOnUnix) 1668 Assert.AreEqual (tempFile4, files [0], "#N3"); 1669 else // on Windows, the trailing dot is automatically trimmed 1670 Assert.AreEqual (Path.Combine (TempFolder, "tempFile4"), files [0], "#N3"); 1671 } 1672 1673 [Test] GetFiles_580090()1674 public void GetFiles_580090 () 1675 { 1676 string cwd = Directory.GetCurrentDirectory (); 1677 Directory.SetCurrentDirectory (Path.GetTempPath ()); 1678 1679 string tempFile = Path.Combine (TempFolder, "tempFile.txt"); 1680 File.Create (tempFile).Close (); 1681 1682 try { 1683 string [] files = Directory.GetFiles (".", TempSubFolder + DSC + "*.t*"); 1684 Assert.IsNotNull (files, "#J1"); 1685 Assert.AreEqual (1, files.Length, "#J2"); 1686 } 1687 finally { 1688 Directory.SetCurrentDirectory (cwd); 1689 } 1690 } 1691 1692 1693 [Test] GetFiles_SubDirInPattern()1694 public void GetFiles_SubDirInPattern () 1695 { 1696 string DirPath = TempFolder + Path.DirectorySeparatorChar + "GetFiles_SubDirInPattern"; 1697 if (Directory.Exists (DirPath)) 1698 Directory.Delete (DirPath, true); 1699 1700 Directory.CreateDirectory ($"{DirPath}{Path.DirectorySeparatorChar}something{Path.DirectorySeparatorChar}else"); 1701 File.WriteAllText($"{DirPath}{Path.DirectorySeparatorChar}something{Path.DirectorySeparatorChar}else{Path.DirectorySeparatorChar}file", "hello"); 1702 1703 var r = Directory.GetFiles (DirPath, $"something{Path.DirectorySeparatorChar}else{Path.DirectorySeparatorChar}*", SearchOption.AllDirectories); 1704 Assert.AreEqual (new string[] { Path.Combine (DirPath, "something", "else", "file") }, r); 1705 } 1706 1707 [Test] 1708 [ExpectedException (typeof (ArgumentNullException))] SetCurrentDirectoryNull()1709 public void SetCurrentDirectoryNull () 1710 { 1711 Directory.SetCurrentDirectory (null); 1712 } 1713 1714 [Test] 1715 [ExpectedException (typeof (ArgumentException))] SetCurrentDirectoryEmpty()1716 public void SetCurrentDirectoryEmpty () 1717 { 1718 Directory.SetCurrentDirectory (String.Empty); 1719 } 1720 1721 [Test] 1722 [ExpectedException (typeof (ArgumentException))] SetCurrentDirectoryWhitespace()1723 public void SetCurrentDirectoryWhitespace () 1724 { 1725 Directory.SetCurrentDirectory (" "); 1726 } 1727 1728 1729 [Test] GetNoFiles()1730 public void GetNoFiles () // Bug 58875. This throwed an exception on windows. 1731 { 1732 DirectoryInfo dir = new DirectoryInfo ("."); 1733 dir.GetFiles ("*.nonext"); 1734 } 1735 1736 [Test] FilenameOnly()1737 public void FilenameOnly () // bug 78209 1738 { 1739 Directory.GetParent ("somefile"); 1740 } 1741 1742 private static bool RunningOnUnix { 1743 get { 1744 // check for Unix platforms - see FAQ for more details 1745 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F 1746 int platform = (int) Environment.OSVersion.Platform; 1747 return ((platform == 4) || (platform == 128) || (platform == 6)); 1748 } 1749 } 1750 1751 #if !MOBILE 1752 [Test] ResolvePathBeforeDirectoryExists()1753 public void ResolvePathBeforeDirectoryExists () 1754 { 1755 if (!RunningOnUnix) 1756 Assert.Ignore ("Not running on Unix."); 1757 1758 string cwd = Directory.GetCurrentDirectory (); 1759 1760 string root = Path.Combine (TempFolder, "test_ResolvePathBeforeExists"); 1761 string testPath = Path.Combine (root, "test"); 1762 string test2Path = Path.Combine (testPath, "test2"); 1763 string testFile = Path.Combine (test2Path, "test_file"); 1764 string symlinkPath = Path.Combine (root, "test3"); 1765 try 1766 { 1767 Directory.CreateDirectory (root); 1768 Directory.SetCurrentDirectory (root); 1769 Directory.CreateDirectory (testPath); 1770 Directory.CreateDirectory (test2Path); 1771 File.WriteAllText (testFile, "hello"); 1772 1773 var info = new UnixFileInfo (test2Path); 1774 info.CreateSymbolicLink (symlinkPath); 1775 1776 var partial_path_with_symlink = "test3/../test3"; // test3 is a symlink to test/test2 1777 1778 Assert.AreEqual (Directory.Exists (partial_path_with_symlink), true); 1779 } 1780 finally 1781 { 1782 DeleteFile (symlinkPath); 1783 DeleteFile (testFile); 1784 DeleteDirectory (test2Path); 1785 DeleteDirectory (testPath); 1786 Directory.SetCurrentDirectory (cwd); 1787 DeleteDirectory (root); 1788 } 1789 } 1790 #endif 1791 DeleteDirectory(string path)1792 private void DeleteDirectory (string path) 1793 { 1794 if (Directory.Exists (path)) 1795 Directory.Delete (path, true); 1796 } 1797 DeleteFile(string path)1798 private void DeleteFile (string path) 1799 { 1800 if (File.Exists (path)) 1801 File.Delete (path); 1802 } 1803 } 1804 } 1805