BaseApplicationPaths.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /// Gets the path to the plugin directory
  86. /// </summary>
  87. /// <value>The plugins path.</value>
  88. public string PluginsPath
  89. {
  90. get
  91. {
  92. return Path.Combine(ProgramDataPath, "plugins");
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the path to the plugin configurations directory
  97. /// </summary>
  98. /// <value>The plugin configurations path.</value>
  99. public string PluginConfigurationsPath
  100. {
  101. get
  102. {
  103. return Path.Combine(PluginsPath, "configurations");
  104. }
  105. }
  106. /// <summary>
  107. /// Gets the path to where temporary update files will be stored
  108. /// </summary>
  109. /// <value>The plugin configurations path.</value>
  110. public string TempUpdatePath
  111. {
  112. get
  113. {
  114. return Path.Combine(ProgramDataPath, "updates");
  115. }
  116. }
  117. /// <summary>
  118. /// Gets the path to the log directory
  119. /// </summary>
  120. /// <value>The log directory path.</value>
  121. public string LogDirectoryPath
  122. {
  123. get
  124. {
  125. return Path.Combine(ProgramDataPath, "logs");
  126. }
  127. }
  128. /// <summary>
  129. /// The _configuration directory path
  130. /// </summary>
  131. private string _configurationDirectoryPath;
  132. /// <summary>
  133. /// Gets the path to the application configuration root directory
  134. /// </summary>
  135. /// <value>The configuration directory path.</value>
  136. public string ConfigurationDirectoryPath
  137. {
  138. get
  139. {
  140. if (_configurationDirectoryPath == null)
  141. {
  142. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  143. Directory.CreateDirectory(_configurationDirectoryPath);
  144. }
  145. return _configurationDirectoryPath;
  146. }
  147. }
  148. /// <summary>
  149. /// Gets the path to the system configuration file
  150. /// </summary>
  151. /// <value>The system configuration file path.</value>
  152. public string SystemConfigurationFilePath
  153. {
  154. get
  155. {
  156. return Path.Combine(ConfigurationDirectoryPath, "system.xml");
  157. }
  158. }
  159. /// <summary>
  160. /// The _cache directory
  161. /// </summary>
  162. private string _cachePath;
  163. /// <summary>
  164. /// Gets the folder path to the cache directory
  165. /// </summary>
  166. /// <value>The cache directory.</value>
  167. public string CachePath
  168. {
  169. get
  170. {
  171. if (string.IsNullOrEmpty(_cachePath))
  172. {
  173. _cachePath = Path.Combine(ProgramDataPath, "cache");
  174. Directory.CreateDirectory(_cachePath);
  175. }
  176. return _cachePath;
  177. }
  178. set
  179. {
  180. _cachePath = value;
  181. }
  182. }
  183. /// <summary>
  184. /// Gets the folder path to the temp directory within the cache folder
  185. /// </summary>
  186. /// <value>The temp directory.</value>
  187. public string TempDirectory
  188. {
  189. get
  190. {
  191. return Path.Combine(CachePath, "temp");
  192. }
  193. }
  194. /// <summary>
  195. /// Gets the path to the application's ProgramDataFolder
  196. /// </summary>
  197. /// <returns>System.String.</returns>
  198. private string GetProgramDataPath()
  199. {
  200. var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
  201. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  202. // If it's a relative path, e.g. "..\"
  203. if (!Path.IsPathRooted(programDataPath))
  204. {
  205. var path = Path.GetDirectoryName(ApplicationPath);
  206. if (string.IsNullOrEmpty(path))
  207. {
  208. throw new ApplicationException("Unable to determine running assembly location");
  209. }
  210. programDataPath = Path.Combine(path, programDataPath);
  211. programDataPath = Path.GetFullPath(programDataPath);
  212. }
  213. Directory.CreateDirectory(programDataPath);
  214. return programDataPath;
  215. }
  216. }
  217. }