BaseApplicationPaths.cs 9.2 KB

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