PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Helpers/DirectoryHelper.cs

#
C# | 803 lines | 498 code | 91 blank | 214 comment | 39 complexity | 5b0d4aa036343df81646d165e75a79c3 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using NUnit.Framework;
  6. namespace Delta.Utilities.Helpers
  7. {
  8. /// <summary>
  9. /// Directory helper
  10. /// </summary>
  11. public static class DirectoryHelper
  12. {
  13. #region Delegates
  14. internal delegate bool ExistsDelegate(string directory);
  15. /// <summary>
  16. /// Delegate definition for getting the base directory.
  17. /// </summary>
  18. /// <returns>String with the base directory</returns>
  19. internal delegate string GetBaseDirectoryDelegate();
  20. /// <summary>
  21. /// Delegate definition for getting the current directory.
  22. /// </summary>
  23. /// <returns>String with the current directory</returns>
  24. internal delegate string GetCurrentDirectoryDelegate();
  25. /// <summary>
  26. /// Delegate definition for setting the current directory.
  27. /// </summary>
  28. /// <param name="newDirectory">New Directory to switch into</param>
  29. internal delegate void SetCurrentDirectoryDelegate(string newDirectory);
  30. /// <summary>
  31. /// Create a directory with the specific path.
  32. /// </summary>
  33. /// <param name="directoryPath">Directory path to create</param>
  34. internal delegate void CreateDirectoryDelegate(string directoryPath);
  35. /// <summary>
  36. /// just Directory.GetFiles).
  37. /// </summary>
  38. /// <param name="path">File path for the search</param>
  39. /// <param name="searchPattern">Search Pattern</param>
  40. internal delegate string[] GetFilesDelegate(string path,
  41. string searchPattern);
  42. internal delegate string[] GetDirectoriesDelegate(string path,
  43. string searchPattern);
  44. internal delegate string[] GetDirectoriesRecursiveDelegate(string path,
  45. string searchPattern);
  46. internal delegate void DeleteDelegate(string path);
  47. #endregion
  48. #region Exists (Static)
  49. /// <summary>
  50. /// Helper method to figure out if a directory exists, will just call
  51. /// MethodAccessException, there we use the isolated storage instead.
  52. /// </summary>
  53. /// <param name="directory">
  54. /// Directory to check for. If this is null or empty, true is returned (the
  55. /// current path we are in obviously exists).
  56. /// </param>
  57. /// <returns>True if the directory path exists, false otherwise</returns>
  58. public static bool Exists(string directory)
  59. {
  60. if (String.IsNullOrEmpty(directory))
  61. {
  62. return true;
  63. }
  64. return ExistsCallback(directory);
  65. }
  66. #endregion
  67. #region GetBaseDirectory (Static)
  68. /// <summary>
  69. /// Get base directory where the application was started, is usually the
  70. /// same as GetCurrentDirectory, but can differ if current directory was
  71. /// changed (e.g. via SetCurrentDirectory).
  72. /// </summary>
  73. /// <returns>String with the base directory</returns>
  74. public static string GetBaseDirectory()
  75. {
  76. return GetBaseDirectoryCallback();
  77. }
  78. #endregion
  79. #region GetCurrentDirectory (Static)
  80. /// <summary>
  81. /// Get current directory, just uses Directory.GetCurrentDirectory except
  82. /// </summary>
  83. /// <returns>String with the current directory</returns>
  84. public static string GetCurrentDirectory()
  85. {
  86. return GetCurrentDirectoryCallback();
  87. }
  88. #endregion
  89. #region SetCurrentDirectory (Static)
  90. /// <summary>
  91. /// Set current directory, just uses Directory.SetCurrentDirectory except
  92. /// </summary>
  93. /// <param name="newDirectory">New Directory to switch into</param>
  94. public static void SetCurrentDirectory(string newDirectory)
  95. {
  96. SetCurrentDirectoryCallback(newDirectory);
  97. }
  98. #endregion
  99. #region CreateDirectory (Static)
  100. /// <summary>
  101. /// Create a directory with the specific path.
  102. /// </summary>
  103. /// <param name="directoryPath">Directory path to create</param>
  104. /// <param name="deleteIfExists">
  105. /// If the directory already exists, then delete it or not.
  106. /// </param>
  107. public static void CreateDirectory(string directoryPath,
  108. bool deleteIfExists = false)
  109. {
  110. bool existsAlready = Exists(directoryPath);
  111. if (deleteIfExists &&
  112. existsAlready)
  113. {
  114. Delete(directoryPath, true);
  115. // Wait a bit to not confuse the OS with the creation of the directory
  116. // we just deleted! If we don't do this the creation below might fail.
  117. Thread.Sleep(10);
  118. }
  119. // We cannot create the current relative directory (empty string)
  120. // And we only need to create if the directory does not exist yet.
  121. if (String.IsNullOrEmpty(directoryPath) == false &&
  122. (deleteIfExists ||
  123. existsAlready == false))
  124. {
  125. CreateDirectoryCallback(directoryPath);
  126. }
  127. }
  128. #endregion
  129. #region GetFiles (Static)
  130. /// <summary>
  131. /// Get files helper method.
  132. /// </summary>
  133. /// <param name="path">File path for the search</param>
  134. /// <param name="searchPattern">Search Pattern</param>
  135. /// <returns>
  136. /// Files in the directory matching searchPattern as filename string list
  137. /// with full file paths.
  138. /// </returns>
  139. public static string[] GetFiles(string path, string searchPattern)
  140. {
  141. // If path does not exist, Directory.GetFiles would throw an exception
  142. if (String.IsNullOrEmpty(path) ||
  143. Exists(path) == false)
  144. {
  145. return new string[0];
  146. }
  147. return GetFilesCallback(path, searchPattern);
  148. }
  149. #endregion
  150. #region GetFilesRecursive (Static)
  151. /// <summary>
  152. /// Get files function with recursive file searching in sub directories.
  153. /// </summary>
  154. /// <param name="path">File path for the start of the search</param>
  155. /// <param name="searchPattern">Search Pattern</param>
  156. /// <returns>
  157. /// Files in the directory and any sub directory matching searchPattern as
  158. /// filename string list (with full absolute file paths).
  159. /// </returns>
  160. public static string[] GetFilesRecursive(string path,
  161. string searchPattern)
  162. {
  163. // check null or empty and path exists
  164. if (String.IsNullOrEmpty(path) ||
  165. Exists(path) == false)
  166. {
  167. return new string[0];
  168. }
  169. List<string> files = new List<string>();
  170. try
  171. {
  172. // find all directories in the current
  173. foreach (string s in GetDirectories(path))
  174. {
  175. // add the search results of the next folder
  176. files.AddRange(GetFilesRecursive(s, searchPattern));
  177. }
  178. // add the files of the current directory
  179. files.AddRange(GetFiles(path, searchPattern));
  180. }
  181. catch (Exception ex)
  182. {
  183. Log.Warning("GetFiles failed for the following reason:" + ex);
  184. }
  185. // return the collected files list
  186. return files.ToArray();
  187. }
  188. #endregion
  189. #region GetFilesMultiPattern (Static)
  190. /// <summary>
  191. /// Same as normal GetFiles but with the option to use multi patterns,
  192. /// e.g. "*.txt|*.wma".
  193. /// </summary>
  194. /// <param name="path">Path to search for files in.</param>
  195. /// <param name="searchPattern">Multiple pattern separated by an |</param>
  196. /// <param name="recursive">Recursive file search</param>
  197. /// <returns>Array of found files.</returns>
  198. public static string[] GetFilesMultiPattern(string path,
  199. string searchPattern, bool recursive = false)
  200. {
  201. string[] patterns = searchPattern.Split('|');
  202. List<string> foundFiles = new List<string>();
  203. foreach (string pattern in patterns)
  204. {
  205. if (recursive)
  206. {
  207. foundFiles.AddRange(GetFilesRecursive(path, pattern));
  208. }
  209. else
  210. {
  211. foundFiles.AddRange(GetFiles(path, pattern));
  212. }
  213. }
  214. return foundFiles.ToArray();
  215. }
  216. #endregion
  217. #region GetDirectories (Static)
  218. /// <summary>
  219. /// Get the directories in the given path.
  220. /// </summary>
  221. /// <param name="path">Path</param>
  222. /// <returns>Array of sub directories of path</returns>
  223. public static string[] GetDirectories(string path)
  224. {
  225. return GetDirectories(path, "*");
  226. }
  227. /// <summary>
  228. /// Get the directories in the given path.
  229. /// </summary>
  230. /// <param name="path">Path</param>
  231. /// <param name="searchPattern">Search pattern to find specific
  232. /// directories only.</param>
  233. /// <returns>Array of sub directories of path</returns>
  234. public static string[] GetDirectories(string path, string searchPattern)
  235. {
  236. if (Exists(path) == false)
  237. {
  238. return new string[0];
  239. }
  240. return GetDirectoriesCallback(path, searchPattern);
  241. }
  242. #endregion
  243. #region GetDirectoriesRecursive (Static)
  244. /// <summary>
  245. /// Get all directories and sub directories in the given path recursively.
  246. /// </summary>
  247. /// <param name="path">Path</param>
  248. /// <returns>List of all directories found</returns>
  249. public static string[] GetDirectoriesRecursive(string path)
  250. {
  251. return GetDirectoriesRecursive(path, "*");
  252. }
  253. /// <summary>
  254. /// Get all directories and sub directories in the given path recursively.
  255. /// </summary>
  256. /// <param name="path">Path</param>
  257. /// <param name="searchPattern">Search Pattern</param>
  258. /// <returns>List of all directories found</returns>
  259. public static string[] GetDirectoriesRecursive(string path,
  260. string searchPattern)
  261. {
  262. if (Exists(path) == false)
  263. {
  264. return new string[0];
  265. }
  266. return GetDirectoriesRecursiveCallback(path, searchPattern);
  267. }
  268. #endregion
  269. #region GetParentDirectory (Static)
  270. /// <summary>
  271. /// Gets the parent directory of the given directory.
  272. /// </summary>
  273. /// <param name="directory">The path of the current folder</param>
  274. /// <param name="parentDirectory">The path of the parent directory.</param>
  275. /// <returns>Returns 'true' if there is a parent directory.</returns>
  276. public static bool GetParentDirectory(this string directory,
  277. out string parentDirectory)
  278. {
  279. parentDirectory = FileHelper.GetDirectoryPath(directory);
  280. return parentDirectory != directory;
  281. }
  282. #endregion
  283. #region ComparePaths (Static)
  284. /// <summary>
  285. /// Compare two paths, ignoring if they use \\ or / and if
  286. /// something is written in big or small letters. We also check the full
  287. /// path so if we have something like .. or . this won't affect the
  288. /// comparison.
  289. /// </summary>
  290. /// <param name="path1">First path.</param>
  291. /// <param name="path2">Second path.</param>
  292. /// <returns>True if the paths are equal.</returns>
  293. public static bool ComparePaths(string path1, string path2)
  294. {
  295. // Validation
  296. if (String.IsNullOrEmpty(path1) ||
  297. String.IsNullOrEmpty(path2))
  298. {
  299. return false;
  300. }
  301. DirectoryInfo info1 = new DirectoryInfo(path1);
  302. DirectoryInfo info2 = new DirectoryInfo(path2);
  303. return String.Compare(
  304. info1.FullName.TrimEnd('\\'),
  305. info2.FullName.TrimEnd('\\'),
  306. StringComparison.InvariantCultureIgnoreCase) == 0 &&
  307. String.Compare(
  308. Path.GetFullPath(path1).ToUpperInvariant().TrimEnd('\\'),
  309. Path.GetFullPath(path2).ToUpperInvariant().TrimEnd('\\'),
  310. StringComparison.InvariantCultureIgnoreCase) == 0;
  311. //path1 = path1.Replace("\\", "/").ToLower();
  312. //path2 = path2.Replace("\\", "/").ToLower();
  313. //return path1 == path2;
  314. }
  315. #endregion
  316. #region IncludeTrailingBackslash (Static)
  317. /// <summary>
  318. /// Include trailing backslash. Makes sure the directory ends with "\".
  319. /// </summary>
  320. /// <param name="directory">Directory</param>
  321. /// <returns>String that always ends with a backslash</returns>
  322. public static string IncludeTrailingBackslash(string directory)
  323. {
  324. if (directory.Length == 0 ||
  325. directory[directory.Length - 1] != '\\')
  326. {
  327. directory += @"\";
  328. }
  329. return directory;
  330. }
  331. #endregion
  332. #region ExcludeTrailingBackslash (Static)
  333. /// <summary>
  334. /// Exclude last trailing backslash from a directory.
  335. /// </summary>
  336. /// <param name="directory">Directory</param>
  337. /// <returns>String without a trailing backslash</returns>
  338. public static string ExcludeTrailingBackslash(string directory)
  339. {
  340. if (directory[directory.Length - 1] == '\\')
  341. {
  342. return directory.Substring(0, directory.Length - 1);
  343. }
  344. return directory;
  345. }
  346. #endregion
  347. #region CopyDirectory (Static)
  348. /// <summary>
  349. /// Copy directory and all files over to destinationPath.
  350. /// </summary>
  351. /// <param name="destinationPath">Destination Path</param>
  352. /// <param name="sourcePath">Source Path</param>
  353. /// <returns>True if the directory was copied successfully</returns>
  354. public static bool CopyDirectory(string sourcePath,
  355. string destinationPath)
  356. {
  357. bool ret = false;
  358. try
  359. {
  360. string separator = Path.DirectorySeparatorChar.ToString();
  361. sourcePath =
  362. sourcePath.EndsWith(separator)
  363. ? sourcePath
  364. : sourcePath + separator;
  365. destinationPath =
  366. destinationPath.EndsWith(separator)
  367. ? destinationPath
  368. : destinationPath + separator;
  369. if (Exists(sourcePath))
  370. {
  371. if (Exists(destinationPath) == false)
  372. {
  373. CreateDirectory(destinationPath);
  374. }
  375. foreach (string fls in GetFiles(sourcePath, "*"))
  376. {
  377. FileInfo flinfo = new FileInfo(fls);
  378. flinfo.CopyTo(destinationPath + flinfo.Name, true);
  379. if (FileHelper.CompareFiles(destinationPath + flinfo.Name, fls) ==
  380. false)
  381. {
  382. throw new Exception(
  383. "Failed to copy a file (" +flinfo.Name +
  384. "). Source and destination files are not equal!");
  385. }
  386. }
  387. ret = true;
  388. foreach (string drs in GetDirectories(sourcePath))
  389. {
  390. DirectoryInfo drinfo = new DirectoryInfo(drs);
  391. if (CopyDirectory(drs, destinationPath + drinfo.Name) == false)
  392. {
  393. ret = false;
  394. }
  395. }
  396. }
  397. }
  398. catch // (Exception ex)
  399. {
  400. ret = false;
  401. }
  402. return ret;
  403. }
  404. #endregion
  405. #region MoveDirectory (Static)
  406. /// <summary>
  407. /// Move directory and all files in it to destinationPath.
  408. /// </summary>
  409. /// <param name="destinationPath">Destination Path</param>
  410. /// <param name="sourcePath">Source Path</param>
  411. /// <returns>True if the move was successful</returns>
  412. public static bool MoveDirectory(string sourcePath,
  413. string destinationPath)
  414. {
  415. bool ret = false;
  416. try
  417. {
  418. string separator = Path.DirectorySeparatorChar.ToString();
  419. sourcePath = sourcePath.EndsWith(separator)
  420. ? sourcePath
  421. : sourcePath + separator;
  422. destinationPath = destinationPath.EndsWith(separator)
  423. ? destinationPath
  424. : destinationPath + separator;
  425. if (Exists(sourcePath))
  426. {
  427. if (Exists(destinationPath) == false)
  428. {
  429. CreateDirectory(destinationPath);
  430. }
  431. foreach (string fls in GetFiles(sourcePath, "*"))
  432. {
  433. FileInfo flinfo = new FileInfo(fls);
  434. flinfo.MoveTo(destinationPath + flinfo.Name);
  435. }
  436. foreach (string drs in Directory.GetDirectories(sourcePath))
  437. {
  438. DirectoryInfo drinfo = new DirectoryInfo(drs);
  439. if (MoveDirectory(drs, destinationPath + drinfo.Name) == false)
  440. {
  441. ret = false;
  442. }
  443. }
  444. }
  445. if (Exists(sourcePath))
  446. {
  447. SafeDelete(sourcePath);
  448. }
  449. ret = true;
  450. }
  451. catch // (Exception ex)
  452. {
  453. ret = false;
  454. }
  455. return ret;
  456. }
  457. #endregion
  458. #region SafeDelete (Static)
  459. /// <summary>
  460. /// Delete directory and warn if we are unable to delete files.
  461. /// </summary>
  462. /// <param name="path">Path</param>
  463. /// <returns>True if the deletion succeeded</returns>
  464. public static bool SafeDelete(string path)
  465. {
  466. return Delete(path, true);
  467. }
  468. #endregion
  469. #region Delete (Static)
  470. /// <summary>
  471. /// Delete directory and optionally warn if we are unable to delete files.
  472. /// </summary>
  473. /// <param name="path">Path to delete</param>
  474. /// <param name="warnIfFailing">
  475. /// Warn if any file fails to be deleted (usually happens when files are
  476. /// still open and locked).
  477. /// </param>
  478. /// <returns>
  479. /// True if the deletion succeeded, if the directory does not exist this
  480. /// method simply returns false.
  481. /// </returns>
  482. public static bool Delete(string path, bool warnIfFailing)
  483. {
  484. if (Exists(path) == false)
  485. {
  486. return true;
  487. }
  488. try
  489. {
  490. foreach (string file in GetFilesRecursive(path, "*.*"))
  491. {
  492. try
  493. {
  494. FileHelper.SafeDelete(file);
  495. }
  496. catch (Exception)
  497. {
  498. if (warnIfFailing)
  499. {
  500. Log.Warning("Unable to delete file: " + file);
  501. }
  502. }
  503. }
  504. DeleteCallback(path);
  505. return true;
  506. }
  507. catch (Exception ex)
  508. {
  509. if (warnIfFailing)
  510. {
  511. Log.Warning("Failed to delete directory " + path + ": " + ex);
  512. }
  513. return false;
  514. }
  515. }
  516. #endregion
  517. #region Internal
  518. #region ExistsCallback (Internal)
  519. internal static ExistsDelegate ExistsCallback;
  520. #endregion
  521. #region GetBaseDirectoryCallback (Internal)
  522. /// <summary>
  523. /// Callback for getting the base directory. On Windows this is just
  524. /// Directory.GetBaseDirectory.
  525. /// </summary>
  526. internal static GetBaseDirectoryDelegate GetBaseDirectoryCallback;
  527. #endregion
  528. #region GetCurrentDirectoryCallback (Internal)
  529. /// <summary>
  530. /// Callback for getting the current directory. On Windows this is just
  531. /// Directory.GetCurrentDirectory.
  532. /// </summary>
  533. internal static GetCurrentDirectoryDelegate GetCurrentDirectoryCallback;
  534. #endregion
  535. #region SetCurrentDirectoryCallback (Internal)
  536. /// <summary>
  537. /// Callback for setting the current directory. On Windows this is just
  538. /// Directory.SetCurrentDirectory.
  539. /// </summary>
  540. internal static SetCurrentDirectoryDelegate SetCurrentDirectoryCallback;
  541. #endregion
  542. #region CreateDirectoryCallback (Internal)
  543. /// <summary>
  544. /// Callback for creating a new directory. On Windows this is just
  545. /// Directory.Create.
  546. /// </summary>
  547. internal static CreateDirectoryDelegate CreateDirectoryCallback;
  548. #endregion
  549. #region GetFilesCallback (Internal)
  550. /// <summary>
  551. /// Callback for getting all files in a directory. On Windows this is just
  552. /// Directory.GetFiles.
  553. /// </summary>
  554. internal static GetFilesDelegate GetFilesCallback;
  555. #endregion
  556. #region GetDirectoriesCallback (Internal)
  557. internal static GetDirectoriesDelegate GetDirectoriesCallback;
  558. #endregion
  559. #region GetDirectoriesRecursiveCallback (Internal)
  560. internal static GetDirectoriesRecursiveDelegate
  561. GetDirectoriesRecursiveCallback;
  562. #endregion
  563. #region DeleteCallback (Internal)
  564. internal static DeleteDelegate DeleteCallback;
  565. #endregion
  566. #endregion
  567. #region Constructors
  568. /// <summary>
  569. /// Static constructor to setup all the needed callbacks.
  570. /// </summary>
  571. static DirectoryHelper()
  572. {
  573. ExistsCallback = Directory.Exists;
  574. SetCurrentDirectoryCallback = Directory.SetCurrentDirectory;
  575. GetDirectoriesCallback = Directory.GetDirectories;
  576. GetCurrentDirectoryCallback = Directory.GetCurrentDirectory;
  577. GetFilesCallback = Directory.GetFiles;
  578. #region CreateDirectoryCallback
  579. CreateDirectoryCallback = delegate(string directoryPath)
  580. {
  581. Directory.CreateDirectory(directoryPath);
  582. };
  583. #endregion
  584. #region GetBaseDirectoryCallback
  585. GetBaseDirectoryCallback = delegate
  586. {
  587. return AppDomain.CurrentDomain.BaseDirectory;
  588. };
  589. #endregion
  590. #region GetDirectoriesRecursiveCallback
  591. GetDirectoriesRecursiveCallback = delegate(string path,
  592. string searchPattern)
  593. {
  594. return Directory.GetDirectories(path, searchPattern,
  595. SearchOption.AllDirectories);
  596. };
  597. #endregion
  598. #region DeleteCallback
  599. DeleteCallback = delegate(string path)
  600. {
  601. Directory.Delete(path, true);
  602. };
  603. #endregion
  604. }
  605. #endregion
  606. /// <summary>
  607. /// Tests for the DirectoryHelper class
  608. /// </summary>
  609. internal class DirectoryHelperTests
  610. {
  611. #region CopyDir (Static)
  612. /// <summary>
  613. /// Copy directory. Note: Too slow for a dynamic unit test.
  614. /// </summary>
  615. [Test, Category("LongRunning")]
  616. public static void CopyDir()
  617. {
  618. // Only do this test if C:\testing and C:\copytest do not exist!
  619. Assert.False(Directory.Exists("C:\\testing\\"));
  620. Assert.False(Directory.Exists("C:\\copytest\\"));
  621. Directory.CreateDirectory("C:\\testing\\");
  622. Directory.CreateDirectory("C:\\testing\\bla\\");
  623. File.Create("C:\\testing\\test.txt").Close();
  624. File.Create("C:\\testing\\bla\\test.txt").Close();
  625. Assert.True(CopyDirectory("C:\\testing\\", "C:\\copytest\\"),
  626. "Copy Directory");
  627. Assert.True(File.Exists("C:\\copytest\\test.txt"), "File Exists");
  628. Assert.True(File.Exists("C:\\copytest\\bla\\test.txt"), "File Exists");
  629. Directory.Delete("C:\\testing\\", true);
  630. Directory.Delete("C:\\copytest\\", true);
  631. }
  632. #endregion
  633. #region GetFilesRecursive (Static)
  634. /// <summary>
  635. /// Test GetFilesRecursive
  636. /// </summary>
  637. [Test, Category("LongRunning")]
  638. public static void GetFilesRecursive()
  639. {
  640. string deltaBasePath =
  641. Exists(@"C:\code\Delta")
  642. ? @"C:\code\Delta"
  643. : @"C:\Development\Delta";
  644. string rootPath = deltaBasePath + @"\Engine\";
  645. string[] result = DirectoryHelper.GetFilesRecursive(rootPath, "*.cs");
  646. Assert.True((rootPath + "Application.cs").Contains(result));
  647. Assert.False((rootPath + "blubbb.cs").Contains(result));
  648. result = DirectoryHelper.GetFilesRecursive(rootPath,
  649. "Delta.Engine.csproj");
  650. Assert.Equal(result.Length, 1);
  651. Assert.Equal(result[0], rootPath + "Delta.Engine.csproj");
  652. }
  653. #endregion
  654. #region TestGetFilesMultiPattern (Static)
  655. /// <summary>
  656. /// Test get files multi pattern
  657. /// </summary>
  658. [Test, Category("LongRunning")]
  659. public static void TestGetFilesMultiPattern()
  660. {
  661. string[] files = GetFilesMultiPattern(@"C:\code\Delta\",
  662. "*.sln|*.suo");
  663. foreach (string file in files)
  664. {
  665. Console.WriteLine(file);
  666. }
  667. }
  668. #endregion
  669. #region ComparePaths (Static)
  670. /// <summary>
  671. /// Test the ComparePaths method.
  672. /// </summary>
  673. [Test]
  674. public static void ComparePaths()
  675. {
  676. Assert.True(DirectoryHelper.ComparePaths(
  677. "C:\\code\\Delta", "C:\\code\\Delta"));
  678. Assert.True(DirectoryHelper.ComparePaths(
  679. "C:\\code\\Delta", "C:/code/Delta"));
  680. Assert.True(DirectoryHelper.ComparePaths(
  681. "c:\\code\\delta", "C:/CODE/DELTA"));
  682. Assert.False(DirectoryHelper.ComparePaths(
  683. "C:\\code\\Delta", "C:\\code\\Delt"));
  684. // Special cases
  685. Assert.True(DirectoryHelper.ComparePaths(
  686. "C:\\code\\Delta\\..\\..\\.\\code\\Delta", "C:\\code\\Delta"));
  687. Assert.True(DirectoryHelper.ComparePaths(
  688. "C:/code\\Delta", "C:\\code/Delta"));
  689. Assert.True(DirectoryHelper.ComparePaths(
  690. "C:/code\\..\\code\\Delta", "C:\\code/./Delta"));
  691. }
  692. #endregion
  693. #region GetParentDirectory
  694. /// <summary>
  695. /// Get parent directory
  696. /// </summary>
  697. [Test]
  698. public void GetParentDirectory()
  699. {
  700. string path = @"C:\Windows\System32";
  701. string parentPath;
  702. Assert.True(path.GetParentDirectory(out parentPath));
  703. Assert.Equal(parentPath, @"C:\Windows");
  704. Assert.True(parentPath.GetParentDirectory(out parentPath));
  705. Assert.Equal(parentPath, @"C:");
  706. Assert.False(parentPath.GetParentDirectory(out parentPath));
  707. Assert.Equal(parentPath, @"C:");
  708. }
  709. #endregion
  710. }
  711. }
  712. }