DirectoryTest.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. //
  2. // System.IO.Directory
  3. //
  4. // Authors:
  5. // Ville Palo ([email protected])
  6. //
  7. // (C) 2003 Ville Palo
  8. //
  9. // TODO: Find out why ArgumentOutOfRange tests does not release directories properly
  10. //
  11. using NUnit.Framework;
  12. using System.IO;
  13. using System.Text;
  14. using System;
  15. using System.Globalization;
  16. using System.Threading;
  17. namespace MonoTests.System.IO {
  18. [TestFixture]
  19. public class DirectoryTest : Assertion {
  20. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  21. static readonly char DSC = Path.DirectorySeparatorChar;
  22. [SetUp]
  23. public void SetUp ()
  24. {
  25. if (!Directory.Exists (TempFolder))
  26. Directory.CreateDirectory (TempFolder);
  27. Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
  28. }
  29. [TearDown]
  30. public void TearDown () {
  31. if (Directory.Exists (TempFolder))
  32. Directory.Delete (TempFolder, true);
  33. }
  34. [Test]
  35. public void CreateDirectory ()
  36. {
  37. string path = TempFolder + DSC + "DirectoryTest.Test.1";
  38. DeleteDirectory (path);
  39. try {
  40. DirectoryInfo info = Directory.CreateDirectory (path);
  41. AssertEquals ("test#01", true, info.Exists);
  42. AssertEquals ("test#02", ".1", info.Extension);
  43. AssertEquals ("test#03", true, info.FullName.EndsWith ("DirectoryTest.Test.1"));
  44. AssertEquals ("test#04", "DirectoryTest.Test.1", info.Name);
  45. } finally {
  46. DeleteDirectory (path);
  47. }
  48. }
  49. [Test]
  50. [ExpectedException(typeof(ArgumentException))]
  51. public void CreateDirectoryNotSupportedException ()
  52. {
  53. DeleteDirectory (":");
  54. DirectoryInfo info = Directory.CreateDirectory (":");
  55. DeleteDirectory (":");
  56. }
  57. [Test]
  58. [ExpectedException(typeof(ArgumentNullException))]
  59. public void CreateDirectoryArgumentNullException ()
  60. {
  61. DirectoryInfo info = Directory.CreateDirectory (null as string);
  62. }
  63. [Test]
  64. [ExpectedException(typeof(ArgumentException))]
  65. public void CreateDirectoryArgumentException1 ()
  66. {
  67. DirectoryInfo info = Directory.CreateDirectory ("");
  68. }
  69. [Test]
  70. [ExpectedException(typeof(ArgumentException))]
  71. public void CreateDirectoryArgumentException2 ()
  72. {
  73. DirectoryInfo info = Directory.CreateDirectory (" ");
  74. }
  75. [Test]
  76. [ExpectedException(typeof(ArgumentException))]
  77. public void CreateDirectoryArgumentException3 ()
  78. {
  79. string path = TempFolder + DSC + "DirectoryTest.Test";
  80. DeleteDirectory (path);
  81. try {
  82. path += Path.InvalidPathChars [0];
  83. path += ".2";
  84. DirectoryInfo info = Directory.CreateDirectory (path);
  85. } finally {
  86. DeleteDirectory (path);
  87. }
  88. }
  89. [Test]
  90. public void Delete ()
  91. {
  92. string path = TempFolder + DSC + "DirectoryTest.Test.Delete.1";
  93. DeleteDirectory (path);
  94. try {
  95. Directory.CreateDirectory (path);
  96. AssertEquals ("test#01", true, Directory.Exists (path));
  97. Directory.CreateDirectory (path + DSC + "DirectoryTest.Test.Delete.1.2");
  98. AssertEquals ("test#02", true, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"));
  99. Directory.Delete (path + DSC + "DirectoryTest.Test.Delete.1.2");
  100. AssertEquals ("test#03", false, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"));
  101. AssertEquals ("test#04", true, Directory.Exists (path));
  102. Directory.Delete (path);
  103. AssertEquals ("test#05", false, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"));
  104. AssertEquals ("test#06", false, Directory.Exists (path));
  105. Directory.CreateDirectory (path);
  106. Directory.CreateDirectory (path + DSC + "DirectoryTest.Test.Delete.1.2");
  107. AssertEquals ("test#07", true, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"));
  108. AssertEquals ("test#08", true, Directory.Exists (path));
  109. Directory.Delete (path, true);
  110. AssertEquals ("test#09", false, Directory.Exists (path + DSC + "DirectoryTest.Test.Delete.1.2"));
  111. AssertEquals ("test#10", false, Directory.Exists (path));
  112. } finally {
  113. DeleteDirectory (path);
  114. }
  115. }
  116. [Test]
  117. [ExpectedException(typeof(ArgumentException))]
  118. public void DeleteArgumentException ()
  119. {
  120. Directory.Delete ("");
  121. }
  122. [Test]
  123. [ExpectedException(typeof(ArgumentException))]
  124. public void DeleteArgumentException2 ()
  125. {
  126. Directory.Delete (" ");
  127. }
  128. [Test]
  129. [ExpectedException(typeof(ArgumentException))]
  130. public void DeleteArgumentException3 ()
  131. {
  132. string path = TempFolder + DSC + "DirectoryTest.Test.4";
  133. DeleteDirectory (path);
  134. path += Path.InvalidPathChars [0];
  135. Directory.Delete (path);
  136. }
  137. [Test]
  138. [ExpectedException(typeof(ArgumentNullException))]
  139. public void DeleteArgumentNullException ()
  140. {
  141. Directory.Delete (null as string);
  142. }
  143. [Test]
  144. [ExpectedException(typeof(DirectoryNotFoundException))]
  145. public void DeleteDirectoryNotFoundException ()
  146. {
  147. string path = TempFolder + DSC + "DirectoryTest.Test.5";
  148. DeleteDirectory (path);
  149. Directory.Delete (path);
  150. }
  151. [Test]
  152. [ExpectedException(typeof(IOException))]
  153. public void DeleteArgumentException4 ()
  154. {
  155. string path = TempFolder + DSC + "DirectoryTest.Test.6";
  156. DeleteDirectory (path);
  157. FileStream s = null;
  158. Directory.CreateDirectory (path);
  159. try {
  160. s = File.Create (path + DSC + "DirectoryTest.Test.6");
  161. Directory.Delete (path);
  162. } finally {
  163. if (s != null)
  164. s.Close ();
  165. DeleteDirectory (path);
  166. };
  167. }
  168. [Test]
  169. public void Exists ()
  170. {
  171. AssertEquals ("test#01", false, Directory.Exists (null as string));
  172. }
  173. [Test]
  174. [ExpectedException(typeof(ArgumentNullException))]
  175. public void GetCreationTimeException1 ()
  176. {
  177. Directory.GetCreationTime (null as string);
  178. }
  179. [Test]
  180. [ExpectedException(typeof(ArgumentException))]
  181. public void GetCreationTimeException2 ()
  182. {
  183. Directory.GetCreationTime ("");
  184. }
  185. [Test]
  186. [ExpectedException(typeof(IOException))]
  187. public void GetCreationTimeException3 ()
  188. {
  189. string path = TempFolder + DSC + "DirectoryTest.GetCreationTime.1";
  190. DeleteDirectory (path);
  191. try {
  192. Directory.GetCreationTime (path);
  193. } finally {
  194. DeleteDirectory (path);
  195. }
  196. }
  197. [Test]
  198. [ExpectedException(typeof(ArgumentException))]
  199. public void GetCreationTimeException4 ()
  200. {
  201. Directory.GetCreationTime (" ");
  202. }
  203. [Test]
  204. [ExpectedException(typeof(ArgumentException))]
  205. public void GetCreationTimeException5 ()
  206. {
  207. Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  208. }
  209. [Test]
  210. [ExpectedException(typeof(ArgumentNullException))]
  211. public void GetCreationTimeUtcException1 ()
  212. {
  213. Directory.GetCreationTimeUtc (null as string);
  214. }
  215. [Test]
  216. [ExpectedException(typeof(ArgumentException))]
  217. public void GetCreationTimeUtcException2 ()
  218. {
  219. Directory.GetCreationTimeUtc ("");
  220. }
  221. [Test]
  222. [ExpectedException(typeof(IOException))]
  223. public void GetCreationTimeUtcException3 ()
  224. {
  225. string path = TempFolder + DSC + "DirectoryTest.GetCreationTimeUtc.1";
  226. DeleteDirectory (path);
  227. try {
  228. Directory.GetCreationTimeUtc (path);
  229. } finally {
  230. DeleteDirectory (path);
  231. }
  232. }
  233. [Test]
  234. [ExpectedException(typeof(ArgumentException))]
  235. public void GetCreationTimeUtcException4 ()
  236. {
  237. Directory.GetCreationTimeUtc (" ");
  238. }
  239. [Test]
  240. [ExpectedException(typeof(ArgumentException))]
  241. public void GetCreationTimeUtcException5 ()
  242. {
  243. Directory.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  244. }
  245. [Test]
  246. [ExpectedException(typeof(ArgumentNullException))]
  247. public void GetLastAccessTimeException1 ()
  248. {
  249. Directory.GetLastAccessTime (null as string);
  250. }
  251. [Test]
  252. [ExpectedException(typeof(ArgumentException))]
  253. public void GetLastAccessTimeException2 ()
  254. {
  255. Directory.GetLastAccessTime ("");
  256. }
  257. [Test]
  258. [ExpectedException(typeof(IOException))]
  259. public void GetLastAccessTimeException3 ()
  260. {
  261. string path = TempFolder + DSC + "DirectoryTest.GetLastAccessTime.1";
  262. DeleteDirectory (path);
  263. try {
  264. Directory.GetLastAccessTime (path);
  265. } finally {
  266. DeleteDirectory (path);
  267. }
  268. }
  269. [Test]
  270. [ExpectedException(typeof(ArgumentException))]
  271. public void GetLastAccessTimeException4 ()
  272. {
  273. Directory.GetLastAccessTime (" ");
  274. }
  275. [Test]
  276. [ExpectedException(typeof(ArgumentException))]
  277. public void GetLastAccessTimeException5 ()
  278. {
  279. Directory.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
  280. }
  281. [Test]
  282. [ExpectedException(typeof(ArgumentNullException))]
  283. public void GetLastAccessTimeUtcException1 ()
  284. {
  285. Directory.GetLastAccessTimeUtc (null as string);
  286. }
  287. [Test]
  288. [ExpectedException(typeof(ArgumentException))]
  289. public void GetLastAccessTimeUtcException2 ()
  290. {
  291. Directory.GetLastAccessTimeUtc ("");
  292. }
  293. [Test]
  294. [ExpectedException(typeof(IOException))]
  295. public void GetLastAccessTimeUtcException3 ()
  296. {
  297. string path = TempFolder + DSC + "DirectoryTest.GetLastAccessTimeUtc.1";
  298. DeleteDirectory (path);
  299. try {
  300. Directory.GetLastAccessTimeUtc (path);
  301. } finally {
  302. DeleteDirectory (path);
  303. }
  304. }
  305. [Test]
  306. [ExpectedException(typeof(ArgumentException))]
  307. public void GetLastAccessTimeUtcException4 ()
  308. {
  309. Directory.GetLastAccessTimeUtc (" ");
  310. }
  311. [Test]
  312. [ExpectedException(typeof(ArgumentException))]
  313. public void GetLastAccessTimeUtcException5 ()
  314. {
  315. Directory.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  316. }
  317. [Test]
  318. [ExpectedException(typeof(ArgumentNullException))]
  319. public void GetLastWriteTimeException1 ()
  320. {
  321. Directory.GetLastWriteTime (null as string);
  322. }
  323. [Test]
  324. [ExpectedException(typeof(ArgumentException))]
  325. public void GetLastWriteTimeException2 ()
  326. {
  327. Directory.GetLastWriteTime ("");
  328. }
  329. [Test]
  330. [ExpectedException(typeof(IOException))]
  331. public void GetLastWriteTimeException3 ()
  332. {
  333. string path = TempFolder + DSC + "DirectoryTest.GetLastWriteTime.1";
  334. DeleteDirectory (path);
  335. try {
  336. Directory.GetLastWriteTime (path);
  337. } finally {
  338. DeleteDirectory (path);
  339. }
  340. }
  341. [Test]
  342. [ExpectedException(typeof(ArgumentException))]
  343. public void GetLastWriteTimeException4 ()
  344. {
  345. Directory.GetLastWriteTime (" ");
  346. }
  347. [Test]
  348. [ExpectedException(typeof(ArgumentException))]
  349. public void GetLastWriteTimeException5 ()
  350. {
  351. Directory.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
  352. }
  353. [Test]
  354. [ExpectedException(typeof(ArgumentNullException))]
  355. public void GetLastWriteTimeUtcException1 ()
  356. {
  357. Directory.GetLastWriteTimeUtc (null as string);
  358. }
  359. [Test]
  360. [ExpectedException(typeof(ArgumentException))]
  361. public void GetLastWriteTimeUtcException2 ()
  362. {
  363. Directory.GetLastAccessTimeUtc ("");
  364. }
  365. [Test]
  366. [ExpectedException(typeof(IOException))]
  367. public void GetLastWriteTimeUtcException3 ()
  368. {
  369. string path = TempFolder + DSC + "DirectoryTest.GetLastWriteTimeUtc.1";
  370. DeleteDirectory (path);
  371. try {
  372. Directory.GetLastAccessTimeUtc (path);
  373. } finally {
  374. DeleteDirectory (path);
  375. }
  376. }
  377. [Test]
  378. [ExpectedException(typeof(ArgumentException))]
  379. public void GetLastWriteTimeUtcException4 ()
  380. {
  381. Directory.GetLastAccessTimeUtc (" ");
  382. }
  383. [Test]
  384. [ExpectedException(typeof(ArgumentException))]
  385. public void GetLastWriteTimeUtcException5 ()
  386. {
  387. Directory.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  388. }
  389. [Test]
  390. public void Move ()
  391. {
  392. string path = TempFolder + DSC + "DirectoryTest.Test.9";
  393. string path2 = TempFolder + DSC + "DirectoryTest.Test.10";
  394. DeleteDirectory (path);
  395. DeleteDirectory (path2);
  396. try {
  397. Directory.CreateDirectory (path);
  398. Directory.CreateDirectory (path + DSC + "dir");
  399. AssertEquals ("test#01", true, Directory.Exists (path + DSC + "dir"));
  400. Directory.Move (path, path2);
  401. AssertEquals ("test#02", false, Directory.Exists (path + DSC + "dir"));
  402. AssertEquals ("test#03", true, Directory.Exists (path2 + DSC + "dir"));
  403. } finally {
  404. DeleteDirectory (path);
  405. DeleteDirectory (path2);
  406. if (Directory.Exists (path2 + DSC + "dir"))
  407. Directory.Delete (path2 + DSC + "dir", true);
  408. }
  409. }
  410. [Test]
  411. [ExpectedException(typeof(IOException))]
  412. public void MoveException1 ()
  413. {
  414. string path = TempFolder + DSC + "DirectoryTest.Test.8";
  415. DeleteDirectory (path);
  416. try {
  417. Directory.Move (path, path);
  418. } finally {
  419. DeleteDirectory (path);
  420. }
  421. }
  422. [Test]
  423. [ExpectedException(typeof(ArgumentException))]
  424. public void MoveException2 ()
  425. {
  426. string path = TempFolder + DSC + "DirectoryTest.Test.11";
  427. DeleteDirectory (path);
  428. try {
  429. Directory.Move ("", path);
  430. } finally {
  431. DeleteDirectory (path);
  432. }
  433. }
  434. [Test]
  435. [ExpectedException(typeof(ArgumentException))]
  436. public void MoveException3 ()
  437. {
  438. string path = TempFolder + DSC + "DirectoryTest.Test.12";
  439. DeleteDirectory (path);
  440. try {
  441. Directory.Move (" ", path);
  442. } finally {
  443. DeleteDirectory (path);
  444. }
  445. }
  446. [Test]
  447. [ExpectedException(typeof(ArgumentException))]
  448. public void MoveException4 ()
  449. {
  450. string path = TempFolder + DSC + "DirectoryTest.Test.13";
  451. path += Path.InvalidPathChars [0];
  452. string path2 = TempFolder + DSC + "DirectoryTest.Test.13";
  453. DeleteDirectory (path);
  454. DeleteDirectory (path2);
  455. try {
  456. Directory.CreateDirectory (path2);
  457. Directory.Move (path2, path);
  458. } finally {
  459. DeleteDirectory (path);
  460. DeleteDirectory (path2);
  461. }
  462. }
  463. [Test]
  464. [ExpectedException(typeof(DirectoryNotFoundException))]
  465. public void MoveException5 ()
  466. {
  467. string path = TempFolder + DSC + "DirectoryTest.Test.14";
  468. DeleteDirectory (path);
  469. try {
  470. Directory.Move (path, path + "Test.Test");
  471. } finally {
  472. DeleteDirectory (path);
  473. DeleteDirectory (path + "Test.Test");
  474. }
  475. }
  476. [Test]
  477. [ExpectedException(typeof(IOException))]
  478. public void MoveException6 ()
  479. {
  480. string path = TempFolder + DSC + "DirectoryTest.Test.15";
  481. DeleteDirectory (path);
  482. try {
  483. Directory.CreateDirectory (path);
  484. Directory.Move (path, path + DSC + "dir");
  485. } finally {
  486. DeleteDirectory (path);
  487. DeleteDirectory (path + DSC + "dir");
  488. }
  489. }
  490. [Test]
  491. [ExpectedException(typeof(IOException))]
  492. public void MoveException7 ()
  493. {
  494. string path = TempFolder + DSC + "DirectoryTest.Test.16";
  495. string path2 = TempFolder + DSC + "DirectoryTest.Test.17";
  496. DeleteDirectory (path);
  497. DeleteDirectory (path2);
  498. try {
  499. Directory.CreateDirectory (path);
  500. Directory.CreateDirectory (path2);
  501. Directory.Move (path, path2);
  502. } finally {
  503. DeleteDirectory (path);
  504. DeleteDirectory (path2);
  505. }
  506. }
  507. [Test]
  508. [Ignore("Unix doesnt support CreationTime")]
  509. public void CreationTime ()
  510. {
  511. string path = TempFolder + DSC + "DirectoryTest.CreationTime.1";
  512. DeleteDirectory (path);
  513. try {
  514. Directory.CreateDirectory (path);
  515. Directory.SetCreationTime (path, new DateTime (2003, 6, 4, 6, 4, 0));
  516. DateTime time = Directory.GetCreationTime (path);
  517. AssertEquals ("test#01", 2003, time.Year);
  518. AssertEquals ("test#02", 6, time.Month);
  519. AssertEquals ("test#03", 4, time.Day);
  520. AssertEquals ("test#04", 6, time.Hour);
  521. AssertEquals ("test#05", 4, time.Minute);
  522. AssertEquals ("test#06", 0, time.Second);
  523. time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetCreationTimeUtc (path));
  524. AssertEquals ("test#07", 2003, time.Year);
  525. AssertEquals ("test#08", 6, time.Month);
  526. AssertEquals ("test#09", 4, time.Day);
  527. AssertEquals ("test#10", 6, time.Hour);
  528. AssertEquals ("test#11", 4, time.Minute);
  529. AssertEquals ("test#12", 0, time.Second);
  530. Directory.SetCreationTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0));
  531. time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetCreationTime (path));
  532. AssertEquals ("test#13", 2003, time.Year);
  533. AssertEquals ("test#14", 6, time.Month);
  534. AssertEquals ("test#15", 4, time.Day);
  535. AssertEquals ("test#16", 6, time.Hour);
  536. AssertEquals ("test#17", 4, time.Minute);
  537. AssertEquals ("test#18", 0, time.Second);
  538. time = Directory.GetCreationTimeUtc (path);
  539. AssertEquals ("test#19", 2003, time.Year);
  540. AssertEquals ("test#20", 6, time.Month);
  541. AssertEquals ("test#21", 4, time.Day);
  542. AssertEquals ("test#22", 6, time.Hour);
  543. AssertEquals ("test#23", 4, time.Minute);
  544. AssertEquals ("test#24", 0, time.Second);
  545. } finally {
  546. DeleteDirectory (path);
  547. }
  548. }
  549. [Test]
  550. public void LastAccessTime ()
  551. {
  552. string path = TempFolder + DSC + "DirectoryTest.AccessTime.1";
  553. DeleteDirectory (path);
  554. try {
  555. Directory.CreateDirectory (path);
  556. Directory.SetLastAccessTime (path, new DateTime (2003, 6, 4, 6, 4, 0));
  557. DateTime time = Directory.GetLastAccessTime (path);
  558. AssertEquals ("test#01", 2003, time.Year);
  559. AssertEquals ("test#02", 6, time.Month);
  560. AssertEquals ("test#03", 4, time.Day);
  561. AssertEquals ("test#04", 6, time.Hour);
  562. AssertEquals ("test#05", 4, time.Minute);
  563. AssertEquals ("test#06", 0, time.Second);
  564. time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastAccessTimeUtc (path));
  565. AssertEquals ("test#07", 2003, time.Year);
  566. AssertEquals ("test#08", 6, time.Month);
  567. AssertEquals ("test#09", 4, time.Day);
  568. AssertEquals ("test#10", 6, time.Hour);
  569. AssertEquals ("test#11", 4, time.Minute);
  570. AssertEquals ("test#12", 0, time.Second);
  571. Directory.SetLastAccessTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0));
  572. time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastAccessTime (path));
  573. AssertEquals ("test#13", 2003, time.Year);
  574. AssertEquals ("test#14", 6, time.Month);
  575. AssertEquals ("test#15", 4, time.Day);
  576. AssertEquals ("test#16", 6, time.Hour);
  577. AssertEquals ("test#17", 4, time.Minute);
  578. AssertEquals ("test#18", 0, time.Second);
  579. time = Directory.GetLastAccessTimeUtc (path);
  580. AssertEquals ("test#19", 2003, time.Year);
  581. AssertEquals ("test#20", 6, time.Month);
  582. AssertEquals ("test#21", 4, time.Day);
  583. AssertEquals ("test#22", 6, time.Hour);
  584. AssertEquals ("test#23", 4, time.Minute);
  585. AssertEquals ("test#24", 0, time.Second);
  586. } finally {
  587. DeleteDirectory (path);
  588. }
  589. }
  590. [Test]
  591. public void LastWriteTime ()
  592. {
  593. string path = TempFolder + DSC + "DirectoryTest.WriteTime.1";
  594. DeleteDirectory (path);
  595. try {
  596. Directory.CreateDirectory (path);
  597. Directory.SetLastWriteTime (path, new DateTime (2003, 6, 4, 6, 4, 0));
  598. DateTime time = Directory.GetLastWriteTime (path);
  599. AssertEquals ("test#01", 2003, time.Year);
  600. AssertEquals ("test#02", 6, time.Month);
  601. AssertEquals ("test#03", 4, time.Day);
  602. AssertEquals ("test#04", 6, time.Hour);
  603. AssertEquals ("test#05", 4, time.Minute);
  604. AssertEquals ("test#06", 0, time.Second);
  605. time = TimeZone.CurrentTimeZone.ToLocalTime (Directory.GetLastWriteTimeUtc (path));
  606. AssertEquals ("test#07", 2003, time.Year);
  607. AssertEquals ("test#08", 6, time.Month);
  608. AssertEquals ("test#09", 4, time.Day);
  609. AssertEquals ("test#10", 6, time.Hour);
  610. AssertEquals ("test#11", 4, time.Minute);
  611. AssertEquals ("test#12", 0, time.Second);
  612. Directory.SetLastWriteTimeUtc (path, new DateTime (2003, 6, 4, 6, 4, 0));
  613. time = TimeZone.CurrentTimeZone.ToUniversalTime (Directory.GetLastWriteTime (path));
  614. AssertEquals ("test#13", 2003, time.Year);
  615. AssertEquals ("test#14", 6, time.Month);
  616. AssertEquals ("test#15", 4, time.Day);
  617. AssertEquals ("test#16", 6, time.Hour);
  618. AssertEquals ("test#17", 4, time.Minute);
  619. AssertEquals ("test#18", 0, time.Second);
  620. time = Directory.GetLastWriteTimeUtc (path);
  621. AssertEquals ("test#19", 2003, time.Year);
  622. AssertEquals ("test#20", 6, time.Month);
  623. AssertEquals ("test#21", 4, time.Day);
  624. AssertEquals ("test#22", 6, time.Hour);
  625. AssertEquals ("test#23", 4, time.Minute);
  626. AssertEquals ("test#24", 0, time.Second);
  627. } finally {
  628. DeleteDirectory (path);
  629. }
  630. }
  631. [Test]
  632. [ExpectedException(typeof(ArgumentNullException))]
  633. public void SetLastWriteTimeException1 ()
  634. {
  635. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  636. Directory.SetLastWriteTime (null as string, time);
  637. }
  638. [Test]
  639. [ExpectedException(typeof(ArgumentException))]
  640. public void SetLastWriteTimeException2 ()
  641. {
  642. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  643. Directory.SetLastWriteTime ("", time);
  644. }
  645. [Test]
  646. [ExpectedException(typeof(FileNotFoundException))]
  647. public void SetLastWriteTimeException3 ()
  648. {
  649. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  650. string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTime.2";
  651. DeleteDirectory (path);
  652. try {
  653. Directory.SetLastWriteTime (path, time);
  654. } finally {
  655. DeleteDirectory (path);
  656. }
  657. }
  658. [Test]
  659. [ExpectedException(typeof(ArgumentException))]
  660. public void SetLastWriteTimeException4 ()
  661. {
  662. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  663. Directory.SetLastWriteTime (" ", time);
  664. }
  665. [Test]
  666. [ExpectedException(typeof(ArgumentException))]
  667. public void SetLastWriteTimeException5 ()
  668. {
  669. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  670. Directory.SetLastWriteTime (Path.InvalidPathChars [0].ToString (), time);
  671. }
  672. // [Test]
  673. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  674. // public void SetLastWriteTimeException6 ()
  675. // {
  676. // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2);
  677. // string path = TempFolder + Path.DirectorySeparatorChar + "DirectoryTest.SetLastWriteTime.1";
  678. //
  679. // try {
  680. // if (!Directory.Exists (path))
  681. // Directory.CreateDirectory (path);
  682. //
  683. // Directory.SetLastWriteTime (path, time);
  684. // } finally {
  685. // DeleteDirectory (path);
  686. // }
  687. //
  688. // }
  689. [Test]
  690. [ExpectedException(typeof(ArgumentNullException))]
  691. public void SetLastWriteTimeUtcException1 ()
  692. {
  693. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  694. Directory.SetLastWriteTimeUtc (null as string, time);
  695. }
  696. [Test]
  697. [ExpectedException(typeof(ArgumentException))]
  698. public void SetLastWriteTimeUtcException2 ()
  699. {
  700. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  701. Directory.SetLastWriteTimeUtc ("", time);
  702. }
  703. [Test]
  704. [ExpectedException(typeof(FileNotFoundException))]
  705. public void SetLastWriteTimeUtcException3 ()
  706. {
  707. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  708. string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTimeUtc.2";
  709. DeleteDirectory (path);
  710. try {
  711. Directory.SetLastWriteTimeUtc (path, time);
  712. } finally {
  713. DeleteDirectory (path);
  714. }
  715. }
  716. [Test]
  717. [ExpectedException(typeof(ArgumentException))]
  718. public void SetLastWriteTimeUtcException4 ()
  719. {
  720. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  721. Directory.SetLastWriteTimeUtc (" ", time);
  722. }
  723. [Test]
  724. [ExpectedException(typeof(ArgumentException))]
  725. public void SetLastWriteTimeUtcException5 ()
  726. {
  727. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  728. Directory.SetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString (), time);
  729. }
  730. // [Test]
  731. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  732. // public void SetLastWriteTimeUtcException6 ()
  733. // {
  734. // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2);
  735. // string path = TempFolder + DSC + "DirectoryTest.SetLastWriteTimeUtc.1";
  736. //
  737. // if (!Directory.Exists (path))
  738. // Directory.CreateDirectory (path);
  739. // try {
  740. // Directory.SetLastWriteTimeUtc (path, time);
  741. // } finally {
  742. // DeleteDirectory (path);
  743. // }
  744. // }
  745. [Test]
  746. [ExpectedException(typeof(ArgumentNullException))]
  747. public void SetLastAccessTimeException1 ()
  748. {
  749. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  750. Directory.SetLastAccessTime (null as string, time);
  751. }
  752. [Test]
  753. [ExpectedException(typeof(ArgumentException))]
  754. public void SetLastAccessTimeException2 ()
  755. {
  756. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  757. Directory.SetLastAccessTime ("", time);
  758. }
  759. [Test]
  760. [ExpectedException(typeof(FileNotFoundException))]
  761. public void SetLastAccessTimeException3 ()
  762. {
  763. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  764. string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTime.2";
  765. DeleteDirectory (path);
  766. try {
  767. Directory.SetLastAccessTime (path, time);
  768. } finally {
  769. DeleteDirectory (path);
  770. }
  771. }
  772. [Test]
  773. [ExpectedException(typeof(ArgumentException))]
  774. public void SetLastAccessTimeException4 ()
  775. {
  776. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  777. Directory.SetLastAccessTime (" ", time);
  778. }
  779. [Test]
  780. [ExpectedException(typeof(ArgumentException))]
  781. public void SetLastAccessTimeException5 ()
  782. {
  783. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  784. Directory.SetLastAccessTime (Path.InvalidPathChars [0].ToString (), time);
  785. }
  786. // [Test]
  787. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  788. // public void SetLastAccessTimeException6 ()
  789. // {
  790. // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2);
  791. // string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTime.1";
  792. //
  793. // if (!Directory.Exists (path))
  794. // Directory.CreateDirectory (path);
  795. // try {
  796. // Directory.SetLastAccessTime (path, time);
  797. // } finally {
  798. // DeleteDirectory (path);
  799. // }
  800. //
  801. // }
  802. [Test]
  803. [ExpectedException(typeof(ArgumentNullException))]
  804. public void SetLastAccessTimeUtcException1 ()
  805. {
  806. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  807. Directory.SetLastAccessTimeUtc (null as string, time);
  808. }
  809. [Test]
  810. [ExpectedException(typeof(ArgumentException))]
  811. public void SetLastAccessTimeUtcException2 ()
  812. {
  813. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  814. Directory.SetLastAccessTimeUtc ("", time);
  815. }
  816. [Test]
  817. [ExpectedException(typeof(FileNotFoundException))]
  818. public void SetLastAccessTimeUtcException3 ()
  819. {
  820. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  821. string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.2";
  822. DeleteDirectory (path);
  823. try {
  824. Directory.SetLastAccessTimeUtc (path, time);
  825. } finally {
  826. DeleteDirectory (path);
  827. }
  828. }
  829. [Test]
  830. [ExpectedException(typeof(ArgumentException))]
  831. public void SetLastAccessTimeUtcException4 ()
  832. {
  833. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  834. Directory.SetLastAccessTimeUtc (" ", time);
  835. }
  836. [Test]
  837. [ExpectedException(typeof(ArgumentException))]
  838. public void SetLastAccessTimeUtcException5 ()
  839. {
  840. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  841. Directory.SetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString (), time);
  842. }
  843. // [Test]
  844. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  845. // public void SetLastAccessTimeUtcException6 ()
  846. // {
  847. // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2);
  848. // string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.1";
  849. //
  850. // if (!Directory.Exists (path))
  851. // Directory.CreateDirectory (path);
  852. // try {
  853. // Directory.SetLastAccessTimeUtc (path, time);
  854. // } finally {
  855. // DeleteDirectory (path);
  856. // }
  857. // }
  858. [Test]
  859. [ExpectedException(typeof(ArgumentNullException))]
  860. public void SetCreationTimeException1 ()
  861. {
  862. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  863. Directory.SetCreationTime (null as string, time);
  864. }
  865. [Test]
  866. [ExpectedException(typeof(ArgumentException))]
  867. public void SetCreationTimeException2 ()
  868. {
  869. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  870. Directory.SetCreationTime ("", time);
  871. }
  872. [Test]
  873. [ExpectedException(typeof(FileNotFoundException))]
  874. public void SetCreationTimeException3 ()
  875. {
  876. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  877. string path = TempFolder + DSC + "DirectoryTest.SetCreationTime.2";
  878. DeleteDirectory (path);
  879. try {
  880. Directory.SetCreationTime (path, time);
  881. } finally {
  882. DeleteDirectory (path);
  883. }
  884. }
  885. [Test]
  886. [ExpectedException(typeof(ArgumentException))]
  887. public void SetCreationTimeException4 ()
  888. {
  889. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  890. Directory.SetCreationTime (" ", time);
  891. }
  892. [Test]
  893. [ExpectedException(typeof(ArgumentException))]
  894. public void SetCreationTimeException5 ()
  895. {
  896. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  897. Directory.SetCreationTime (Path.InvalidPathChars [0].ToString (), time);
  898. }
  899. // [Test]
  900. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  901. // public void SetCreationTimeException6 ()
  902. // {
  903. // DateTime time = new DateTime (1003, 4, 6, 6, 4, 2);
  904. // string path = TempFolder + DSC + "DirectoryTest.SetCreationTime.1";
  905. //
  906. // if (!Directory.Exists (path))
  907. // Directory.CreateDirectory (path);
  908. // try {
  909. // Directory.SetCreationTime (path, time);
  910. // DeleteDirectory (path);
  911. // } finally {
  912. // DeleteDirectory (path);
  913. // }
  914. //
  915. // }
  916. [Test]
  917. [ExpectedException(typeof(ArgumentNullException))]
  918. public void SetCreationTimeUtcException1 ()
  919. {
  920. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  921. Directory.SetCreationTimeUtc (null as string, time);
  922. }
  923. [Test]
  924. [ExpectedException(typeof(ArgumentException))]
  925. public void SetCreationTimeUtcException2 ()
  926. {
  927. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  928. Directory.SetCreationTimeUtc ("", time);
  929. }
  930. [Test]
  931. [ExpectedException(typeof(FileNotFoundException))]
  932. public void SetCreationTimeUtcException3 ()
  933. {
  934. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  935. string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.2";
  936. DeleteDirectory (path);
  937. try {
  938. Directory.SetCreationTimeUtc (path, time);
  939. DeleteDirectory (path);
  940. } finally {
  941. DeleteDirectory (path);
  942. }
  943. }
  944. [Test]
  945. [ExpectedException(typeof(ArgumentException))]
  946. public void SetCreationTimeUtcException4 ()
  947. {
  948. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  949. Directory.SetCreationTimeUtc (" ", time);
  950. }
  951. [Test]
  952. [ExpectedException(typeof(ArgumentException))]
  953. public void SetCreationTimeUtcException5 ()
  954. {
  955. DateTime time = new DateTime (2003, 4, 6, 6, 4, 2);
  956. Directory.SetCreationTimeUtc (Path.InvalidPathChars [0].ToString (), time);
  957. }
  958. // [Test]
  959. // [ExpectedException(typeof(ArgumentOutOfRangeException))]
  960. // public void SetCreationTimeUtcException6 ()
  961. // {
  962. // DateTime time = new DateTime (1000, 4, 6, 6, 4, 2);
  963. // string path = TempFolder + DSC + "DirectoryTest.SetLastAccessTimeUtc.1";
  964. //
  965. // if (!Directory.Exists (path))
  966. // Directory.CreateDirectory (path);
  967. // try {
  968. // Directory.SetCreationTimeUtc (path, time);
  969. // DeleteDirectory (path);
  970. // } finally {
  971. // DeleteDirectory (path);
  972. // }
  973. // }
  974. [Test]
  975. public void GetDirectories ()
  976. {
  977. string path = TempFolder;
  978. string DirPath = TempFolder + Path.DirectorySeparatorChar + ".GetDirectories";
  979. DeleteDirectory (DirPath);
  980. try {
  981. Directory.CreateDirectory (DirPath);
  982. string [] dirs = Directory.GetDirectories (path);
  983. foreach (string directory in dirs) {
  984. if (directory == DirPath)
  985. return;
  986. }
  987. Assert ("Directory Not Found", false);
  988. } finally {
  989. DeleteDirectory (DirPath);
  990. }
  991. }
  992. [Test]
  993. public void GetParentOfRootDirectory ()
  994. {
  995. DirectoryInfo info;
  996. info = Directory.GetParent (Path.GetPathRoot (Path.GetTempPath ()));
  997. AssertEquals (null, info);
  998. }
  999. [Test]
  1000. public void GetFiles ()
  1001. {
  1002. string path = TempFolder;
  1003. string DirPath = TempFolder + Path.DirectorySeparatorChar + ".GetFiles";
  1004. if (File.Exists (DirPath))
  1005. File.Delete (DirPath);
  1006. try {
  1007. File.Create (DirPath).Close ();
  1008. string [] files = Directory.GetFiles (TempFolder);
  1009. foreach (string directory in files) {
  1010. if (directory == DirPath)
  1011. return;
  1012. }
  1013. Assert ("File Not Found", false);
  1014. } finally {
  1015. if (File.Exists (DirPath))
  1016. File.Delete (DirPath);
  1017. }
  1018. }
  1019. [Test]
  1020. [ExpectedException (typeof (ArgumentNullException))]
  1021. public void SetCurrentDirectoryNull ()
  1022. {
  1023. Directory.SetCurrentDirectory (null);
  1024. }
  1025. [Test]
  1026. [ExpectedException (typeof (ArgumentException))]
  1027. public void SetCurrentDirectoryEmpty ()
  1028. {
  1029. Directory.SetCurrentDirectory (String.Empty);
  1030. }
  1031. [Test]
  1032. [ExpectedException (typeof (ArgumentException))]
  1033. public void SetCurrentDirectoryWhitespace ()
  1034. {
  1035. Directory.SetCurrentDirectory (" ");
  1036. }
  1037. [Test]
  1038. public void GetNoFiles () // Bug 58875. This throwed an exception on windows.
  1039. {
  1040. DirectoryInfo dir = new DirectoryInfo (".");
  1041. dir.GetFiles ("*.nonext");
  1042. }
  1043. private void DeleteDirectory (string path)
  1044. {
  1045. if (Directory.Exists (path))
  1046. Directory.Delete (path, true);
  1047. }
  1048. }
  1049. }