BaseApplicationPaths.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using MediaBrowser.Common.Configuration;
  2. using System;
  3. using System.Configuration;
  4. using System.IO;
  5. namespace MediaBrowser.Common.Implementations
  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 : IApplicationPaths
  12. {
  13. /// <summary>
  14. /// The _use debug path
  15. /// </summary>
  16. private readonly bool _useDebugPath;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="BaseApplicationPaths" /> class.
  19. /// </summary>
  20. protected BaseApplicationPaths(bool useDebugPath, string applicationPath)
  21. {
  22. _useDebugPath = useDebugPath;
  23. ApplicationPath = applicationPath;
  24. }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
  27. /// </summary>
  28. protected BaseApplicationPaths(string programDataPath, string applicationPath)
  29. {
  30. _programDataPath = programDataPath;
  31. ApplicationPath = applicationPath;
  32. }
  33. public string ApplicationPath { get; private set; }
  34. /// <summary>
  35. /// The _program data path
  36. /// </summary>
  37. private string _programDataPath;
  38. /// <summary>
  39. /// Gets the path to the program data folder
  40. /// </summary>
  41. /// <value>The program data path.</value>
  42. public string ProgramDataPath
  43. {
  44. get
  45. {
  46. return _programDataPath ?? (_programDataPath = GetProgramDataPath());
  47. }
  48. }
  49. /// <summary>
  50. /// Gets the path to the system folder
  51. /// </summary>
  52. public string ProgramSystemPath { get { return Path.Combine(ProgramDataPath, "system"); } }
  53. /// <summary>
  54. /// The _data directory
  55. /// </summary>
  56. private string _dataDirectory;
  57. /// <summary>
  58. /// Gets the folder path to the data directory
  59. /// </summary>
  60. /// <value>The data directory.</value>
  61. public string DataPath
  62. {
  63. get
  64. {
  65. if (_dataDirectory == null)
  66. {
  67. _dataDirectory = Path.Combine(ProgramDataPath, "data");
  68. Directory.CreateDirectory(_dataDirectory);
  69. }
  70. return _dataDirectory;
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the image cache path.
  75. /// </summary>
  76. /// <value>The image cache path.</value>
  77. public string ImageCachePath
  78. {
  79. get
  80. {
  81. return Path.Combine(CachePath, "images");
  82. }
  83. }
  84. /// <summary>
  85. /// The _plugins path
  86. /// </summary>
  87. private string _pluginsPath;
  88. /// <summary>
  89. /// Gets the path to the plugin directory
  90. /// </summary>
  91. /// <value>The plugins path.</value>
  92. public string PluginsPath
  93. {
  94. get
  95. {
  96. if (_pluginsPath == null)
  97. {
  98. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  99. Directory.CreateDirectory(_pluginsPath);
  100. }
  101. return _pluginsPath;
  102. }
  103. }
  104. /// <summary>
  105. /// The _plugin configurations path
  106. /// </summary>
  107. private string _pluginConfigurationsPath;
  108. /// <summary>
  109. /// Gets the path to the plugin configurations directory
  110. /// </summary>
  111. /// <value>The plugin configurations path.</value>
  112. public string PluginConfigurationsPath
  113. {
  114. get
  115. {
  116. if (_pluginConfigurationsPath == null)
  117. {
  118. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  119. Directory.CreateDirectory(_pluginConfigurationsPath);
  120. }
  121. return _pluginConfigurationsPath;
  122. }
  123. }
  124. private string _tempUpdatePath;
  125. /// <summary>
  126. /// Gets the path to where temporary update files will be stored
  127. /// </summary>
  128. /// <value>The plugin configurations path.</value>
  129. public string TempUpdatePath
  130. {
  131. get
  132. {
  133. if (_tempUpdatePath == null)
  134. {
  135. _tempUpdatePath = Path.Combine(ProgramDataPath, "updates");
  136. Directory.CreateDirectory(_tempUpdatePath);
  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. Directory.CreateDirectory(_logDirectoryPath);
  157. }
  158. return _logDirectoryPath;
  159. }
  160. }
  161. /// <summary>
  162. /// The _configuration directory path
  163. /// </summary>
  164. private string _configurationDirectoryPath;
  165. /// <summary>
  166. /// Gets the path to the application configuration root directory
  167. /// </summary>
  168. /// <value>The configuration directory path.</value>
  169. public string ConfigurationDirectoryPath
  170. {
  171. get
  172. {
  173. if (_configurationDirectoryPath == null)
  174. {
  175. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  176. Directory.CreateDirectory(_configurationDirectoryPath);
  177. }
  178. return _configurationDirectoryPath;
  179. }
  180. }
  181. /// <summary>
  182. /// Gets the path to the system configuration file
  183. /// </summary>
  184. /// <value>The system configuration file path.</value>
  185. public string SystemConfigurationFilePath
  186. {
  187. get
  188. {
  189. return Path.Combine(ConfigurationDirectoryPath, "system.xml");
  190. }
  191. }
  192. /// <summary>
  193. /// The _cache directory
  194. /// </summary>
  195. private string _cachePath;
  196. /// <summary>
  197. /// Gets the folder path to the cache directory
  198. /// </summary>
  199. /// <value>The cache directory.</value>
  200. public string CachePath
  201. {
  202. get
  203. {
  204. if (string.IsNullOrEmpty(_cachePath))
  205. {
  206. _cachePath = Path.Combine(ProgramDataPath, "cache");
  207. Directory.CreateDirectory(_cachePath);
  208. }
  209. return _cachePath;
  210. }
  211. set
  212. {
  213. _cachePath = value;
  214. }
  215. }
  216. /// <summary>
  217. /// Gets the folder path to the temp directory within the cache folder
  218. /// </summary>
  219. /// <value>The temp directory.</value>
  220. public string TempDirectory
  221. {
  222. get
  223. {
  224. return Path.Combine(CachePath, "temp");
  225. }
  226. }
  227. /// <summary>
  228. /// Gets the path to the application's ProgramDataFolder
  229. /// </summary>
  230. /// <returns>System.String.</returns>
  231. private string GetProgramDataPath()
  232. {
  233. var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
  234. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  235. // If it's a relative path, e.g. "..\"
  236. if (!Path.IsPathRooted(programDataPath))
  237. {
  238. var path = Path.GetDirectoryName(ApplicationPath);
  239. if (string.IsNullOrEmpty(path))
  240. {
  241. throw new ApplicationException("Unable to determine running assembly location");
  242. }
  243. programDataPath = Path.Combine(path, programDataPath);
  244. programDataPath = Path.GetFullPath(programDataPath);
  245. }
  246. Directory.CreateDirectory(programDataPath);
  247. return programDataPath;
  248. }
  249. }
  250. }