PathsImplementation.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.IO;
  3. namespace Wayne.Lib.IO
  4. {
  5. /// <summary>
  6. /// Support class managing file paths of the Wayne system.
  7. /// </summary>
  8. public class PathsImplementation : IPaths
  9. {
  10. #region DLL Import
  11. [System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint = "GetModuleFileName", SetLastError = true)]
  12. private static extern int GetModuleFileNameWinCE(IntPtr hModule, byte[] lpFilename, int nSize);
  13. [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "GetModuleFileNameW", SetLastError = true)]
  14. private static extern int GetModuleFileNameWin32(IntPtr hModule, byte[] lpFilename, int nSize);
  15. #endregion
  16. #region Construction
  17. /// <summary>
  18. /// Constructor.
  19. /// </summary>
  20. /// <param name="fullWindows"></param>
  21. public PathsImplementation(bool fullWindows)
  22. {
  23. //bool fullWindows = FileSupport.DirectoryExists(@"C:\");
  24. if (fullWindows)
  25. Root = @"C:\Wayne";
  26. else
  27. Root = @"\Storage card";
  28. Config = Combine("Config");
  29. Log = Combine("Log");
  30. Transactions = Combine("Transactions");
  31. Data = Combine("Data");
  32. const int MAX_PATH = 260;
  33. byte[] buffer = new byte[MAX_PATH * 2];
  34. int chars;
  35. if (fullWindows)
  36. chars = GetModuleFileNameWin32(IntPtr.Zero, buffer, MAX_PATH);
  37. else
  38. chars = GetModuleFileNameWinCE(IntPtr.Zero, buffer, MAX_PATH);
  39. if (chars > 0)
  40. {
  41. ExecutablePath = System.Text.Encoding.Unicode.GetString(buffer, 0, chars * 2);
  42. ExecutableDirectory = Path.GetDirectoryName(ExecutablePath);
  43. }
  44. else
  45. {
  46. ExecutablePath = string.Empty;
  47. ExecutableDirectory = string.Empty;
  48. }
  49. }
  50. #endregion
  51. #region Properties
  52. /// <summary>
  53. /// The root path used by Wayne applications.
  54. /// </summary>
  55. public string Root { get; private set; }
  56. /// <summary>
  57. /// The base path to all config files.
  58. /// </summary>
  59. public string Config { get; private set; }
  60. /// <summary>
  61. /// The base path to all transaction files.
  62. /// </summary>
  63. public string Transactions { get; private set; }
  64. /// <summary>
  65. /// The base path to all log files.
  66. /// </summary>
  67. public string Log { get; private set; }
  68. /// <summary>
  69. /// The complete path (including the file name) to the executable.
  70. /// </summary>
  71. public string ExecutablePath { get; private set; }
  72. /// <summary>
  73. /// The directory path to the executable.
  74. /// </summary>
  75. public string ExecutableDirectory { get; private set; }
  76. /// <summary>
  77. /// The base path to data files.
  78. /// </summary>
  79. public string Data { get; private set; }
  80. #endregion
  81. #region Methods
  82. /// <summary>
  83. /// Combines the Root path and the given subPath.
  84. /// </summary>
  85. /// <param name="subPath">The sub path (under the main path).</param>
  86. /// <returns></returns>
  87. public string Combine(string subPath)
  88. {
  89. return Path.Combine(Root, subPath);
  90. }
  91. /// <summary>
  92. /// Combines the Root path and the given subPath and file name.
  93. /// </summary>
  94. /// <param name="subPath">The sub path (under the main path).</param>
  95. /// <param name="subPath2">An additional sub path (or the name of a file).</param>
  96. /// <returns></returns>
  97. public string Combine(string subPath, string subPath2)
  98. {
  99. return Path.Combine(Path.Combine(Root, subPath), subPath2);
  100. }
  101. /// <summary>
  102. /// Returns the config path to the given config name.
  103. /// </summary>
  104. /// <param name="configName">The name of the config.</param>
  105. /// <returns></returns>
  106. public string GetConfigPath(string configName)
  107. {
  108. return Path.Combine(Config, configName);
  109. }
  110. /// <summary>
  111. /// Returns the config path to the given config name.
  112. /// </summary>
  113. /// <param name="configName">The name of the config.</param>
  114. /// <param name="fileName">The name of a config file.</param>
  115. /// <returns></returns>
  116. public string GetConfigPath(string configName, string fileName)
  117. {
  118. return Path.Combine(Path.Combine(Config, configName), fileName);
  119. }
  120. /// <summary>
  121. /// Returns the transaction file path to the given terminal type.
  122. /// </summary>
  123. /// <param name="terminalType">The name of the terminal type.</param>
  124. /// <returns></returns>
  125. public string GetTransactionsPath(string terminalType)
  126. {
  127. return Path.Combine(Transactions, terminalType);
  128. }
  129. /// <summary>
  130. /// Returns the transaction file path to the given terminal type.
  131. /// </summary>
  132. /// <param name="terminalType">The name of the terminal type.</param>
  133. /// <param name="subState">The name of the sub state.</param>
  134. /// <returns></returns>
  135. public string GetTransactionsPath(string terminalType, string subState)
  136. {
  137. return Path.Combine(Path.Combine(Transactions, terminalType), subState);
  138. }
  139. /// <summary>
  140. /// Replaces the extension of a file (e.g. from C:\MyFile.aaa to C:\MyFile.bbb)
  141. /// </summary>
  142. /// <param name="fileName"></param>
  143. /// <param name="newExtension"></param>
  144. /// <returns></returns>
  145. public string ReplaceExtension(string fileName, string newExtension)
  146. {
  147. string dir = Path.GetDirectoryName(fileName);
  148. string name = Path.GetFileNameWithoutExtension(fileName);
  149. return Path.Combine(dir, string.Concat(name, newExtension.StartsWith(".") ? string.Empty : ".", newExtension));
  150. }
  151. #endregion
  152. #region Parse
  153. /// <summary>
  154. /// Parses the path to replace any %...% variables with the current actual path.
  155. /// </summary>
  156. /// <param name="path"></param>
  157. /// <returns></returns>
  158. public string Parse(string path)
  159. {
  160. if (path != null)
  161. {
  162. Replace(ref path, "%Root%", Root);
  163. Replace(ref path, "%Config%", Config);
  164. Replace(ref path, "%Transactions%", Transactions);
  165. Replace(ref path, "%Log%", Log);
  166. Replace(ref path, "%ExeDir%", ExecutableDirectory);
  167. Replace(ref path, "%Data%", Data);
  168. }
  169. return path;
  170. }
  171. private static void Replace(ref string path, string variableName, string actualText)
  172. {
  173. int index = path.IndexOf(variableName, StringComparison.InvariantCultureIgnoreCase);
  174. if (index > -1)
  175. path = path.Remove(index, variableName.Length).Insert(index, actualText);
  176. }
  177. /// <summary>
  178. /// Checks if file exists
  179. /// </summary>
  180. /// <param name="fileName"></param>
  181. /// <returns></returns>
  182. [Obsolete("Use the DirectoryExists() in FileSupport instead!")]
  183. public bool FileExists(string fileName)
  184. {
  185. return FileSupport.FileExists(Parse(fileName));
  186. }
  187. /// <summary>
  188. /// Checks if directory exists
  189. /// </summary>
  190. /// <param name="path"></param>
  191. /// <returns></returns>
  192. [Obsolete("Use the DirectoryExists() in FileSupport instead!")]
  193. public bool DirectoryExists(string path)
  194. {
  195. return FileSupport.EnsureDirectoryExists(path);
  196. }
  197. /// <summary>
  198. /// Ensures the directory exists by trying to create it if it does not exist already.
  199. /// </summary>
  200. /// <param name="path"></param>
  201. /// <returns></returns>
  202. [Obsolete("Use the DirectoryExists() in FileSupport instead!")]
  203. public bool EnsureDirectoryExists(string path)
  204. {
  205. return FileSupport.EnsureDirectoryExists(path);
  206. }
  207. #endregion
  208. }
  209. }