BaseApplicationPaths.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using MediaBrowser.Common.Kernel;
  2. using System;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Reflection;
  6. namespace MediaBrowser.Common.Implementations
  7. {
  8. /// <summary>
  9. /// Provides a base class to hold common application paths used by both the Ui and Server.
  10. /// This can be subclassed to add application-specific paths.
  11. /// </summary>
  12. public abstract class BaseApplicationPaths : IApplicationPaths
  13. {
  14. /// <summary>
  15. /// The _program data path
  16. /// </summary>
  17. private string _programDataPath;
  18. /// <summary>
  19. /// Gets the path to the program data folder
  20. /// </summary>
  21. /// <value>The program data path.</value>
  22. public string ProgramDataPath
  23. {
  24. get
  25. {
  26. return _programDataPath ?? (_programDataPath = GetProgramDataPath());
  27. }
  28. }
  29. /// <summary>
  30. /// The _data directory
  31. /// </summary>
  32. private string _dataDirectory;
  33. /// <summary>
  34. /// Gets the folder path to the data directory
  35. /// </summary>
  36. /// <value>The data directory.</value>
  37. public string DataPath
  38. {
  39. get
  40. {
  41. if (_dataDirectory == null)
  42. {
  43. _dataDirectory = Path.Combine(ProgramDataPath, "data");
  44. if (!Directory.Exists(_dataDirectory))
  45. {
  46. Directory.CreateDirectory(_dataDirectory);
  47. }
  48. }
  49. return _dataDirectory;
  50. }
  51. }
  52. /// <summary>
  53. /// The _image cache path
  54. /// </summary>
  55. private string _imageCachePath;
  56. /// <summary>
  57. /// Gets the image cache path.
  58. /// </summary>
  59. /// <value>The image cache path.</value>
  60. public string ImageCachePath
  61. {
  62. get
  63. {
  64. if (_imageCachePath == null)
  65. {
  66. _imageCachePath = Path.Combine(CachePath, "images");
  67. if (!Directory.Exists(_imageCachePath))
  68. {
  69. Directory.CreateDirectory(_imageCachePath);
  70. }
  71. }
  72. return _imageCachePath;
  73. }
  74. }
  75. /// <summary>
  76. /// The _plugins path
  77. /// </summary>
  78. private string _pluginsPath;
  79. /// <summary>
  80. /// Gets the path to the plugin directory
  81. /// </summary>
  82. /// <value>The plugins path.</value>
  83. public string PluginsPath
  84. {
  85. get
  86. {
  87. if (_pluginsPath == null)
  88. {
  89. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  90. if (!Directory.Exists(_pluginsPath))
  91. {
  92. Directory.CreateDirectory(_pluginsPath);
  93. }
  94. }
  95. return _pluginsPath;
  96. }
  97. }
  98. /// <summary>
  99. /// The _plugin configurations path
  100. /// </summary>
  101. private string _pluginConfigurationsPath;
  102. /// <summary>
  103. /// Gets the path to the plugin configurations directory
  104. /// </summary>
  105. /// <value>The plugin configurations path.</value>
  106. public string PluginConfigurationsPath
  107. {
  108. get
  109. {
  110. if (_pluginConfigurationsPath == null)
  111. {
  112. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  113. if (!Directory.Exists(_pluginConfigurationsPath))
  114. {
  115. Directory.CreateDirectory(_pluginConfigurationsPath);
  116. }
  117. }
  118. return _pluginConfigurationsPath;
  119. }
  120. }
  121. private string _tempUpdatePath;
  122. /// <summary>
  123. /// Gets the path to where temporary update files will be stored
  124. /// </summary>
  125. /// <value>The plugin configurations path.</value>
  126. public string TempUpdatePath
  127. {
  128. get
  129. {
  130. if (_tempUpdatePath == null)
  131. {
  132. _tempUpdatePath = Path.Combine(ProgramDataPath, "Updates");
  133. if (!Directory.Exists(_tempUpdatePath))
  134. {
  135. Directory.CreateDirectory(_tempUpdatePath);
  136. }
  137. }
  138. return _tempUpdatePath;
  139. }
  140. }
  141. /// <summary>
  142. /// The _log directory path
  143. /// </summary>
  144. private string _logDirectoryPath;
  145. /// <summary>
  146. /// Gets the path to the log directory
  147. /// </summary>
  148. /// <value>The log directory path.</value>
  149. public string LogDirectoryPath
  150. {
  151. get
  152. {
  153. if (_logDirectoryPath == null)
  154. {
  155. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  156. if (!Directory.Exists(_logDirectoryPath))
  157. {
  158. Directory.CreateDirectory(_logDirectoryPath);
  159. }
  160. }
  161. return _logDirectoryPath;
  162. }
  163. }
  164. /// <summary>
  165. /// The _configuration directory path
  166. /// </summary>
  167. private string _configurationDirectoryPath;
  168. /// <summary>
  169. /// Gets the path to the application configuration root directory
  170. /// </summary>
  171. /// <value>The configuration directory path.</value>
  172. public string ConfigurationDirectoryPath
  173. {
  174. get
  175. {
  176. if (_configurationDirectoryPath == null)
  177. {
  178. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  179. if (!Directory.Exists(_configurationDirectoryPath))
  180. {
  181. Directory.CreateDirectory(_configurationDirectoryPath);
  182. }
  183. }
  184. return _configurationDirectoryPath;
  185. }
  186. }
  187. /// <summary>
  188. /// The _system configuration file path
  189. /// </summary>
  190. private string _systemConfigurationFilePath;
  191. /// <summary>
  192. /// Gets the path to the system configuration file
  193. /// </summary>
  194. /// <value>The system configuration file path.</value>
  195. public string SystemConfigurationFilePath
  196. {
  197. get
  198. {
  199. return _systemConfigurationFilePath ?? (_systemConfigurationFilePath = Path.Combine(ConfigurationDirectoryPath, "system.xml"));
  200. }
  201. }
  202. /// <summary>
  203. /// The _cache directory
  204. /// </summary>
  205. private string _cachePath;
  206. /// <summary>
  207. /// Gets the folder path to the cache directory
  208. /// </summary>
  209. /// <value>The cache directory.</value>
  210. public string CachePath
  211. {
  212. get
  213. {
  214. if (_cachePath == null)
  215. {
  216. _cachePath = Path.Combine(ProgramDataPath, "cache");
  217. if (!Directory.Exists(_cachePath))
  218. {
  219. Directory.CreateDirectory(_cachePath);
  220. }
  221. }
  222. return _cachePath;
  223. }
  224. }
  225. /// <summary>
  226. /// The _temp directory
  227. /// </summary>
  228. private string _tempDirectory;
  229. /// <summary>
  230. /// Gets the folder path to the temp directory within the cache folder
  231. /// </summary>
  232. /// <value>The temp directory.</value>
  233. public string TempDirectory
  234. {
  235. get
  236. {
  237. if (_tempDirectory == null)
  238. {
  239. _tempDirectory = Path.Combine(CachePath, "temp");
  240. if (!Directory.Exists(_tempDirectory))
  241. {
  242. Directory.CreateDirectory(_tempDirectory);
  243. }
  244. }
  245. return _tempDirectory;
  246. }
  247. }
  248. /// <summary>
  249. /// Gets the path to the application's ProgramDataFolder
  250. /// </summary>
  251. /// <returns>System.String.</returns>
  252. public static string GetProgramDataPath()
  253. {
  254. #if DEBUG
  255. string programDataPath = ConfigurationManager.AppSettings["DebugProgramDataPath"];
  256. #else
  257. string programDataPath = Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
  258. #endif
  259. programDataPath = programDataPath.Replace("%CommonApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
  260. // If it's a relative path, e.g. "..\"
  261. if (!Path.IsPathRooted(programDataPath))
  262. {
  263. var path = Assembly.GetExecutingAssembly().Location;
  264. path = Path.GetDirectoryName(path);
  265. programDataPath = Path.Combine(path, programDataPath);
  266. programDataPath = Path.GetFullPath(programDataPath);
  267. }
  268. if (!Directory.Exists(programDataPath))
  269. {
  270. Directory.CreateDirectory(programDataPath);
  271. }
  272. return programDataPath;
  273. }
  274. }
  275. }