BaseApplicationPaths.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /// Gets the path to the application configuration root directory
  130. /// </summary>
  131. /// <value>The configuration directory path.</value>
  132. public string ConfigurationDirectoryPath
  133. {
  134. get
  135. {
  136. return Path.Combine(ProgramDataPath, "config");
  137. }
  138. }
  139. /// <summary>
  140. /// Gets the path to the system configuration file
  141. /// </summary>
  142. /// <value>The system configuration file path.</value>
  143. public string SystemConfigurationFilePath
  144. {
  145. get
  146. {
  147. return Path.Combine(ConfigurationDirectoryPath, "system.xml");
  148. }
  149. }
  150. /// <summary>
  151. /// The _cache directory
  152. /// </summary>
  153. private string _cachePath;
  154. /// <summary>
  155. /// Gets the folder path to the cache directory
  156. /// </summary>
  157. /// <value>The cache directory.</value>
  158. public string CachePath
  159. {
  160. get
  161. {
  162. if (string.IsNullOrEmpty(_cachePath))
  163. {
  164. _cachePath = Path.Combine(ProgramDataPath, "cache");
  165. Directory.CreateDirectory(_cachePath);
  166. }
  167. return _cachePath;
  168. }
  169. set
  170. {
  171. _cachePath = value;
  172. }
  173. }
  174. /// <summary>
  175. /// Gets the folder path to the temp directory within the cache folder
  176. /// </summary>
  177. /// <value>The temp directory.</value>
  178. public string TempDirectory
  179. {
  180. get
  181. {
  182. return Path.Combine(CachePath, "temp");
  183. }
  184. }
  185. /// <summary>
  186. /// Gets the path to the application's ProgramDataFolder
  187. /// </summary>
  188. /// <returns>System.String.</returns>
  189. private string GetProgramDataPath()
  190. {
  191. var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : ConfigurationManager.AppSettings["ReleaseProgramDataPath"];
  192. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  193. // If it's a relative path, e.g. "..\"
  194. if (!Path.IsPathRooted(programDataPath))
  195. {
  196. var path = Path.GetDirectoryName(ApplicationPath);
  197. if (string.IsNullOrEmpty(path))
  198. {
  199. throw new ApplicationException("Unable to determine running assembly location");
  200. }
  201. programDataPath = Path.Combine(path, programDataPath);
  202. programDataPath = Path.GetFullPath(programDataPath);
  203. }
  204. Directory.CreateDirectory(programDataPath);
  205. return programDataPath;
  206. }
  207. }
  208. }