FileInfoTest.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. // FileInfoTest.cs - NUnit Test Cases for System.IO.FileInfo class
  2. //
  3. // Ville Palo ([email protected])
  4. //
  5. // (C) 2003 Ville Palo
  6. //
  7. using System;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.IO
  13. {
  14. [TestFixture]
  15. public class FileInfoTest
  16. {
  17. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  18. static readonly char DSC = Path.DirectorySeparatorChar;
  19. [SetUp]
  20. public void SetUp ()
  21. {
  22. DeleteDirectory (TempFolder);
  23. Directory.CreateDirectory (TempFolder);
  24. }
  25. [TearDown]
  26. public void TearDown ()
  27. {
  28. DeleteDirectory (TempFolder);
  29. }
  30. [Test] // ctor (String)
  31. public void Constructor1 ()
  32. {
  33. string path = TempFolder + DSC + "FIT.Ctr.Test";
  34. DeleteFile (path);
  35. FileInfo info = new FileInfo (path);
  36. Assert.IsTrue (info.DirectoryName.EndsWith (".Tests"), "#1");
  37. Assert.IsFalse (info.Exists, "#2");
  38. Assert.AreEqual (".Test", info.Extension, "#3");
  39. Assert.AreEqual ("FIT.Ctr.Test", info.Name, "#4");
  40. }
  41. [Test] // ctor (String)
  42. public void Constructor1_FileName_Null ()
  43. {
  44. try {
  45. new FileInfo (null);
  46. Assert.Fail ("#1");
  47. } catch (ArgumentNullException ex) {
  48. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  49. Assert.IsNull (ex.InnerException, "#3");
  50. Assert.IsNotNull (ex.Message, "#4");
  51. Assert.AreEqual ("fileName", ex.ParamName, "#5");
  52. }
  53. }
  54. [Test] // ctor (String)
  55. public void Constructor1_FileName_Empty ()
  56. {
  57. try {
  58. new FileInfo (string.Empty);
  59. Assert.Fail ("#1");
  60. } catch (ArgumentException ex) {
  61. // Empty file name is not legal
  62. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  63. Assert.IsNull (ex.InnerException, "#3");
  64. Assert.IsNotNull (ex.Message, "#4");
  65. Assert.IsNull (ex.ParamName, "#5");
  66. }
  67. }
  68. [Test] // ctor (String)
  69. public void Constructor1_FileName_InvalidPathChars ()
  70. {
  71. string path = string.Empty;
  72. foreach (char c in Path.InvalidPathChars)
  73. path += c;
  74. try {
  75. new FileInfo (path);
  76. } catch (ArgumentException ex) {
  77. // The path contains illegal characters
  78. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  79. Assert.IsNull (ex.InnerException, "#3");
  80. Assert.IsNotNull (ex.Message, "#4");
  81. Assert.IsNull (ex.ParamName, "#5");
  82. }
  83. }
  84. [Test] // ctor (String)
  85. public void Constructor1_FileName_Whitespace ()
  86. {
  87. try {
  88. new FileInfo (" ");
  89. Assert.Fail ("#1");
  90. } catch (ArgumentException ex) {
  91. // The path is not of a legal form
  92. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  93. Assert.IsNull (ex.InnerException, "#3");
  94. Assert.IsNotNull (ex.Message, "#4");
  95. Assert.IsNull (ex.ParamName, "#5");
  96. }
  97. }
  98. [Test]
  99. public void DirectoryTest ()
  100. {
  101. string path = TempFolder + DSC + "FIT.Directory.Test";
  102. DeleteFile (path);
  103. FileInfo info = new FileInfo (path);
  104. DirectoryInfo dir = info.Directory;
  105. Assert.AreEqual ("MonoTests.System.IO.Tests", dir.Name);
  106. }
  107. [Test]
  108. public void Exists ()
  109. {
  110. string path = TempFolder + DSC + "FIT.Exists.Test";
  111. DeleteFile (path);
  112. try {
  113. FileInfo info = new FileInfo (path);
  114. Assert.IsFalse (info.Exists, "#1");
  115. File.Create (path).Close ();
  116. Assert.IsFalse (info.Exists, "#2");
  117. info = new FileInfo (path);
  118. Assert.IsTrue (info.Exists, "#3");
  119. info = new FileInfo (TempFolder);
  120. Assert.IsFalse (info.Exists, "#4");
  121. } finally {
  122. DeleteFile (path);
  123. }
  124. }
  125. #if !MOBILE
  126. [Test]
  127. public void IsReadOnly ()
  128. {
  129. string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
  130. DeleteFile (path);
  131. try {
  132. using (FileStream stream = File.Create (path)) {
  133. stream.WriteByte (12);
  134. stream.Close ();
  135. }
  136. FileInfo info1 = new FileInfo (path);
  137. Assert.IsFalse (info1.IsReadOnly, "#1");
  138. FileInfo info2 = new FileInfo (path);
  139. info2.IsReadOnly = true;
  140. Assert.IsFalse (info1.IsReadOnly, "#2");
  141. Assert.IsTrue (info2.IsReadOnly, "#3");
  142. FileInfo info3 = new FileInfo (path);
  143. Assert.IsTrue (info3.IsReadOnly, "#4");
  144. info3.IsReadOnly = false;
  145. Assert.IsFalse (info1.IsReadOnly, "#4");
  146. Assert.IsTrue (info2.IsReadOnly, "#5");
  147. Assert.IsFalse (info3.IsReadOnly, "#6");
  148. } finally {
  149. File.SetAttributes (path, FileAttributes.Normal);
  150. DeleteFile (path);
  151. }
  152. }
  153. #endif
  154. [Test]
  155. public void Length ()
  156. {
  157. string path = TempFolder + DSC + "FIT.Length.Test";
  158. DeleteFile (path);
  159. try {
  160. FileStream stream = File.Create (path);
  161. FileInfo info = new FileInfo (path);
  162. Assert.AreEqual (0, info.Length, "#1");
  163. stream.WriteByte (12);
  164. stream.Flush ();
  165. Assert.AreEqual (0, info.Length, "#2");
  166. info = new FileInfo (path);
  167. Assert.AreEqual (1, info.Length, "#3");
  168. stream.Close ();
  169. } finally {
  170. DeleteFile (path);
  171. }
  172. }
  173. [Test]
  174. public void Length_FileDoesNotExist ()
  175. {
  176. string path = TempFolder + DSC + "FIT.LengthException.Test";
  177. DeleteFile (path);
  178. FileInfo info = new FileInfo (path);
  179. try {
  180. long l = info.Length;
  181. Assert.Fail ("#1:" + l);
  182. } catch (FileNotFoundException ex) {
  183. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  184. Assert.AreEqual (path, ex.FileName, "#3");
  185. Assert.IsNull (ex.InnerException, "#4");
  186. Assert.IsNotNull (ex.Message, "#5");
  187. }
  188. }
  189. [Test]
  190. public void AppendText ()
  191. {
  192. string path = TempFolder + DSC + "FIT.AppendText.Test";
  193. DeleteFile (path);
  194. try {
  195. FileInfo info = new FileInfo (path);
  196. Assert.IsFalse (info.Exists, "#1");
  197. StreamWriter writer = info.AppendText ();
  198. info = new FileInfo (path);
  199. Assert.IsTrue (info.Exists, "#2");
  200. writer.Write ("aaa");
  201. writer.Flush ();
  202. writer.Close ();
  203. Assert.AreEqual (0, info.Length, "#3");
  204. info = new FileInfo (path);
  205. Assert.AreEqual (3, info.Length, "#4");
  206. } finally {
  207. DeleteFile (path);
  208. }
  209. }
  210. [Test] // CopyTo (String)
  211. public void CopyTo1 ()
  212. {
  213. string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
  214. string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
  215. DeleteFile (path1);
  216. DeleteFile (path2);
  217. try {
  218. File.Create (path1).Close ();
  219. FileInfo info = new FileInfo (path1);
  220. Assert.IsTrue (info.Exists, "#1");
  221. FileInfo info2 = info.CopyTo (path2);
  222. info = new FileInfo (path1);
  223. Assert.IsTrue (info2.Exists, "#2");
  224. } finally {
  225. DeleteFile (path1);
  226. DeleteFile (path2);
  227. }
  228. }
  229. [Test] // CopyTo (String)
  230. public void CopyTo1_DestFileName_AlreadyExists ()
  231. {
  232. string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
  233. string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
  234. try {
  235. DeleteFile (path1);
  236. DeleteFile (path2);
  237. File.Create (path1).Close ();
  238. File.Create (path2).Close ();
  239. FileInfo info = new FileInfo (path1);
  240. try {
  241. info.CopyTo (path2);
  242. Assert.Fail ("#1");
  243. } catch (IOException ex) {
  244. // The file '...' already exists.
  245. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  246. Assert.IsNull (ex.InnerException, "#3");
  247. Assert.IsNotNull (ex.Message, "#4");
  248. Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
  249. }
  250. } finally {
  251. DeleteFile (path1);
  252. DeleteFile (path2);
  253. }
  254. }
  255. [Test] // CopyTo (String)
  256. public void CopyTo1_DestFileName_Null ()
  257. {
  258. string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
  259. DeleteFile (path);
  260. try {
  261. File.Create (path).Close ();
  262. FileInfo info = new FileInfo (path);
  263. try {
  264. info.CopyTo (null);
  265. Assert.Fail ("#1");
  266. } catch (ArgumentNullException ex) {
  267. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  268. Assert.IsNull (ex.InnerException, "#3");
  269. Assert.IsNotNull (ex.Message, "#4");
  270. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  271. }
  272. } finally {
  273. DeleteFile (path);
  274. }
  275. }
  276. [Test] // CopyTo (String)
  277. public void CopyTo1_DestFileName_Empty ()
  278. {
  279. string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
  280. DeleteFile (path);
  281. try {
  282. File.Create (path).Close ();
  283. FileInfo info = new FileInfo (path);
  284. try {
  285. info.CopyTo (string.Empty);
  286. Assert.Fail ("#1");
  287. } catch (ArgumentException ex) {
  288. // Empty file name is not legal
  289. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  290. Assert.IsNull (ex.InnerException, "#3");
  291. Assert.IsNotNull (ex.Message, "#4");
  292. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  293. }
  294. } finally {
  295. DeleteFile (path);
  296. }
  297. }
  298. [Test] // CopyTo (String)
  299. public void CopyTo1_DestFileName_Whitespace ()
  300. {
  301. string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
  302. DeleteFile (path);
  303. try {
  304. File.Create (path).Close ();
  305. FileInfo info = new FileInfo (path);
  306. try {
  307. info.CopyTo (" ");
  308. Assert.Fail ("#1");
  309. } catch (ArgumentException ex) {
  310. // The path is not of a legal form
  311. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  312. Assert.IsNotNull (ex.Message, "#4");
  313. Assert.IsNull (ex.ParamName, "#5");
  314. }
  315. } finally {
  316. DeleteFile (path);
  317. }
  318. }
  319. [Test] // CopyTo (String)
  320. public void CopyTo1_DestFileName_InvalidPathChars ()
  321. {
  322. string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
  323. string path2 = string.Empty;
  324. DeleteFile (path);
  325. try {
  326. File.Create (path).Close ();
  327. FileInfo info = new FileInfo (path);
  328. foreach (char c in Path.InvalidPathChars)
  329. path2 += c;
  330. try {
  331. info.CopyTo (path2);
  332. Assert.Fail ("#1");
  333. } catch (ArgumentException ex) {
  334. // Illegal characters in path
  335. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  336. Assert.IsNull (ex.InnerException, "#3");
  337. Assert.IsNotNull (ex.Message, "#4");
  338. Assert.IsNull (ex.ParamName, "#5");
  339. }
  340. } finally {
  341. DeleteFile (path);
  342. }
  343. }
  344. [Test] // CopyTo (String, Boolean)
  345. public void CopyTo2 ()
  346. {
  347. string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
  348. string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
  349. DeleteFile (path1);
  350. DeleteFile (path2);
  351. try {
  352. File.Create (path1).Close ();
  353. File.Create (path2).Close ();
  354. FileInfo info = new FileInfo (path1);
  355. FileInfo info2 = info.CopyTo (path2, true);
  356. info = new FileInfo (path1);
  357. Assert.IsTrue (info.Exists, "#1");
  358. Assert.IsTrue (info2.Exists, "#2");
  359. } finally {
  360. DeleteFile (path1);
  361. DeleteFile (path2);
  362. }
  363. }
  364. [Test] // CopyTo (String, Boolean)
  365. public void CopyTo2_DestFileName_AlreadyExists ()
  366. {
  367. string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
  368. string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
  369. try {
  370. DeleteFile (path1);
  371. DeleteFile (path2);
  372. File.Create (path1).Close ();
  373. File.Create (path2).Close ();
  374. FileInfo info = new FileInfo (path1);
  375. try {
  376. info.CopyTo (path2, false);
  377. Assert.Fail ("#1");
  378. } catch (IOException ex) {
  379. // The file '...' already exists.
  380. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  381. Assert.IsNull (ex.InnerException, "#3");
  382. Assert.IsNotNull (ex.Message, "#4");
  383. Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
  384. }
  385. } finally {
  386. DeleteFile (path1);
  387. DeleteFile (path2);
  388. }
  389. }
  390. [Test] // CopyTo (String, Boolean)
  391. public void CopyTo2_DestFileName_Null ()
  392. {
  393. string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
  394. DeleteFile (path);
  395. try {
  396. File.Create (path).Close ();
  397. FileInfo info = new FileInfo (path);
  398. try {
  399. info.CopyTo (null, false);
  400. Assert.Fail ("#1");
  401. } catch (ArgumentNullException ex) {
  402. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  403. Assert.IsNull (ex.InnerException, "#3");
  404. Assert.IsNotNull (ex.Message, "#4");
  405. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  406. }
  407. } finally {
  408. DeleteFile (path);
  409. }
  410. }
  411. [Test] // CopyTo (String, Boolean)
  412. public void CopyTo2_DestFileName_Empty ()
  413. {
  414. string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
  415. DeleteFile (path);
  416. try {
  417. File.Create (path).Close ();
  418. FileInfo info = new FileInfo (path);
  419. try {
  420. info.CopyTo (string.Empty, false);
  421. Assert.Fail ("#1");
  422. } catch (ArgumentException ex) {
  423. // Empty file name is not legal
  424. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  425. Assert.IsNull (ex.InnerException, "#3");
  426. Assert.IsNotNull (ex.Message, "#4");
  427. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  428. }
  429. } finally {
  430. DeleteFile (path);
  431. }
  432. }
  433. [Test] // CopyTo (String, Boolean)
  434. public void CopyTo2_DestFileName_Whitespace ()
  435. {
  436. string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
  437. DeleteFile (path);
  438. try {
  439. File.Create (path).Close ();
  440. FileInfo info = new FileInfo (path);
  441. try {
  442. info.CopyTo (" ", false);
  443. Assert.Fail ("#1");
  444. } catch (ArgumentException ex) {
  445. // The path is not of a legal form
  446. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  447. Assert.IsNull (ex.InnerException, "#3");
  448. Assert.IsNotNull (ex.Message, "#4");
  449. Assert.IsNull (ex.ParamName, "#5");
  450. }
  451. } finally {
  452. DeleteFile (path);
  453. }
  454. }
  455. [Test] // CopyTo (String, Boolean)
  456. public void CopyTo2_DestFileName_InvalidPathChars ()
  457. {
  458. string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
  459. string path2 = string.Empty;
  460. DeleteFile (path);
  461. try {
  462. File.Create (path).Close ();
  463. FileInfo info = new FileInfo (path);
  464. foreach (char c in Path.InvalidPathChars)
  465. path2 += c;
  466. try {
  467. info.CopyTo (path2, false);
  468. Assert.Fail ("#1");
  469. } catch (ArgumentException ex) {
  470. // Illegal characters in path
  471. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  472. Assert.IsNull (ex.InnerException, "#3");
  473. Assert.IsNotNull (ex.Message, "#4");
  474. Assert.IsNull (ex.ParamName, "#5");
  475. }
  476. } finally {
  477. DeleteFile (path);
  478. }
  479. }
  480. [Test]
  481. public void Create ()
  482. {
  483. string path = TempFolder + DSC + "FIT.Create.Test";
  484. DeleteFile (path);
  485. try {
  486. FileInfo info = new FileInfo (path);
  487. Assert.IsFalse (info.Exists, "#1");
  488. FileStream stream = info.Create ();
  489. Assert.IsFalse (info.Exists, "#2");
  490. info = new FileInfo (path);
  491. Assert.IsTrue (info.Exists, "#3");
  492. Assert.IsTrue (stream.CanRead, "#4");
  493. Assert.IsTrue (stream.CanWrite, "#5");
  494. Assert.IsTrue (stream.CanSeek, "#6");
  495. stream.Close ();
  496. } finally {
  497. DeleteFile (path);
  498. }
  499. }
  500. [Test]
  501. public void CreateText ()
  502. {
  503. string path = TempFolder + DSC + "FIT.CreateText.Test";
  504. DeleteFile (path);
  505. try {
  506. FileInfo info = new FileInfo (path);
  507. Assert.IsFalse (info.Exists, "#1");
  508. StreamWriter writer = info.CreateText ();
  509. writer.WriteLine ("test");
  510. writer.Close ();
  511. info = new FileInfo (path);
  512. Assert.IsTrue (info.Exists, "#2");
  513. } finally {
  514. DeleteFile (path);
  515. }
  516. }
  517. [Test]
  518. public void CreateText_Directory ()
  519. {
  520. FileInfo info = new FileInfo (TempFolder);
  521. try {
  522. info.CreateText ();
  523. Assert.Fail ("#1");
  524. } catch (UnauthorizedAccessException ex) {
  525. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  526. Assert.IsNull (ex.InnerException, "#3");
  527. Assert.IsNotNull (ex.Message, "#4");
  528. }
  529. }
  530. [Test]
  531. public void Delete ()
  532. {
  533. string path = TempFolder + DSC + "FIT.Delete.Test";
  534. DeleteFile (path);
  535. try {
  536. FileInfo info = new FileInfo (path);
  537. Assert.IsFalse (info.Exists, "#1");
  538. info.Delete ();
  539. Assert.IsFalse (info.Exists, "#1a");
  540. info.Create ().Close ();
  541. info = new FileInfo (path);
  542. Assert.IsTrue (info.Exists, "#2");
  543. info.Delete ();
  544. Assert.IsTrue (info.Exists, "#3");
  545. info = new FileInfo (path);
  546. Assert.IsFalse (info.Exists, "#4");
  547. } finally {
  548. DeleteFile (path);
  549. }
  550. }
  551. [Test]
  552. public void Delete_Directory ()
  553. {
  554. FileInfo info = new FileInfo (TempFolder);
  555. try {
  556. info.Delete ();
  557. Assert.Fail ("#1");
  558. } catch (UnauthorizedAccessException ex) {
  559. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  560. Assert.IsNotNull (ex.Message, "#4");
  561. }
  562. }
  563. [Test]
  564. public void MoveTo ()
  565. {
  566. string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
  567. string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
  568. DeleteFile (path1);
  569. DeleteFile (path2);
  570. try {
  571. File.Create (path1).Close ();
  572. FileInfo info1 = new FileInfo (path1);
  573. FileInfo info2 = new FileInfo (path2);
  574. Assert.IsTrue (info1.Exists, "#A1");
  575. Assert.AreEqual (path1, info1.FullName, "#A2");
  576. Assert.IsFalse (info2.Exists, "#A3");
  577. Assert.AreEqual (path2, info2.FullName, "#A4");
  578. info1.MoveTo (path2);
  579. info2 = new FileInfo (path2);
  580. Assert.IsTrue (info1.Exists, "#B1");
  581. Assert.AreEqual (path2, info1.FullName, "#B2");
  582. Assert.IsTrue (info2.Exists, "#B3");
  583. Assert.AreEqual (path2, info2.FullName, "#B4");
  584. } finally {
  585. DeleteFile (path1);
  586. DeleteFile (path2);
  587. }
  588. }
  589. [Test] //Covers #18361
  590. public void MoveTo_SameName ()
  591. {
  592. string name = "FIT.MoveTo.SameName.Test";
  593. string path1 = TempFolder + DSC + name;
  594. string path2 = TempFolder + DSC + "same";
  595. Directory.CreateDirectory (path2);
  596. path2 += DSC + name;
  597. DeleteFile (path1);
  598. DeleteFile (path2);
  599. try {
  600. File.Create (path1).Close ();
  601. FileInfo info1 = new FileInfo (path1);
  602. FileInfo info2 = new FileInfo (path2);
  603. Assert.IsTrue (info1.Exists, "#A1");
  604. Assert.IsFalse (info2.Exists, "#A2");
  605. info1.MoveTo (path2);
  606. info1 = new FileInfo (path1);
  607. info2 = new FileInfo (path2);
  608. Assert.IsFalse (info1.Exists, "#B1");
  609. Assert.IsTrue (info2.Exists, "#B2");
  610. } finally {
  611. DeleteFile (path1);
  612. DeleteFile (path2);
  613. }
  614. }
  615. [Test]
  616. public void MoveTo_DestFileName_AlreadyExists ()
  617. {
  618. string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
  619. string destFile;
  620. FileInfo info;
  621. // move to same directory
  622. File.Create (sourceFile).Close ();
  623. info = new FileInfo (sourceFile);
  624. try {
  625. info.MoveTo (TempFolder);
  626. Assert.Fail ("#A1");
  627. } catch (IOException ex) {
  628. // Cannot create a file when that file already exists
  629. Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
  630. Assert.IsNull (ex.InnerException, "#A3");
  631. Assert.IsNotNull (ex.Message, "#A4");
  632. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
  633. } finally {
  634. DeleteFile (sourceFile);
  635. }
  636. // move to exist file
  637. File.Create (sourceFile).Close ();
  638. destFile = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
  639. File.Create (destFile).Close ();
  640. info = new FileInfo (sourceFile);
  641. try {
  642. info.MoveTo (destFile);
  643. Assert.Fail ("#B1");
  644. } catch (IOException ex) {
  645. // Cannot create a file when that file already exists
  646. Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
  647. Assert.IsNull (ex.InnerException, "#B3");
  648. Assert.IsNotNull (ex.Message, "#B4");
  649. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
  650. } finally {
  651. DeleteFile (sourceFile);
  652. DeleteFile (destFile);
  653. }
  654. // move to existing directory
  655. File.Create (sourceFile).Close ();
  656. destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
  657. Directory.CreateDirectory (destFile);
  658. info = new FileInfo (sourceFile);
  659. try {
  660. info.MoveTo (destFile);
  661. Assert.Fail ("#C1");
  662. } catch (IOException ex) {
  663. // Cannot create a file when that file already exists
  664. Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
  665. Assert.IsNull (ex.InnerException, "#C3");
  666. Assert.IsNotNull (ex.Message, "#C4");
  667. Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
  668. } finally {
  669. DeleteFile (sourceFile);
  670. DeleteDirectory (destFile);
  671. }
  672. }
  673. [Test]
  674. [Category ("NotWasm")]
  675. public void MoveTo_DestFileName_DirectoryDoesNotExist ()
  676. {
  677. string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
  678. string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
  679. DeleteFile (sourceFile);
  680. try {
  681. File.Create (sourceFile).Close ();
  682. FileInfo info = new FileInfo (sourceFile);
  683. try {
  684. info.MoveTo (destFile);
  685. Assert.Fail ("#1");
  686. } catch (DirectoryNotFoundException ex) {
  687. // Could not find a part of the path
  688. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
  689. Assert.IsNull (ex.InnerException, "#3");
  690. Assert.IsNotNull (ex.Message, "#4");
  691. }
  692. } finally {
  693. DeleteFile (sourceFile);
  694. }
  695. }
  696. [Test]
  697. public void MoveTo_DestFileName_Null ()
  698. {
  699. string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
  700. DeleteFile (path);
  701. try {
  702. File.Create (path).Close ();
  703. FileInfo info = new FileInfo (path);
  704. try {
  705. info.MoveTo (null);
  706. Assert.Fail ("#1");
  707. } catch (ArgumentNullException ex) {
  708. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  709. Assert.IsNull (ex.InnerException, "#3");
  710. Assert.IsNotNull (ex.Message, "#4");
  711. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  712. }
  713. } finally {
  714. DeleteFile (path);
  715. }
  716. }
  717. [Test]
  718. public void MoveTo_DestFileName_Empty ()
  719. {
  720. string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
  721. DeleteFile (path);
  722. try {
  723. File.Create (path).Close ();
  724. FileInfo info = new FileInfo (path);
  725. try {
  726. info.MoveTo (string.Empty);
  727. Assert.Fail ("#1");
  728. } catch (ArgumentException ex) {
  729. // Empty file name is not legal
  730. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  731. Assert.IsNull (ex.InnerException, "#3");
  732. Assert.IsNotNull (ex.Message, "#4");
  733. Assert.AreEqual ("destFileName", ex.ParamName, "#5");
  734. }
  735. } finally {
  736. DeleteFile (path);
  737. }
  738. }
  739. [Test]
  740. public void MoveTo_DestFileName_Whitespace ()
  741. {
  742. string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
  743. DeleteFile (path);
  744. try {
  745. File.Create (path).Close ();
  746. FileInfo info = new FileInfo (path);
  747. try {
  748. info.MoveTo (" ");
  749. Assert.Fail ("#1");
  750. } catch (ArgumentException ex) {
  751. // The path is not of a legal form
  752. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  753. Assert.IsNull (ex.InnerException, "#3");
  754. Assert.IsNotNull (ex.Message, "#4");
  755. Assert.IsNull (ex.ParamName, "#5");
  756. }
  757. } finally {
  758. DeleteFile (path);
  759. }
  760. }
  761. [Test]
  762. public void MoveTo_FileDoesNotExist ()
  763. {
  764. string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
  765. string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
  766. DeleteFile (path1);
  767. DeleteFile (path2);
  768. try {
  769. FileInfo info = new FileInfo (path1);
  770. try {
  771. info.MoveTo (path2);
  772. Assert.Fail ("#1");
  773. } catch (FileNotFoundException ex) {
  774. // Unable to find the specified file
  775. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  776. Assert.IsNull (ex.InnerException, "#3");
  777. Assert.IsNotNull (ex.Message, "#4");
  778. }
  779. } finally {
  780. DeleteFile (path1);
  781. DeleteFile (path2);
  782. }
  783. }
  784. [Test]
  785. public void MoveTo_Same ()
  786. {
  787. string path = TempFolder + DSC + "FIT.MoveToSame.Test";
  788. DeleteFile (path);
  789. try {
  790. File.Create (path).Close ();
  791. FileInfo info = new FileInfo (path);
  792. info.MoveTo (path);
  793. Assert.IsTrue (info.Exists, "#1");
  794. Assert.IsTrue (File.Exists (path), "#2");
  795. } finally {
  796. DeleteFile (path);
  797. }
  798. }
  799. [Test] //Covers #38796
  800. public void ToStringAfterMoveTo ()
  801. {
  802. string name1 = "FIT.ToStringAfterMoveTo.Test";
  803. string name2 = "FIT.ToStringAfterMoveTo.Test.Alt";
  804. string path1 = TempFolder + DSC + name1;
  805. string path2 = TempFolder + DSC + name2;
  806. DeleteFile (path1);
  807. DeleteFile (path2);
  808. try {
  809. File.Create (path1).Close ();
  810. FileInfo info = new FileInfo (path1);
  811. Assert.AreEqual (path1, info.ToString (), "#A");
  812. info.MoveTo (path2);
  813. Assert.AreEqual (path2, info.ToString (), "#B");
  814. } finally {
  815. DeleteFile (path1);
  816. DeleteFile (path2);
  817. }
  818. }
  819. #if !MOBILE
  820. [Test]
  821. public void Replace1 ()
  822. {
  823. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  824. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  825. string path3 = TempFolder + DSC + "FIT.Replace.Back.Test";
  826. DeleteFile (path1);
  827. DeleteFile (path2);
  828. DeleteFile (path3);
  829. try {
  830. File.Create (path1).Close ();
  831. File.Create (path2).Close ();
  832. File.Create (path3).Close ();
  833. FileInfo info = new FileInfo (path1);
  834. Assert.IsTrue (info.Exists, "#1");
  835. FileInfo info2 = info.Replace (path2, path3);
  836. Assert.IsTrue (info2.Exists, "#2");
  837. FileInfo info3 = new FileInfo (path3);
  838. Assert.IsTrue (info3.Exists, "#3");
  839. } finally {
  840. DeleteFile (path2);
  841. DeleteFile (path3);
  842. }
  843. }
  844. [Test]
  845. public void Replace1_Backup_Null ()
  846. {
  847. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  848. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  849. DeleteFile (path1);
  850. DeleteFile (path2);
  851. try {
  852. File.Create (path1).Close ();
  853. File.Create (path2).Close ();
  854. FileInfo info = new FileInfo (path1);
  855. Assert.IsTrue (info.Exists, "#1");
  856. FileInfo info2 = info.Replace (path2, null);
  857. Assert.IsTrue (info2.Exists, "#2");
  858. info = new FileInfo (path1);
  859. Assert.IsFalse (info.Exists, "#3");
  860. } finally {
  861. DeleteFile (path2);
  862. }
  863. }
  864. [Test]
  865. public void Replace1_DestFileName_Null ()
  866. {
  867. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  868. DeleteFile (path1);
  869. try {
  870. try {
  871. File.Create (path1).Close ();
  872. FileInfo info = new FileInfo (path1);
  873. info.Replace (null, null);
  874. Assert.Fail ("#1");
  875. } catch (ArgumentNullException ex) {
  876. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  877. Assert.IsNull (ex.InnerException, "#3");
  878. Assert.IsNotNull (ex.Message, "#4");
  879. }
  880. } finally {
  881. DeleteFile (path1);
  882. }
  883. }
  884. [Test]
  885. public void Replace1_DestFileName_Empty ()
  886. {
  887. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  888. DeleteFile (path1);
  889. try {
  890. try {
  891. File.Create (path1).Close ();
  892. FileInfo info = new FileInfo (path1);
  893. info.Replace (string.Empty, null);
  894. Assert.Fail ("#1");
  895. } catch (ArgumentException ex) {
  896. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  897. Assert.IsNull (ex.InnerException, "#3");
  898. Assert.IsNotNull (ex.Message, "#4");
  899. }
  900. } finally {
  901. DeleteFile (path1);
  902. }
  903. }
  904. [Test]
  905. public void Replace1_DestFileName_WhiteSpace ()
  906. {
  907. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  908. DeleteFile (path1);
  909. try {
  910. try {
  911. File.Create (path1).Close ();
  912. FileInfo info = new FileInfo (path1);
  913. info.Replace (" ", null);
  914. Assert.Fail ("#1");
  915. } catch (ArgumentException ex) {
  916. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  917. Assert.IsNull (ex.InnerException, "#3");
  918. Assert.IsNotNull (ex.Message, "#4");
  919. }
  920. } finally {
  921. DeleteFile (path1);
  922. }
  923. }
  924. [Test]
  925. public void Replace1_DestFileName_InvalidPathChars ()
  926. {
  927. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  928. string path2 = string.Empty;
  929. DeleteFile (path1);
  930. try {
  931. File.Create (path1).Close ();
  932. FileInfo info = new FileInfo (path1);
  933. foreach (char c in Path.InvalidPathChars)
  934. path2 += c;
  935. try {
  936. info.Replace (path2, null);
  937. Assert.Fail ("#1");
  938. } catch (ArgumentException ex) {
  939. // Illegal characters in path
  940. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  941. Assert.IsNull (ex.InnerException, "#3");
  942. Assert.IsNotNull (ex.Message, "#4");
  943. Assert.IsNull (ex.ParamName, "#5");
  944. }
  945. } finally {
  946. DeleteFile (path1);
  947. }
  948. }
  949. [Test]
  950. public void Replace1_DestFileName_FileNotFound ()
  951. {
  952. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  953. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  954. DeleteFile (path1);
  955. DeleteFile (path2);
  956. try {
  957. try {
  958. File.Create (path1).Close ();
  959. FileInfo info = new FileInfo (path1);
  960. info.Replace (path2, null);
  961. Assert.Fail ("#1");
  962. } catch (FileNotFoundException ex) {
  963. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  964. Assert.IsNull (ex.InnerException, "#3");
  965. Assert.IsNotNull (ex.Message, "#4");
  966. }
  967. } finally {
  968. DeleteFile (path1);
  969. }
  970. }
  971. [Test]
  972. public void Replace1_Source_FileNotFound ()
  973. {
  974. string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
  975. string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
  976. DeleteFile (path2);
  977. try {
  978. try {
  979. File.Create (path2).Close ();
  980. FileInfo info = new FileInfo (path1);
  981. info.Replace (path2, null);
  982. Assert.Fail ("#1");
  983. } catch (FileNotFoundException ex) {
  984. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  985. Assert.IsNull (ex.InnerException, "#3");
  986. Assert.IsNotNull (ex.Message, "#4");
  987. }
  988. } finally {
  989. DeleteFile (path2);
  990. }
  991. }
  992. #endif
  993. [Test]
  994. public void Open ()
  995. {
  996. string path = TempFolder + DSC + "FIT.Open.Test";
  997. DeleteFile (path);
  998. FileStream stream = null;
  999. try {
  1000. FileInfo info = new FileInfo (path);
  1001. stream = info.Open (FileMode.CreateNew);
  1002. Assert.IsTrue (stream.CanRead, "#A1");
  1003. Assert.IsTrue (stream.CanSeek, "#A2");
  1004. Assert.IsTrue (stream.CanWrite, "#A3");
  1005. stream.Close ();
  1006. stream = info.Open (FileMode.Open);
  1007. Assert.IsTrue (stream.CanRead, "#B1");
  1008. Assert.IsTrue (stream.CanSeek, "#B2");
  1009. Assert.IsTrue (stream.CanWrite, "#B3");
  1010. stream.Close ();
  1011. stream = info.Open (FileMode.Append, FileAccess.Write);
  1012. Assert.IsFalse (stream.CanRead, "#C1");
  1013. Assert.IsTrue (stream.CanSeek, "#C2");
  1014. Assert.IsTrue (stream.CanWrite, "#C3");
  1015. stream.Close ();
  1016. stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  1017. Assert.IsTrue (stream.CanRead, "#D1");
  1018. Assert.IsTrue (stream.CanSeek, "#D2");
  1019. Assert.IsTrue (stream.CanWrite, "#D3");
  1020. stream.Close ();
  1021. } finally {
  1022. if (stream != null)
  1023. stream.Close ();
  1024. DeleteFile (path);
  1025. }
  1026. }
  1027. [Test]
  1028. public void Open_FileDoesNotExist ()
  1029. {
  1030. string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
  1031. DeleteFile (path);
  1032. FileInfo info = new FileInfo (path);
  1033. try {
  1034. info.Open (FileMode.Open);
  1035. Assert.Fail ("#1");
  1036. } catch (FileNotFoundException ex) {
  1037. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1038. Assert.AreEqual (path, ex.FileName, "#3");
  1039. Assert.IsNull (ex.InnerException, "#4");
  1040. Assert.IsNotNull (ex.Message, "#5");
  1041. }
  1042. }
  1043. [Test]
  1044. public void OpenRead ()
  1045. {
  1046. string path = TempFolder + DSC + "FIT.OpenRead.Test";
  1047. DeleteFile (path);
  1048. FileStream stream = null;
  1049. try {
  1050. File.Create (path).Close ();
  1051. FileInfo info = new FileInfo (path);
  1052. stream = info.OpenRead ();
  1053. Assert.IsTrue (stream.CanRead, "#1");
  1054. Assert.IsTrue (stream.CanSeek, "#2");
  1055. Assert.IsFalse (stream.CanWrite, "#3");
  1056. stream.Close ();
  1057. } finally {
  1058. if (stream != null)
  1059. stream.Close ();
  1060. DeleteFile (path);
  1061. }
  1062. }
  1063. [Test]
  1064. public void OpenRead_FileLock ()
  1065. {
  1066. string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
  1067. DeleteFile (path);
  1068. FileStream stream = null;
  1069. try {
  1070. stream = File.Create (path);
  1071. FileInfo info = new FileInfo (path);
  1072. try {
  1073. info.OpenRead ();
  1074. Assert.Fail ("#1");
  1075. } catch (IOException ex) {
  1076. // The process cannot access the file because
  1077. // it is being used by another process
  1078. Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
  1079. Assert.IsNull (ex.InnerException, "#3");
  1080. Assert.IsNotNull (ex.Message, "#4");
  1081. }
  1082. } finally {
  1083. if (stream != null)
  1084. stream.Close ();
  1085. DeleteFile (path);
  1086. }
  1087. }
  1088. [Test]
  1089. public void OpenRead_Directory ()
  1090. {
  1091. FileInfo info = new FileInfo (TempFolder);
  1092. try {
  1093. info.OpenRead ();
  1094. Assert.Fail ("#1");
  1095. } catch (UnauthorizedAccessException ex) {
  1096. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  1097. Assert.IsNull (ex.InnerException, "#3");
  1098. Assert.IsNotNull (ex.Message, "#4");
  1099. }
  1100. }
  1101. [Test]
  1102. public void OpenText ()
  1103. {
  1104. string path = TempFolder + DSC + "FIT.OpenText.Test";
  1105. DeleteFile (path);
  1106. StreamReader reader = null;
  1107. try {
  1108. File.Create (path).Close ();
  1109. FileInfo info = new FileInfo (path);
  1110. reader = info.OpenText ();
  1111. Assert.AreEqual (-1, reader.Peek ());
  1112. } finally {
  1113. if (reader != null)
  1114. reader.Close ();
  1115. DeleteFile (path);
  1116. }
  1117. }
  1118. [Test]
  1119. public void OpenText_FileDoesNotExist ()
  1120. {
  1121. string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
  1122. DeleteFile (path);
  1123. FileInfo info = new FileInfo (path);
  1124. try {
  1125. info.OpenText ();
  1126. Assert.Fail ("#1");
  1127. } catch (FileNotFoundException ex) {
  1128. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
  1129. Assert.AreEqual (path, ex.FileName, "#3");
  1130. Assert.IsNull (ex.InnerException, "#4");
  1131. Assert.IsNotNull (ex.Message, "#5");
  1132. }
  1133. }
  1134. [Test]
  1135. public void OpenText_Directory ()
  1136. {
  1137. FileInfo info = new FileInfo (TempFolder);
  1138. try {
  1139. info.OpenText ();
  1140. Assert.Fail ("#1");
  1141. } catch (UnauthorizedAccessException ex) {
  1142. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  1143. Assert.IsNull (ex.InnerException, "#3");
  1144. Assert.IsNotNull (ex.Message, "#4");
  1145. }
  1146. }
  1147. [Test]
  1148. public void OpenWrite ()
  1149. {
  1150. string path = TempFolder + DSC + "FIT.OpenWrite.Test";
  1151. DeleteFile (path);
  1152. FileStream stream = null;
  1153. try {
  1154. File.Create (path).Close ();
  1155. FileInfo info = new FileInfo (path);
  1156. stream = info.OpenWrite ();
  1157. Assert.IsFalse (stream.CanRead, "#1");
  1158. Assert.IsTrue (stream.CanSeek, "#2");
  1159. Assert.IsTrue (stream.CanWrite, "#3");
  1160. } finally {
  1161. if (stream != null)
  1162. stream.Close ();
  1163. DeleteFile (path);
  1164. }
  1165. }
  1166. [Test]
  1167. public void OpenWrite_Directory ()
  1168. {
  1169. FileInfo info = new FileInfo (TempFolder);
  1170. try {
  1171. info.OpenWrite ();
  1172. Assert.Fail ("#1");
  1173. } catch (UnauthorizedAccessException ex) {
  1174. Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
  1175. Assert.IsNull (ex.InnerException, "#3");
  1176. Assert.IsNotNull (ex.Message, "#4");
  1177. }
  1178. }
  1179. #if !MOBILE
  1180. [Test]
  1181. public void Serialization ()
  1182. {
  1183. FileInfo info;
  1184. SerializationInfo si;
  1185. info = new FileInfo ("Test");
  1186. si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
  1187. info.GetObjectData (si, new StreamingContext ());
  1188. Assert.AreEqual (3, si.MemberCount, "#A1");
  1189. Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
  1190. Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
  1191. info = new FileInfo (TempFolder);
  1192. si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
  1193. info.GetObjectData (si, new StreamingContext ());
  1194. Assert.AreEqual (3, si.MemberCount, "#B1");
  1195. Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
  1196. Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
  1197. }
  1198. [Test]
  1199. public void Deserialization ()
  1200. {
  1201. FileInfo info = new FileInfo ("Test");
  1202. MemoryStream ms = new MemoryStream ();
  1203. BinaryFormatter bf = new BinaryFormatter ();
  1204. bf.Serialize (ms, info);
  1205. ms.Position = 0;
  1206. FileInfo clone = (FileInfo) bf.Deserialize (ms);
  1207. Assert.AreEqual (info.Name, clone.Name, "#1");
  1208. Assert.AreEqual (info.FullName, clone.FullName, "#2");
  1209. }
  1210. #endif
  1211. [Test]
  1212. [Category ("MobileNotWorking")]
  1213. public void ToStringVariety ()
  1214. {
  1215. Assert.AreEqual ("foo", new FileInfo ("foo").ToString ());
  1216. Assert.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
  1217. Assert.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
  1218. Assert.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
  1219. Assert.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
  1220. Assert.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
  1221. Assert.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
  1222. Assert.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
  1223. }
  1224. void DeleteFile (string path)
  1225. {
  1226. if (File.Exists (path))
  1227. File.Delete (path);
  1228. }
  1229. void DeleteDirectory (string path)
  1230. {
  1231. if (Directory.Exists (path))
  1232. Directory.Delete (path, true);
  1233. }
  1234. }
  1235. }