FileTest.cs 49 KB

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