FileTest.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. //
  2. // FileTest.cs: Test cases for System.IO.File
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. // Ville Palo ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. http://www.ximian.com
  9. //
  10. // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
  11. //
  12. using NUnit.Framework;
  13. using System;
  14. using System.IO;
  15. using System.Globalization;
  16. using System.Threading;
  17. namespace MonoTests.System.IO
  18. {
  19. [TestFixture]
  20. public class FileTest : Assertion
  21. {
  22. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  23. [SetUp]
  24. public void SetUp ()
  25. {
  26. if (Directory.Exists (TempFolder))
  27. Directory.Delete (TempFolder, true);
  28. Directory.CreateDirectory (TempFolder);
  29. Thread.CurrentThread.CurrentCulture = new CultureInfo ("EN-us");
  30. }
  31. [TearDown]
  32. public void TearDown ()
  33. {
  34. if (Directory.Exists (TempFolder))
  35. Directory.Delete (TempFolder, true);
  36. }
  37. [Test]
  38. public void TestExists ()
  39. {
  40. int i = 0;
  41. FileStream s = null;
  42. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  43. try {
  44. Assert ("null filename should not exist", !File.Exists (null));
  45. i++;
  46. Assert ("empty filename should not exist", !File.Exists (""));
  47. i++;
  48. Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
  49. i++;
  50. DeleteFile (path);
  51. s = File.Create (path);
  52. s.Close ();
  53. Assert ("File " + path + " should exists", File.Exists (path));
  54. i++;
  55. Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"));
  56. } catch (Exception e) {
  57. Fail ("Unexpected exception at i = " + i + ". e=" + e);
  58. } finally {
  59. if (s != null)
  60. s.Close ();
  61. DeleteFile (path);
  62. }
  63. }
  64. [Test]
  65. public void Exists_InvalidFileName ()
  66. {
  67. Assert ("><|", !File.Exists ("><|"));
  68. }
  69. [Test]
  70. public void Exists_InvalidDirectory ()
  71. {
  72. Assert ("InvalidDirectory", !File.Exists (Path.Combine ("does not exist", "file.txt")));
  73. }
  74. [Test]
  75. [ExpectedException(typeof (ArgumentNullException))]
  76. public void CtorArgumentNullException1 ()
  77. {
  78. FileStream stream = File.Create (null);
  79. }
  80. [Test]
  81. [ExpectedException(typeof (ArgumentException))]
  82. public void CtorArgumentException1 ()
  83. {
  84. FileStream stream = File.Create ("");
  85. }
  86. [Test]
  87. [ExpectedException(typeof (ArgumentException))]
  88. public void CtorArgumentException2 ()
  89. {
  90. FileStream stream = File.Create (" ");
  91. }
  92. [Test]
  93. [ExpectedException(typeof (DirectoryNotFoundException))]
  94. public void CtorDirectoryNotFoundException ()
  95. {
  96. FileStream stream = null;
  97. string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  98. try {
  99. stream = File.Create (path);
  100. } finally {
  101. if (stream != null)
  102. stream.Close ();
  103. DeleteFile (path);
  104. }
  105. }
  106. [Test]
  107. public void TestCreate ()
  108. {
  109. FileStream stream = null;
  110. string path = "";
  111. /* positive test: create resources/foo */
  112. try {
  113. path = TempFolder + Path.DirectorySeparatorChar + "foo";
  114. stream = File.Create (path);
  115. Assert ("File should exist", File.Exists (path));
  116. stream.Close ();
  117. } catch (Exception e) {
  118. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  119. } finally {
  120. if (stream != null)
  121. stream.Close ();
  122. DeleteFile (path);
  123. }
  124. path = "";
  125. stream = null;
  126. /* positive test: repeat test above again to test for overwriting file */
  127. try {
  128. path = TempFolder + Path.DirectorySeparatorChar + "foo";
  129. stream = File.Create (path);
  130. Assert ("File should exist", File.Exists (path));
  131. stream.Close ();
  132. } catch (Exception e) {
  133. Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
  134. } finally {
  135. if (stream != null)
  136. stream.Close ();
  137. DeleteFile (path);
  138. }
  139. }
  140. [Test]
  141. [ExpectedException(typeof(ArgumentNullException))]
  142. public void CopyArgumentNullException1 ()
  143. {
  144. File.Copy (null, "b");
  145. }
  146. [Test]
  147. [ExpectedException(typeof(ArgumentNullException))]
  148. public void CopyArgumentNullException2 ()
  149. {
  150. File.Copy ("a", null);
  151. }
  152. [Test]
  153. [ExpectedException(typeof(ArgumentException))]
  154. public void CopyArgumentException1 ()
  155. {
  156. File.Copy ("", "b");
  157. }
  158. [Test]
  159. [ExpectedException(typeof(ArgumentException))]
  160. public void CopyArgumentException2 ()
  161. {
  162. File.Copy ("a", "");
  163. }
  164. [Test]
  165. [ExpectedException(typeof(ArgumentException))]
  166. public void CopyArgumentException3 ()
  167. {
  168. File.Copy (" ", "b");
  169. }
  170. [Test]
  171. [ExpectedException(typeof(ArgumentException))]
  172. public void CopyArgumentException4 ()
  173. {
  174. File.Copy ("a", " ");
  175. }
  176. [Test]
  177. [ExpectedException(typeof(FileNotFoundException))]
  178. public void CopyFileNotFoundException ()
  179. {
  180. File.Copy ("doesnotexist", "b");
  181. }
  182. [ExpectedException(typeof(IOException))]
  183. public void CopyIOException ()
  184. {
  185. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
  186. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  187. try {
  188. File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
  189. File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
  190. File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
  191. } finally {
  192. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
  193. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  194. }
  195. }
  196. [Test]
  197. public void TestCopy ()
  198. {
  199. string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
  200. string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  201. /* positive test: copy resources/AFile.txt to resources/bar */
  202. try {
  203. try {
  204. DeleteFile (path1);
  205. DeleteFile (path2);
  206. File.Create (path2).Close ();
  207. File.Copy (path2, path1);
  208. Assert ("File AFile.txt should still exist", File.Exists (path2));
  209. Assert ("File bar should exist after File.Copy", File.Exists (path1));
  210. } catch (Exception e) {
  211. Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
  212. }
  213. /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
  214. try {
  215. Assert ("File bar should exist before File.Copy", File.Exists (path1));
  216. File.Copy (path2, path1, true);
  217. Assert ("File AFile.txt should still exist", File.Exists (path2));
  218. Assert ("File bar should exist after File.Copy", File.Exists (path1));
  219. } catch (Exception e) {
  220. Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
  221. }
  222. }finally {
  223. DeleteFile (path1);
  224. DeleteFile (path2);
  225. }
  226. }
  227. [Test]
  228. [ExpectedException (typeof (ArgumentNullException))]
  229. public void DeleteArgumentNullException ()
  230. {
  231. File.Delete (null);
  232. }
  233. [Test]
  234. [ExpectedException (typeof (ArgumentException))]
  235. public void DeleteArgumentException1 ()
  236. {
  237. File.Delete ("");
  238. }
  239. [Test]
  240. [ExpectedException (typeof (ArgumentException))]
  241. public void DeleteArgumentException2 ()
  242. {
  243. File.Delete (" ");
  244. }
  245. [Test]
  246. [ExpectedException (typeof (DirectoryNotFoundException))]
  247. public void DeleteDirectoryNotFoundException ()
  248. {
  249. string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
  250. if (Directory.Exists (path))
  251. Directory.Delete (path, true);
  252. File.Delete (path);
  253. }
  254. [Test]
  255. public void TestDelete ()
  256. {
  257. string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
  258. DeleteFile (foopath);
  259. try {
  260. File.Create (foopath).Close ();
  261. try {
  262. File.Delete (foopath);
  263. } catch (Exception e) {
  264. Fail ("Unable to delete " + foopath + " e=" + e.ToString());
  265. }
  266. Assert ("File " + foopath + " should not exist after File.Delete", !File.Exists (foopath));
  267. } finally {
  268. DeleteFile (foopath);
  269. }
  270. }
  271. [Test]
  272. [ExpectedException(typeof (IOException))]
  273. [Category("NotWorking")]
  274. public void DeleteOpenStreamException ()
  275. {
  276. string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
  277. DeleteFile (path);
  278. FileStream stream = null;
  279. try {
  280. stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  281. File.Delete (path);
  282. } finally {
  283. if (stream != null)
  284. stream.Close ();
  285. DeleteFile (path);
  286. }
  287. }
  288. [Test]
  289. [ExpectedException(typeof (ArgumentNullException))]
  290. public void MoveException1 ()
  291. {
  292. File.Move (null, "b");
  293. }
  294. [Test]
  295. [ExpectedException(typeof (ArgumentNullException))]
  296. public void MoveException2 ()
  297. {
  298. File.Move ("a", null);
  299. }
  300. [Test]
  301. [ExpectedException(typeof (ArgumentException))]
  302. public void MoveException3 ()
  303. {
  304. File.Move ("", "b");
  305. }
  306. [Test]
  307. [ExpectedException(typeof (ArgumentException))]
  308. public void MoveException4 ()
  309. {
  310. File.Move ("a", "");
  311. }
  312. [Test]
  313. [ExpectedException(typeof (ArgumentException))]
  314. public void MoveException5 ()
  315. {
  316. File.Move (" ", "b");
  317. }
  318. [Test]
  319. [ExpectedException(typeof (ArgumentException))]
  320. public void MoveException6 ()
  321. {
  322. File.Move ("a", " ");
  323. }
  324. [Test]
  325. [ExpectedException(typeof (FileNotFoundException))]
  326. public void MoveException7 ()
  327. {
  328. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist");
  329. File.Move (TempFolder + Path.DirectorySeparatorChar + "doesnotexist", "b");
  330. }
  331. [Test]
  332. [ExpectedException(typeof (DirectoryNotFoundException))]
  333. public void MoveException8 ()
  334. {
  335. string path = TempFolder + Path.DirectorySeparatorChar + "foo";
  336. DeleteFile (path);
  337. try {
  338. File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
  339. File.Copy(TempFolder + Path.DirectorySeparatorChar + "AFile.txt", path, true);
  340. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
  341. File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
  342. } finally {
  343. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
  344. DeleteFile (path);
  345. }
  346. }
  347. [Test]
  348. [ExpectedException(typeof (IOException))]
  349. public void MoveException9 ()
  350. {
  351. File.Create (TempFolder + Path.DirectorySeparatorChar + "foo").Close ();
  352. try {
  353. File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder);
  354. } finally {
  355. DeleteFile (TempFolder + Path.DirectorySeparatorChar + "foo");
  356. }
  357. }
  358. [Test]
  359. public void TestMove ()
  360. {
  361. string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
  362. string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
  363. if (!File.Exists (bar)) {
  364. FileStream f = File.Create(bar);
  365. f.Close();
  366. }
  367. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should exist", File.Exists (bar));
  368. File.Move (bar, baz);
  369. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists (bar));
  370. Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "baz should exist", File.Exists (baz));
  371. // Test moving of directories
  372. string dir = Path.Combine (TempFolder, "dir");
  373. string dir2 = Path.Combine (TempFolder, "dir2");
  374. string dir_foo = Path.Combine (dir, "foo");
  375. string dir2_foo = Path.Combine (dir2, "foo");
  376. if (Directory.Exists (dir))
  377. Directory.Delete (dir, true);
  378. Directory.CreateDirectory (dir);
  379. Directory.CreateDirectory (dir2);
  380. File.Create (dir_foo).Close ();
  381. File.Move (dir_foo, dir2_foo);
  382. Assert (File.Exists (dir2_foo));
  383. Directory.Delete (dir, true);
  384. Directory.Delete (dir2, true);
  385. DeleteFile (dir_foo);
  386. DeleteFile (dir2_foo);
  387. }
  388. [Test]
  389. public void TestOpen ()
  390. {
  391. string path = "";
  392. FileStream stream = null;
  393. try {
  394. path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  395. if (!File.Exists (path))
  396. stream = File.Create (path);
  397. stream.Close ();
  398. stream = File.Open (path, FileMode.Open);
  399. stream.Close ();
  400. } catch (Exception e) {
  401. Fail ("Unable to open " + TempFolder + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
  402. } finally {
  403. if (stream != null)
  404. stream.Close ();
  405. DeleteFile (path);
  406. }
  407. path = "";
  408. stream = null;
  409. /* Exception tests */
  410. try {
  411. path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
  412. stream = File.Open (path, FileMode.Open);
  413. Fail ("File 'filedoesnotexist' should not exist");
  414. } catch (FileNotFoundException) {
  415. // do nothing, this is what we expect
  416. } catch (Exception e) {
  417. Fail ("Unexpect exception caught: e=" + e.ToString());
  418. } finally {
  419. if (stream != null)
  420. stream.Close ();
  421. DeleteFile (path);
  422. }
  423. }
  424. [Test]
  425. public void Open ()
  426. {
  427. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  428. if (!File.Exists (path))
  429. File.Create (path).Close ();
  430. FileStream stream = null;
  431. try {
  432. stream = File.Open (path, FileMode.Open);
  433. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  434. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  435. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  436. stream.Close ();
  437. stream = File.Open (path, FileMode.Open, FileAccess.Write);
  438. Assertion.AssertEquals ("test#04", false, stream.CanRead);
  439. Assertion.AssertEquals ("test#05", true, stream.CanSeek);
  440. Assertion.AssertEquals ("test#06", true, stream.CanWrite);
  441. stream.Close ();
  442. stream = File.Open (path, FileMode.Open, FileAccess.Read);
  443. Assertion.AssertEquals ("test#04", true, stream.CanRead);
  444. Assertion.AssertEquals ("test#05", true, stream.CanSeek);
  445. Assertion.AssertEquals ("test#06", false, stream.CanWrite);
  446. stream.Close ();
  447. } finally {
  448. if (stream != null)
  449. stream.Close ();
  450. DeleteFile (path);
  451. }
  452. }
  453. [Test]
  454. [ExpectedException(typeof(ArgumentException))]
  455. public void OpenException1 ()
  456. {
  457. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  458. FileStream stream = null;
  459. // CreateNew + Read throws an exceptoin
  460. try {
  461. stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
  462. } finally {
  463. if (stream != null)
  464. stream.Close ();
  465. DeleteFile (path);
  466. }
  467. }
  468. [Test]
  469. [ExpectedException(typeof(ArgumentException))]
  470. public void OpenException2 ()
  471. {
  472. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  473. FileStream s = null;
  474. // Append + Read throws an exceptoin
  475. if (!File.Exists (path))
  476. File.Create (path).Close ();
  477. try {
  478. s = File.Open (path, FileMode.Append, FileAccess.Read);
  479. } finally {
  480. if (s != null)
  481. s.Close ();
  482. DeleteFile (path);
  483. }
  484. }
  485. [Test]
  486. public void OpenRead ()
  487. {
  488. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  489. if (!File.Exists (path))
  490. File.Create (path).Close ();
  491. FileStream stream = null;
  492. try {
  493. stream = File.OpenRead (path);
  494. Assertion.AssertEquals ("test#01", true, stream.CanRead);
  495. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  496. Assertion.AssertEquals ("test#03", false, stream.CanWrite);
  497. } finally {
  498. if (stream != null)
  499. stream.Close ();
  500. DeleteFile (path);
  501. }
  502. }
  503. [Test]
  504. public void OpenWrite ()
  505. {
  506. string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  507. if (!File.Exists (path))
  508. File.Create (path).Close ();
  509. FileStream stream = null;
  510. try {
  511. stream = File.OpenWrite (path);
  512. Assertion.AssertEquals ("test#01", false, stream.CanRead);
  513. Assertion.AssertEquals ("test#02", true, stream.CanSeek);
  514. Assertion.AssertEquals ("test#03", true, stream.CanWrite);
  515. stream.Close ();
  516. } finally {
  517. if (stream != null)
  518. stream.Close ();
  519. DeleteFile (path);
  520. }
  521. }
  522. [Test]
  523. public void TestGetCreationTime ()
  524. {
  525. string path = TempFolder + Path.DirectorySeparatorChar + "baz";
  526. DeleteFile (path);
  527. try {
  528. File.Create (path).Close();
  529. DateTime time = File.GetCreationTime (path);
  530. Assert ("GetCreationTime incorrect", (DateTime.Now - time).TotalSeconds < 10);
  531. } finally {
  532. DeleteFile (path);
  533. }
  534. }
  535. [Test]
  536. [ExpectedException(typeof(IOException))]
  537. public void TestGetCreationTimeException ()
  538. {
  539. // Test nonexistent files
  540. string path2 = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
  541. DeleteFile (path2);
  542. // should throw an exception
  543. File.GetCreationTime (path2);
  544. }
  545. [Test]
  546. public void CreationTime ()
  547. {
  548. string path = TempFolder + Path.DirectorySeparatorChar + "creationTime";
  549. if (File.Exists (path))
  550. File.Delete (path);
  551. FileStream stream = null;
  552. try {
  553. stream = File.Create (path);
  554. stream.Close ();
  555. File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  556. DateTime time = File.GetCreationTime (path);
  557. Assertion.AssertEquals ("test#01", 2002, time.Year);
  558. Assertion.AssertEquals ("test#02", 4, time.Month);
  559. Assertion.AssertEquals ("test#03", 6, time.Day);
  560. Assertion.AssertEquals ("test#04", 4, time.Hour);
  561. Assertion.AssertEquals ("test#05", 4, time.Second);
  562. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
  563. Assertion.AssertEquals ("test#06", 2002, time.Year);
  564. Assertion.AssertEquals ("test#07", 4, time.Month);
  565. Assertion.AssertEquals ("test#08", 6, time.Day);
  566. Assertion.AssertEquals ("test#09", 4, time.Hour);
  567. Assertion.AssertEquals ("test#10", 4, time.Second);
  568. File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  569. time = File.GetCreationTimeUtc (path);
  570. Assertion.AssertEquals ("test#11", 2002, time.Year);
  571. Assertion.AssertEquals ("test#12", 4, time.Month);
  572. Assertion.AssertEquals ("test#13", 6, time.Day);
  573. Assertion.AssertEquals ("test#14", 4, time.Hour);
  574. Assertion.AssertEquals ("test#15", 4, time.Second);
  575. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
  576. Assertion.AssertEquals ("test#16", 2002, time.Year);
  577. Assertion.AssertEquals ("test#17", 4, time.Month);
  578. Assertion.AssertEquals ("test#18", 6, time.Day);
  579. Assertion.AssertEquals ("test#19", 4, time.Hour);
  580. Assertion.AssertEquals ("test#20", 4, time.Second);
  581. } finally {
  582. if (stream != null)
  583. stream.Close ();
  584. DeleteFile (path);
  585. }
  586. }
  587. [Test]
  588. public void LastAccessTime ()
  589. {
  590. string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
  591. if (File.Exists (path))
  592. File.Delete (path);
  593. FileStream stream = null;
  594. try {
  595. stream = File.Create (path);
  596. stream.Close ();
  597. File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  598. DateTime time = File.GetLastAccessTime (path);
  599. Assertion.AssertEquals ("test#01", 2002, time.Year);
  600. Assertion.AssertEquals ("test#02", 4, time.Month);
  601. Assertion.AssertEquals ("test#03", 6, time.Day);
  602. Assertion.AssertEquals ("test#04", 4, time.Hour);
  603. Assertion.AssertEquals ("test#05", 4, time.Second);
  604. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
  605. Assertion.AssertEquals ("test#06", 2002, time.Year);
  606. Assertion.AssertEquals ("test#07", 4, time.Month);
  607. Assertion.AssertEquals ("test#08", 6, time.Day);
  608. Assertion.AssertEquals ("test#09", 4, time.Hour);
  609. Assertion.AssertEquals ("test#10", 4, time.Second);
  610. File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  611. time = File.GetLastAccessTimeUtc (path);
  612. Assertion.AssertEquals ("test#11", 2002, time.Year);
  613. Assertion.AssertEquals ("test#12", 4, time.Month);
  614. Assertion.AssertEquals ("test#13", 6, time.Day);
  615. Assertion.AssertEquals ("test#14", 4, time.Hour);
  616. Assertion.AssertEquals ("test#15", 4, time.Second);
  617. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
  618. Assertion.AssertEquals ("test#16", 2002, time.Year);
  619. Assertion.AssertEquals ("test#17", 4, time.Month);
  620. Assertion.AssertEquals ("test#18", 6, time.Day);
  621. Assertion.AssertEquals ("test#19", 4, time.Hour);
  622. Assertion.AssertEquals ("test#20", 4, time.Second);
  623. } finally {
  624. if (stream != null)
  625. stream.Close ();
  626. DeleteFile (path);
  627. }
  628. }
  629. [Test]
  630. public void LastWriteTime ()
  631. {
  632. string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
  633. if (File.Exists (path))
  634. File.Delete (path);
  635. FileStream stream = null;
  636. try {
  637. stream = File.Create (path);
  638. stream.Close ();
  639. File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
  640. DateTime time = File.GetLastWriteTime (path);
  641. Assertion.AssertEquals ("test#01", 2002, time.Year);
  642. Assertion.AssertEquals ("test#02", 4, time.Month);
  643. Assertion.AssertEquals ("test#03", 6, time.Day);
  644. Assertion.AssertEquals ("test#04", 4, time.Hour);
  645. Assertion.AssertEquals ("test#05", 4, time.Second);
  646. time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
  647. Assertion.AssertEquals ("test#06", 2002, time.Year);
  648. Assertion.AssertEquals ("test#07", 4, time.Month);
  649. Assertion.AssertEquals ("test#08", 6, time.Day);
  650. Assertion.AssertEquals ("test#09", 4, time.Hour);
  651. Assertion.AssertEquals ("test#10", 4, time.Second);
  652. File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
  653. time = File.GetLastWriteTimeUtc (path);
  654. Assertion.AssertEquals ("test#11", 2002, time.Year);
  655. Assertion.AssertEquals ("test#12", 4, time.Month);
  656. Assertion.AssertEquals ("test#13", 6, time.Day);
  657. Assertion.AssertEquals ("test#14", 4, time.Hour);
  658. Assertion.AssertEquals ("test#15", 4, time.Second);
  659. time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
  660. Assertion.AssertEquals ("test#16", 2002, time.Year);
  661. Assertion.AssertEquals ("test#17", 4, time.Month);
  662. Assertion.AssertEquals ("test#18", 6, time.Day);
  663. Assertion.AssertEquals ("test#19", 4, time.Hour);
  664. Assertion.AssertEquals ("test#20", 4, time.Second);
  665. } finally {
  666. if (stream != null)
  667. stream.Close ();
  668. DeleteFile (path);
  669. }
  670. }
  671. [Test]
  672. [ExpectedException(typeof(ArgumentNullException))]
  673. public void GetCreationTimeException1 ()
  674. {
  675. File.GetCreationTime (null as string);
  676. }
  677. [Test]
  678. [ExpectedException(typeof(ArgumentException))]
  679. public void GetCreationTimeException2 ()
  680. {
  681. File.GetCreationTime ("");
  682. }
  683. [Test]
  684. [ExpectedException(typeof(IOException))]
  685. public void GetCreationTimeException3 ()
  686. {
  687. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
  688. DeleteFile (path);
  689. File.GetCreationTime (path);
  690. }
  691. [Test]
  692. [ExpectedException(typeof(ArgumentException))]
  693. public void GetCreationTimeException4 ()
  694. {
  695. File.GetCreationTime (" ");
  696. }
  697. [Test]
  698. [ExpectedException(typeof(ArgumentException))]
  699. public void GetCreationTimeException5 ()
  700. {
  701. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  702. }
  703. [Test]
  704. [ExpectedException(typeof(ArgumentNullException))]
  705. public void GetCreationTimeUtcException1 ()
  706. {
  707. File.GetCreationTimeUtc (null as string);
  708. }
  709. [Test]
  710. [ExpectedException(typeof(ArgumentException))]
  711. public void GetCreationTimeUtcException2 ()
  712. {
  713. File.GetCreationTimeUtc ("");
  714. }
  715. [Test]
  716. [ExpectedException(typeof(IOException))]
  717. public void GetCreationTimeUtcException3 ()
  718. {
  719. string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
  720. DeleteFile (path);
  721. File.GetCreationTimeUtc (path);
  722. }
  723. [Test]
  724. [ExpectedException(typeof(ArgumentException))]
  725. public void GetCreationTimeUtcException4 ()
  726. {
  727. File.GetCreationTimeUtc (" ");
  728. }
  729. [Test]
  730. [ExpectedException(typeof(ArgumentException))]
  731. public void GetCreationTimeUtcException5 ()
  732. {
  733. File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
  734. }
  735. [Test]
  736. [ExpectedException(typeof(ArgumentNullException))]
  737. public void GetLastAccessTimeException1 ()
  738. {
  739. File.GetLastAccessTime (null as string);
  740. }
  741. [Test]
  742. [ExpectedException(typeof(ArgumentException))]
  743. public void GetLastAccessTimeException2 ()
  744. {
  745. File.GetLastAccessTime ("");
  746. }
  747. [Test]
  748. [ExpectedException(typeof(IOException))]
  749. public void GetLastAccessTimeException3 ()
  750. {
  751. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
  752. DeleteFile (path);
  753. File.GetLastAccessTime (path);
  754. }
  755. [Test]
  756. [ExpectedException(typeof(ArgumentException))]
  757. public void GetLastAccessTimeException4 ()
  758. {
  759. File.GetLastAccessTime (" ");
  760. }
  761. [Test]
  762. [ExpectedException(typeof(ArgumentException))]
  763. public void GetLastAccessTimeException5 ()
  764. {
  765. File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
  766. }
  767. [Test]
  768. [ExpectedException(typeof(ArgumentNullException))]
  769. public void GetLastAccessTimeUtcException1 ()
  770. {
  771. File.GetLastAccessTimeUtc (null as string);
  772. }
  773. [Test]
  774. [ExpectedException(typeof(ArgumentException))]
  775. public void GetLastAccessTimeUtcException2 ()
  776. {
  777. File.GetLastAccessTimeUtc ("");
  778. }
  779. [Test]
  780. [ExpectedException(typeof(IOException))]
  781. public void GetLastAccessTimeUtcException3 ()
  782. {
  783. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  784. DeleteFile (path);
  785. File.GetLastAccessTimeUtc (path);
  786. }
  787. [Test]
  788. [ExpectedException(typeof(ArgumentException))]
  789. public void GetLastAccessTimeUtcException4 ()
  790. {
  791. File.GetLastAccessTimeUtc (" ");
  792. }
  793. [Test]
  794. [ExpectedException(typeof(ArgumentException))]
  795. public void GetLastAccessTimeUtcException5 ()
  796. {
  797. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  798. }
  799. [Test]
  800. [ExpectedException(typeof(ArgumentNullException))]
  801. public void GetLastWriteTimeException1 ()
  802. {
  803. File.GetLastWriteTime (null as string);
  804. }
  805. [Test]
  806. [ExpectedException(typeof(ArgumentException))]
  807. public void GetLastWriteTimeException2 ()
  808. {
  809. File.GetLastWriteTime ("");
  810. }
  811. [Test]
  812. [ExpectedException(typeof(IOException))]
  813. public void GetLastWriteTimeException3 ()
  814. {
  815. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
  816. DeleteFile (path);
  817. File.GetLastWriteTime (path);
  818. }
  819. [Test]
  820. [ExpectedException(typeof(ArgumentException))]
  821. public void GetLastWriteTimeException4 ()
  822. {
  823. File.GetLastWriteTime (" ");
  824. }
  825. [Test]
  826. [ExpectedException(typeof(ArgumentException))]
  827. public void GetLastWriteTimeException5 ()
  828. {
  829. File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
  830. }
  831. [Test]
  832. [ExpectedException(typeof(ArgumentNullException))]
  833. public void GetLastWriteTimeUtcException1 ()
  834. {
  835. File.GetLastWriteTimeUtc (null as string);
  836. }
  837. [Test]
  838. [ExpectedException(typeof(ArgumentException))]
  839. public void GetLastWriteTimeUtcException2 ()
  840. {
  841. File.GetLastAccessTimeUtc ("");
  842. }
  843. [Test]
  844. [ExpectedException(typeof(IOException))]
  845. public void GetLastWriteTimeUtcException3 ()
  846. {
  847. string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
  848. DeleteFile (path);
  849. File.GetLastAccessTimeUtc (path);
  850. }
  851. [Test]
  852. [ExpectedException(typeof(ArgumentException))]
  853. public void GetLastWriteTimeUtcException4 ()
  854. {
  855. File.GetLastAccessTimeUtc (" ");
  856. }
  857. [Test]
  858. [ExpectedException(typeof(ArgumentException))]
  859. public void GetLastWriteTimeUtcException5 ()
  860. {
  861. File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
  862. }
  863. [Test]
  864. [ExpectedException(typeof(IOException))]
  865. public void FileStreamCloseException ()
  866. {
  867. string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamCloseException";
  868. DeleteFile (path);
  869. FileStream stream = null;
  870. try {
  871. stream = File.Create (path);
  872. File.Delete (path);
  873. } finally {
  874. if (stream != null)
  875. stream.Close ();
  876. DeleteFile (path);
  877. }
  878. }
  879. [Test]
  880. public void FileStreamClose ()
  881. {
  882. string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
  883. FileStream stream = null;
  884. try {
  885. stream = File.Create (path);
  886. stream.Close ();
  887. File.Delete (path);
  888. } finally {
  889. if (stream != null)
  890. stream.Close ();
  891. DeleteFile (path);
  892. }
  893. }
  894. // SetCreationTime and SetCreationTimeUtc exceptions
  895. [Test]
  896. [ExpectedException(typeof (ArgumentNullException))]
  897. public void SetCreationTimeArgumentNullException1 ()
  898. {
  899. File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  900. }
  901. [Test]
  902. [ExpectedException(typeof (ArgumentException))]
  903. public void SetCreationTimeArgumenException1 ()
  904. {
  905. File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  906. }
  907. [Test]
  908. [ExpectedException(typeof (ArgumentException))]
  909. public void SetCreationTimeArgumenException2 ()
  910. {
  911. File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  912. }
  913. [Test]
  914. [ExpectedException(typeof (ArgumentException))]
  915. public void SetCreationTimeArgumenException3 ()
  916. {
  917. File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  918. }
  919. [Test]
  920. [ExpectedException(typeof (FileNotFoundException))]
  921. public void SetCreationTimeFileNotFoundException1 ()
  922. {
  923. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
  924. DeleteFile (path);
  925. File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  926. }
  927. // [Test]
  928. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  929. // public void SetCreationTimeArgumentOutOfRangeException1 ()
  930. // {
  931. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
  932. // FileStream stream = null;
  933. // DeleteFile (path);
  934. // try {
  935. // stream = File.Create (path);
  936. // stream.Close ();
  937. // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  938. // } finally {
  939. // if (stream != null)
  940. // stream.Close ();
  941. // DeleteFile (path);
  942. // }
  943. // }
  944. [Test]
  945. [ExpectedException(typeof (IOException))]
  946. public void SetCreationTimeIOException1 ()
  947. {
  948. string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
  949. DeleteFile (path);
  950. FileStream stream = null;
  951. try {
  952. stream = File.Create (path);
  953. File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  954. } finally {
  955. if (stream != null)
  956. stream.Close ();
  957. DeleteFile (path);
  958. }
  959. }
  960. [Test]
  961. [ExpectedException(typeof (ArgumentNullException))]
  962. public void SetCreationTimeUtcArgumentNullException1 ()
  963. {
  964. File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  965. }
  966. [Test]
  967. [ExpectedException(typeof (ArgumentException))]
  968. public void SetCreationTimeUtcArgumenException1 ()
  969. {
  970. File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  971. }
  972. [Test]
  973. [ExpectedException(typeof (ArgumentException))]
  974. public void SetCreationTimeUtcArgumenException2 ()
  975. {
  976. File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  977. }
  978. [Test]
  979. [ExpectedException(typeof (ArgumentException))]
  980. public void SetCreationTimeUtcArgumenException3 ()
  981. {
  982. File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  983. }
  984. [Test]
  985. [ExpectedException(typeof (FileNotFoundException))]
  986. public void SetCreationTimeUtcFileNotFoundException1 ()
  987. {
  988. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
  989. DeleteFile (path);
  990. File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  991. }
  992. // [Test]
  993. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  994. // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
  995. // {
  996. // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
  997. // DeleteFile (path);
  998. // FileStream stream = null;
  999. // try {
  1000. // stream = File.Create (path);
  1001. // stream.Close ();
  1002. // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1003. // } finally {
  1004. // if (stream != null)
  1005. // stream.Close();
  1006. // DeleteFile (path);
  1007. // }
  1008. // }
  1009. [Test]
  1010. [ExpectedException(typeof (IOException))]
  1011. public void SetCreationTimeUtcIOException1 ()
  1012. {
  1013. string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
  1014. DeleteFile (path);
  1015. FileStream stream = null;
  1016. try {
  1017. stream = File.Create (path);
  1018. File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1019. } finally {
  1020. if (stream != null)
  1021. stream.Close ();
  1022. DeleteFile (path);
  1023. }
  1024. }
  1025. // SetLastAccessTime and SetLastAccessTimeUtc exceptions
  1026. [Test]
  1027. [ExpectedException(typeof (ArgumentNullException))]
  1028. public void SetLastAccessTimeArgumentNullException1 ()
  1029. {
  1030. File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1031. }
  1032. [Test]
  1033. [ExpectedException(typeof (ArgumentException))]
  1034. public void SetLastAccessTimeArgumenException1 ()
  1035. {
  1036. File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1037. }
  1038. [Test]
  1039. [ExpectedException(typeof (ArgumentException))]
  1040. public void SetLastAccessTimeArgumenException2 ()
  1041. {
  1042. File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1043. }
  1044. [Test]
  1045. [ExpectedException(typeof (ArgumentException))]
  1046. public void SetLastAccessTimeArgumenException3 ()
  1047. {
  1048. File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1049. }
  1050. [Test]
  1051. [ExpectedException(typeof (FileNotFoundException))]
  1052. public void SetLastAccessTimeFileNotFoundException1 ()
  1053. {
  1054. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
  1055. DeleteFile (path);
  1056. File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1057. }
  1058. // [Test]
  1059. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1060. // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
  1061. // {
  1062. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
  1063. // DeleteFile (path);
  1064. // FileStream stream = null;
  1065. // try {
  1066. // stream = File.Create (path);
  1067. // stream.Close ();
  1068. // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1069. // } finally {
  1070. // if (stream != null)
  1071. // stream.Close ();
  1072. // DeleteFile (path);
  1073. // }
  1074. // }
  1075. [Test]
  1076. [ExpectedException(typeof (IOException))]
  1077. public void SetLastAccessTimeIOException1 ()
  1078. {
  1079. string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
  1080. DeleteFile (path);
  1081. FileStream stream = null;
  1082. try {
  1083. stream = File.Create (path);
  1084. File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1085. } finally {
  1086. if (stream != null)
  1087. stream.Close ();
  1088. DeleteFile (path);
  1089. }
  1090. }
  1091. [Test]
  1092. [ExpectedException(typeof (ArgumentNullException))]
  1093. public void SetLastAccessTimeUtcArgumentNullException1 ()
  1094. {
  1095. File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1096. }
  1097. [Test]
  1098. [ExpectedException(typeof (ArgumentException))]
  1099. public void SetCLastAccessTimeUtcArgumenException1 ()
  1100. {
  1101. File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1102. }
  1103. [Test]
  1104. [ExpectedException(typeof (ArgumentException))]
  1105. public void SetLastAccessTimeUtcArgumenException2 ()
  1106. {
  1107. File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1108. }
  1109. [Test]
  1110. [ExpectedException(typeof (ArgumentException))]
  1111. public void SetLastAccessTimeUtcArgumenException3 ()
  1112. {
  1113. File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1114. }
  1115. [Test]
  1116. [ExpectedException(typeof (FileNotFoundException))]
  1117. public void SetLastAccessTimeUtcFileNotFoundException1 ()
  1118. {
  1119. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
  1120. DeleteFile (path);
  1121. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1122. }
  1123. // [Test]
  1124. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1125. // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
  1126. // {
  1127. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
  1128. // DeleteFile (path);
  1129. // FileStream stream = null;
  1130. // try {
  1131. // stream = File.Create (path);
  1132. // stream.Close ();
  1133. // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1134. // } finally {
  1135. // if (stream != null)
  1136. // stream.Close ();
  1137. // DeleteFile (path);
  1138. // }
  1139. // }
  1140. [Test]
  1141. [ExpectedException(typeof (IOException))]
  1142. public void SetLastAccessTimeUtcIOException1 ()
  1143. {
  1144. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
  1145. DeleteFile (path);
  1146. FileStream stream = null;
  1147. try {
  1148. stream = File.Create (path);
  1149. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1150. } finally {
  1151. if (stream != null)
  1152. stream.Close ();
  1153. DeleteFile (path);
  1154. }
  1155. }
  1156. // SetLastWriteTime and SetLastWriteTimeUtc exceptions
  1157. [Test]
  1158. [ExpectedException(typeof (ArgumentNullException))]
  1159. public void SetLastWriteTimeArgumentNullException1 ()
  1160. {
  1161. File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1162. }
  1163. [Test]
  1164. [ExpectedException(typeof (ArgumentException))]
  1165. public void SetLastWriteTimeArgumenException1 ()
  1166. {
  1167. File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1168. }
  1169. [Test]
  1170. [ExpectedException(typeof (ArgumentException))]
  1171. public void SetLastWriteTimeArgumenException2 ()
  1172. {
  1173. File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1174. }
  1175. [Test]
  1176. [ExpectedException(typeof (ArgumentException))]
  1177. public void SetLastWriteTimeArgumenException3 ()
  1178. {
  1179. File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1180. }
  1181. [Test]
  1182. [ExpectedException(typeof (FileNotFoundException))]
  1183. public void SetLastWriteTimeFileNotFoundException1 ()
  1184. {
  1185. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
  1186. DeleteFile (path);
  1187. File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1188. }
  1189. // [Test]
  1190. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1191. // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
  1192. // {
  1193. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
  1194. // DeleteFile (path);
  1195. // FileStream stream = null;
  1196. // try {
  1197. // stream = File.Create (path);
  1198. // stream.Close ();
  1199. // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1200. // } finally {
  1201. // if (stream != null)
  1202. // stream.Close ();
  1203. // DeleteFile (path);
  1204. // }
  1205. // }
  1206. [Test]
  1207. [ExpectedException(typeof (IOException))]
  1208. public void SetLastWriteTimeIOException1 ()
  1209. {
  1210. string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
  1211. DeleteFile (path);
  1212. FileStream stream = null;
  1213. try {
  1214. stream = File.Create (path);
  1215. File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1216. } finally {
  1217. if (stream != null)
  1218. stream.Close ();
  1219. DeleteFile (path);
  1220. }
  1221. }
  1222. [Test]
  1223. [ExpectedException(typeof (ArgumentNullException))]
  1224. public void SetLastWriteTimeUtcArgumentNullException1 ()
  1225. {
  1226. File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
  1227. }
  1228. [Test]
  1229. [ExpectedException(typeof (ArgumentException))]
  1230. public void SetCLastWriteTimeUtcArgumenException1 ()
  1231. {
  1232. File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
  1233. }
  1234. [Test]
  1235. [ExpectedException(typeof (ArgumentException))]
  1236. public void SetLastWriteTimeUtcArgumenException2 ()
  1237. {
  1238. File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
  1239. }
  1240. [Test]
  1241. [ExpectedException(typeof (ArgumentException))]
  1242. public void SetLastWriteTimeUtcArgumenException3 ()
  1243. {
  1244. File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
  1245. }
  1246. [Test]
  1247. [ExpectedException(typeof (FileNotFoundException))]
  1248. public void SetLastWriteTimeUtcFileNotFoundException1 ()
  1249. {
  1250. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
  1251. DeleteFile (path);
  1252. File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
  1253. }
  1254. // [Test]
  1255. // [ExpectedException(typeof (ArgumentOutOfRangeException))]
  1256. // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
  1257. // {
  1258. // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
  1259. // DeleteFile (path);
  1260. // FileStream stream = null;
  1261. // try {
  1262. // stream = File.Create (path);
  1263. // stream.Close ();
  1264. // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1265. // } finally {
  1266. // if (stream != null)
  1267. // stream.Close ();
  1268. // DeleteFile (path);
  1269. // }
  1270. // }
  1271. //
  1272. [Test]
  1273. [ExpectedException(typeof (IOException))]
  1274. public void SetLastWriteTimeUtcIOException1 ()
  1275. {
  1276. string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
  1277. DeleteFile (path);
  1278. FileStream stream = null;
  1279. try {
  1280. stream = File.Create (path);
  1281. File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
  1282. } finally {
  1283. if (stream != null)
  1284. stream.Close ();
  1285. DeleteFile (path);
  1286. }
  1287. }
  1288. private void DeleteFile (string path)
  1289. {
  1290. if (File.Exists (path))
  1291. File.Delete (path);
  1292. }
  1293. }
  1294. }