BaseApplicationPaths.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using MediaBrowser.Common.Configuration;
  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 _use debug path
  16. /// </summary>
  17. private readonly bool _useDebugPath;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="BaseApplicationPaths" /> class.
  20. /// </summary>
  21. protected BaseApplicationPaths(bool useDebugPath, string applicationPath)
  22. {
  23. _useDebugPath = useDebugPath;
  24. ApplicationPath = applicationPath;
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
  28. /// </summary>
  29. protected BaseApplicationPaths(string programDataPath, string applicationPath)
  30. {
  31. _programDataPath = programDataPath;
  32. ApplicationPath = applicationPath;
  33. }
  34. public string ApplicationPath { get; private set; }
  35. /// <summary>
  36. /// The _program data path
  37. /// </summary>
  38. private string _programDataPath;
  39. /// <summary>
  40. /// Gets the path to the program data folder
  41. /// </summary>
  42. /// <value>The program data path.</value>
  43. public string ProgramDataPath
  44. {
  45. get
  46. {
  47. return _programDataPath ?? (_programDataPath = GetProgramDataPath());
  48. }
  49. }
  50. /// <summary>
  51. /// Gets the path to the system folder
  52. /// </summary>
  53. public string ProgramSystemPath { get { return Path.Combine(ProgramDataPath, "system"); } }
  54. /// <summary>
  55. /// The _data directory
  56. /// </summary>
  57. private string _dataDirectory;
  58. /// <summary>
  59. /// Gets the folder path to the data directory
  60. /// </summary>
  61. /// <value>The data directory.</value>
  62. public string DataPath
  63. {
  64. get
  65. {
  66. if (_dataDirectory == null)
  67. {
  68. _dataDirectory = Path.Combine(ProgramDataPath, "data");
  69. Directory.CreateDirectory(_dataDirectory);
  70. }
  71. return _dataDirectory;
  72. }
  73. }
  74. /// <summary>
  75. /// The _image cache path
  76. /// </summary>
  77. private string _imageCachePath;
  78. /// <summary>
  79. /// Gets the image cache path.
  80. /// </summary>
  81. /// <value>The image cache path.</value>
  82. public string ImageCachePath
  83. {
  84. get
  85. {
  86. if (_imageCachePath == null)
  87. {
  88. _imageCachePath = Path.Combine(CachePath, "images");
  89. Directory.CreateDirectory(_imageCachePath);
  90. }
  91. return _imageCachePath;
  92. }
  93. }
  94. /// <summary>
  95. /// The _plugins path
  96. /// </summary>
  97. private string _pluginsPath;
  98. /// <summary>
  99. /// Gets the path to the plugin directory
  100. /// </summary>
  101. /// <value>The plugins path.</value>
  102. public string PluginsPath
  103. {
  104. get
  105. {
  106. if (_pluginsPath == null)
  107. {
  108. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  109. Directory.CreateDirectory(_pluginsPath);
  110. }
  111. return _pluginsPath;
  112. }
  113. }
  114. /// <summary>
  115. /// The _plugin configurations path
  116. /// </summary>
  117. private string _pluginConfigurationsPath;
  118. /// <summary>
  119. /// Gets the path to the plugin configurations directory
  120. /// </summary>
  121. /// <value>The plugin configurations path.</value>
  122. public string PluginConfigurationsPath
  123. {
  124. get
  125. {
  126. if (_pluginConfigurationsPath == null)
  127. {
  128. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  129. Directory.CreateDirectory(_pluginConfigurationsPath);
  130. }
  131. return _pluginConfigurationsPath;
  132. }
  133. }
  134. private string _tempUpdatePath;
  135. /// <summary>
  136. /// Gets the path to where temporary update files will be stored
  137. /// </summary>
  138. /// <value>The plugin configurations path.</value>
  139. public string TempUpdatePath
  140. {
  141. get
  142. {
  143. if (_tempUpdatePath == null)
  144. {
  145. _tempUpdatePath = Path.Combine(ProgramDataPath, "updates");
  146. Directory.CreateDirectory(_tempUpdatePath);
  147. }
  148. return _tempUpdatePath;
  149. }
  150. }
  151. /// <summary>
  152. /// The _log directory path
  153. /// </summary>
  154. private string _logDirectoryPath;
  155. /// <summary>
  156. /// Gets the path to the log directory
  157. /// </summary>
  158. /// <value>The log directory path.</value>
  159. public string LogDirectoryPath
  160. {
  161. get
  162. {
  163. if (_logDirectoryPath == null)
  164. {
  165. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  166. Directory.CreateDirectory(_logDirectoryPath);
  167. }
  168. return _logDirectoryPath;
  169. }
  170. }
  171. /// <summary>
  172. /// The _configuration directory path
  173. /// </summary>
  174. private string _configurationDirectoryPath;
  175. /// <summary>
  176. /// Gets the path to the application configuration root directory
  177. /// </summary>
  178. /// <value>The configuration directory path.</value>
  179. public string ConfigurationDirectoryPath
  180. {
  181. get
  182. {
  183. if (_configurationDirectoryPath == null)
  184. {
  185. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  186. Directory.CreateDirectory(_configurationDirectoryPath);
  187. }
  188. return _configurationDirectoryPath;
  189. }
  190. }
  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 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. Directory.CreateDirectory(_cachePath);
  218. }
  219. return _cachePath;
  220. }
  221. }
  222. /// <summary>
  223. /// The _temp directory
  224. /// </summary>
  225. private string _tempDirectory;
  226. /// <summary>
  227. /// Gets the folder path to the temp directory within the cache folder
  228. /// </summary>
  229. /// <value>The temp directory.</value>
  230. public string TempDirectory
  231. {
  232. get
  233. {
  234. if (_tempDirectory == null)
  235. {
  236. _tempDirectory = Path.Combine(CachePath, "temp");
  237. Directory.CreateDirectory(_tempDirectory);
  238. }
  239. return _tempDirectory;
  240. }
  241. }
  242. /// <summary>
  243. /// Gets the path to the application's ProgramDataFolder
  244. /// </summary>
  245. /// <returns>System.String.</returns>
  246. private string GetProgramDataPath()
  247. {
  248. var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
  249. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  250. // If it's a relative path, e.g. "..\"
  251. if (!Path.IsPathRooted(programDataPath))
  252. {
  253. var path = Path.GetDirectoryName(ApplicationPath);
  254. if (string.IsNullOrEmpty(path))
  255. {
  256. throw new ApplicationException("Unable to determine running assembly location");
  257. }
  258. programDataPath = Path.Combine(path, programDataPath);
  259. programDataPath = Path.GetFullPath(programDataPath);
  260. }
  261. Directory.CreateDirectory(programDataPath);
  262. return programDataPath;
  263. }
  264. }
  265. }