PathsImplementation.cs 8.5 KB

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