Paths.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. namespace Wayne.Lib.IO
  3. {
  4. /// <summary>
  5. /// Support class managing file paths of the Wayne system.
  6. /// </summary>
  7. public static class Paths
  8. {
  9. #region Construction
  10. static Paths()
  11. {
  12. pathsImplementation = FileSupport.fileSupport.Paths;
  13. }
  14. #endregion
  15. #region fields
  16. /// <summary>
  17. /// The current implementation to be used.
  18. /// </summary>
  19. private static IPaths pathsImplementation;
  20. #endregion
  21. #region Properties
  22. /// <summary>
  23. /// The root path used by Wayne applications.
  24. /// </summary>
  25. public static string Root { get { return pathsImplementation.Root; } }
  26. /// <summary>
  27. /// The base path to all config files.
  28. /// </summary>
  29. public static string Config { get { return pathsImplementation.Config; } }
  30. /// <summary>
  31. /// The base path to all transaction files.
  32. /// </summary>
  33. public static string Transactions { get { return pathsImplementation.Transactions; } }
  34. /// <summary>
  35. /// The base path to all log files.
  36. /// </summary>
  37. public static string Log { get { return pathsImplementation.Log; } }
  38. /// <summary>
  39. /// The complete path (including the file name) to the executable.
  40. /// </summary>
  41. public static string ExecutablePath { get { return pathsImplementation.ExecutablePath; } }
  42. /// <summary>
  43. /// The directory path to the executable.
  44. /// </summary>
  45. public static string ExecutableDirectory { get { return pathsImplementation.ExecutableDirectory; } }
  46. /// <summary>
  47. /// The base path to data files.
  48. /// </summary>
  49. public static string Data { get { return pathsImplementation.Data; } }
  50. #endregion
  51. #region Methods
  52. /// <summary>
  53. /// Combines the Root path and the given subPath.
  54. /// </summary>
  55. /// <param name="subPath">The sub path (under the main path).</param>
  56. /// <returns></returns>
  57. public static string Combine(string subPath)
  58. {
  59. return pathsImplementation.Combine(subPath);
  60. }
  61. /// <summary>
  62. /// Combines the Root path and the given subPath and file name.
  63. /// </summary>
  64. /// <param name="subPath">The sub path (under the main path).</param>
  65. /// <param name="subPath2">An additional sub path (or the name of a file).</param>
  66. /// <returns></returns>
  67. public static string Combine(string subPath, string subPath2)
  68. {
  69. return pathsImplementation.Combine(subPath, subPath2);
  70. }
  71. /// <summary>
  72. /// Returns the config path to the given config name.
  73. /// </summary>
  74. /// <param name="configName">The name of the config.</param>
  75. /// <returns></returns>
  76. public static string GetConfigPath(string configName)
  77. {
  78. return pathsImplementation.GetConfigPath(configName);
  79. }
  80. /// <summary>
  81. /// Returns the config path to the given config name.
  82. /// </summary>
  83. /// <param name="configName">The name of the config.</param>
  84. /// <param name="fileName">The name of a config file.</param>
  85. /// <returns></returns>
  86. public static string GetConfigPath(string configName, string fileName)
  87. {
  88. return pathsImplementation.GetConfigPath(configName, fileName);
  89. }
  90. /// <summary>
  91. /// Returns the transaction file path to the given terminal type.
  92. /// </summary>
  93. /// <param name="terminalType">The name of the terminal type.</param>
  94. /// <returns></returns>
  95. public static string GetTransactionsPath(string terminalType)
  96. {
  97. return pathsImplementation.GetTransactionsPath(terminalType);
  98. }
  99. /// <summary>
  100. /// Returns the transaction file path to the given terminal type.
  101. /// </summary>
  102. /// <param name="terminalType">The name of the terminal type.</param>
  103. /// <param name="subState">The name of the sub state.</param>
  104. /// <returns></returns>
  105. public static string GetTransactionsPath(string terminalType, string subState)
  106. {
  107. return pathsImplementation.GetTransactionsPath(terminalType, subState);
  108. }
  109. /// <summary>
  110. /// Replaces the extension of a file (e.g. from C:\MyFile.aaa to C:\MyFile.bbb)
  111. /// </summary>
  112. /// <param name="fileName"></param>
  113. /// <param name="newExtension"></param>
  114. /// <returns></returns>
  115. public static string ReplaceExtension(string fileName, string newExtension)
  116. {
  117. return pathsImplementation.ReplaceExtension(fileName, newExtension);
  118. }
  119. #endregion
  120. #region Parse
  121. /// <summary>
  122. /// Parses the path to replace any %...% variables with the current actual path.
  123. /// </summary>
  124. /// <param name="path"></param>
  125. /// <returns></returns>
  126. public static string Parse(string path)
  127. {
  128. return pathsImplementation.Parse(path);
  129. }
  130. private static void Replace(ref string path, string variableName, string actualText)
  131. {
  132. int index = path.IndexOf(variableName, StringComparison.InvariantCultureIgnoreCase);
  133. if (index > -1)
  134. path = path.Remove(index, variableName.Length).Insert(index, actualText);
  135. }
  136. /// <summary>
  137. /// Checks if file exists
  138. /// </summary>
  139. /// <param name="fileName"></param>
  140. /// <returns></returns>
  141. [Obsolete("Use the DirectoryExists() in FileSupport instead!")]
  142. public static bool FileExists(string fileName)
  143. {
  144. return pathsImplementation.FileExists(Parse(fileName));
  145. }
  146. /// <summary>
  147. /// Checks if directory exists
  148. /// </summary>
  149. /// <param name="path"></param>
  150. /// <returns></returns>
  151. [Obsolete("Use the DirectoryExists() in FileSupport instead!")]
  152. public static bool DirectoryExists(string path)
  153. {
  154. return pathsImplementation.EnsureDirectoryExists(path);
  155. }
  156. /// <summary>
  157. /// Ensures the directory exists by trying to create it if it does not exist already.
  158. /// </summary>
  159. /// <param name="path"></param>
  160. /// <returns></returns>
  161. [Obsolete("Use the DirectoryExists() in FileSupport instead!")]
  162. public static bool EnsureDirectoryExists(string path)
  163. {
  164. return pathsImplementation.EnsureDirectoryExists(path);
  165. }
  166. #endregion
  167. }
  168. }