PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Castle.IO/Internal/Directory.cs

https://github.com/castleproject/Castle.Transactions
C# | 118 lines | 77 code | 20 blank | 21 comment | 7 complexity | 3a23416229d2b51249e469bd9b2b3970 MD5 | raw file
Possible License(s): Apache-2.0
  1. #region license
  2. // Copyright 2004-2012 Castle Project, Henrik Feldt &contributors - https://github.com/castleproject
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Diagnostics.Contracts;
  18. namespace Castle.IO.Internal
  19. {
  20. /// <summary>
  21. /// Utility class for directories.
  22. /// </summary>
  23. public static class Directory
  24. {
  25. private static IDirectoryAdapter _DirectoryAdapter;
  26. public static void InitializeWith(IDirectoryAdapter adapter)
  27. {
  28. Contract.Requires(adapter != null);
  29. Contract.Ensures(_DirectoryAdapter != null);
  30. if (_DirectoryAdapter != null)
  31. throw new InvalidOperationException(
  32. "This method cannot be called twice without resetting the class with Directory.Reset().");
  33. _DirectoryAdapter = adapter;
  34. }
  35. public static void Reset()
  36. {
  37. _DirectoryAdapter = null;
  38. }
  39. private static IDirectoryAdapter GetAdapter()
  40. {
  41. Contract.Requires(_DirectoryAdapter != null);
  42. if (_DirectoryAdapter == null)
  43. throw new InvalidOperationException(
  44. "If you call the Directory API you first need to call Directory.InitializeWith(IDirectoryAdapter)");
  45. return _DirectoryAdapter;
  46. }
  47. public static bool Create(string path)
  48. {
  49. Contract.Requires(!string.IsNullOrEmpty(path));
  50. return GetAdapter().Create(path);
  51. }
  52. /// <summary>
  53. /// Returns whether the given paths exists.
  54. /// </summary>
  55. /// <param name = "path"></param>
  56. /// <returns></returns>
  57. public static bool Exists(string path)
  58. {
  59. Contract.Requires(!string.IsNullOrEmpty(path));
  60. return GetAdapter().Exists(path);
  61. }
  62. public static void DeleteDirectory(string path)
  63. {
  64. Contract.Requires(!string.IsNullOrEmpty(path));
  65. GetAdapter().Delete(path);
  66. }
  67. public static bool DeleteDirectory(string path, bool recursively)
  68. {
  69. Contract.Requires(!string.IsNullOrEmpty(path));
  70. return GetAdapter().Delete(path, recursively);
  71. }
  72. public static string GetFullPath(string dir)
  73. {
  74. Contract.Requires(!string.IsNullOrEmpty(dir));
  75. return GetAdapter().GetFullPath(dir);
  76. }
  77. public static string MapPath(string path)
  78. {
  79. Contract.Requires(!string.IsNullOrEmpty(path));
  80. return GetAdapter().MapPath(path);
  81. }
  82. public static void Move(this string source, string target)
  83. {
  84. Contract.Requires(!string.IsNullOrEmpty(source));
  85. Contract.Requires(!string.IsNullOrEmpty(target));
  86. GetAdapter().Move(source, target);
  87. }
  88. public static void Move(string source, string target, bool overwrite)
  89. {
  90. GetAdapter().Move(source, target, overwrite);
  91. }
  92. public static bool CreateDirectory(string directoryPath)
  93. {
  94. Contract.Requires(!string.IsNullOrEmpty(directoryPath));
  95. return Create(directoryPath);
  96. }
  97. }
  98. }