ApplicationPaths.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.ComponentModel.Composition;
  8. using System.ComponentModel.Composition.Hosting;
  9. using System.Configuration;
  10. using System.Reflection;
  11. namespace MediaBrowser.Common.Configuration
  12. {
  13. public static class ApplicationPaths
  14. {
  15. private static string _programDataPath;
  16. /// <summary>
  17. /// Gets the path to the program data folder
  18. /// </summary>
  19. public static string ProgramDataPath
  20. {
  21. get
  22. {
  23. if (_programDataPath == null)
  24. {
  25. _programDataPath = GetProgramDataPath();
  26. }
  27. return _programDataPath;
  28. }
  29. }
  30. private static string _pluginsPath;
  31. /// <summary>
  32. /// Gets the path to the plugin directory
  33. /// </summary>
  34. public static string PluginsPath
  35. {
  36. get
  37. {
  38. if (_pluginsPath == null)
  39. {
  40. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  41. if (!Directory.Exists(_pluginsPath))
  42. {
  43. Directory.CreateDirectory(_pluginsPath);
  44. }
  45. }
  46. return _pluginsPath;
  47. }
  48. }
  49. private static string _configurationPath;
  50. /// <summary>
  51. /// Gets the path to the application configuration root directory
  52. /// </summary>
  53. public static string ConfigurationPath
  54. {
  55. get
  56. {
  57. if (_configurationPath == null)
  58. {
  59. _configurationPath = Path.Combine(ProgramDataPath, "config");
  60. if (!Directory.Exists(_configurationPath))
  61. {
  62. Directory.CreateDirectory(_configurationPath);
  63. }
  64. }
  65. return _configurationPath;
  66. }
  67. }
  68. private static string _systemConfigurationPath;
  69. /// <summary>
  70. /// Gets the path to the system configuration directory
  71. /// </summary>
  72. public static string SystemConfigurationPath
  73. {
  74. get
  75. {
  76. if (_systemConfigurationPath == null)
  77. {
  78. _systemConfigurationPath = Path.Combine(ConfigurationPath, "system");
  79. if (!Directory.Exists(_systemConfigurationPath))
  80. {
  81. Directory.CreateDirectory(_systemConfigurationPath);
  82. }
  83. }
  84. return _systemConfigurationPath;
  85. }
  86. }
  87. private static string _userConfigurationPath;
  88. /// <summary>
  89. /// Gets the path to the user configuration directory
  90. /// </summary>
  91. public static string UserConfigurationPath
  92. {
  93. get
  94. {
  95. if (_userConfigurationPath == null)
  96. {
  97. _userConfigurationPath = Path.Combine(ConfigurationPath, "user");
  98. if (!Directory.Exists(_userConfigurationPath))
  99. {
  100. Directory.CreateDirectory(_userConfigurationPath);
  101. }
  102. }
  103. return _userConfigurationPath;
  104. }
  105. }
  106. private static string _deviceConfigurationPath;
  107. /// <summary>
  108. /// Gets the path to the device configuration directory
  109. /// </summary>
  110. public static string DeviceConfigurationPath
  111. {
  112. get
  113. {
  114. if (_deviceConfigurationPath == null)
  115. {
  116. _deviceConfigurationPath = Path.Combine(ConfigurationPath, "device");
  117. if (!Directory.Exists(_deviceConfigurationPath))
  118. {
  119. Directory.CreateDirectory(_deviceConfigurationPath);
  120. }
  121. }
  122. return _deviceConfigurationPath;
  123. }
  124. }
  125. private static string _logDirectoryPath;
  126. /// <summary>
  127. /// Gets the path to the log directory
  128. /// </summary>
  129. public static string LogDirectoryPath
  130. {
  131. get
  132. {
  133. if (_logDirectoryPath == null)
  134. {
  135. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  136. if (!Directory.Exists(_logDirectoryPath))
  137. {
  138. Directory.CreateDirectory(_logDirectoryPath);
  139. }
  140. }
  141. return _logDirectoryPath;
  142. }
  143. }
  144. private static string _rootFolderPath;
  145. /// <summary>
  146. /// Gets the path to the root media directory
  147. /// </summary>
  148. public static string RootFolderPath
  149. {
  150. get
  151. {
  152. if (_rootFolderPath == null)
  153. {
  154. _rootFolderPath = Path.Combine(ProgramDataPath, "root");
  155. if (!Directory.Exists(_rootFolderPath))
  156. {
  157. Directory.CreateDirectory(_rootFolderPath);
  158. }
  159. }
  160. return _rootFolderPath;
  161. }
  162. }
  163. private static string _ibnPath;
  164. /// <summary>
  165. /// Gets the path to the Images By Name directory
  166. /// </summary>
  167. public static string ImagesByNamePath
  168. {
  169. get
  170. {
  171. if (_ibnPath == null)
  172. {
  173. _ibnPath = Path.Combine(ProgramDataPath, "ImagesByName");
  174. if (!Directory.Exists(_ibnPath))
  175. {
  176. Directory.CreateDirectory(_ibnPath);
  177. }
  178. }
  179. return _ibnPath;
  180. }
  181. }
  182. private static string _PeoplePath;
  183. /// <summary>
  184. /// Gets the path to the People directory
  185. /// </summary>
  186. public static string PeoplePath
  187. {
  188. get
  189. {
  190. if (_PeoplePath == null)
  191. {
  192. _PeoplePath = Path.Combine(ImagesByNamePath, "People");
  193. if (!Directory.Exists(_PeoplePath))
  194. {
  195. Directory.CreateDirectory(_PeoplePath);
  196. }
  197. }
  198. return _PeoplePath;
  199. }
  200. }
  201. private static string _GenrePath;
  202. /// <summary>
  203. /// Gets the path to the Genre directory
  204. /// </summary>
  205. public static string GenrePath
  206. {
  207. get
  208. {
  209. if (_GenrePath == null)
  210. {
  211. _GenrePath = Path.Combine(ImagesByNamePath, "Genre");
  212. if (!Directory.Exists(_GenrePath))
  213. {
  214. Directory.CreateDirectory(_GenrePath);
  215. }
  216. }
  217. return _GenrePath;
  218. }
  219. }
  220. private static string _StudioPath;
  221. /// <summary>
  222. /// Gets the path to the Studio directory
  223. /// </summary>
  224. public static string StudioPath
  225. {
  226. get
  227. {
  228. if (_StudioPath == null)
  229. {
  230. _StudioPath = Path.Combine(ImagesByNamePath, "Studio");
  231. if (!Directory.Exists(_StudioPath))
  232. {
  233. Directory.CreateDirectory(_StudioPath);
  234. }
  235. }
  236. return _StudioPath;
  237. }
  238. }
  239. private static string _yearPath;
  240. /// <summary>
  241. /// Gets the path to the Year directory
  242. /// </summary>
  243. public static string YearPath
  244. {
  245. get
  246. {
  247. if (_yearPath == null)
  248. {
  249. _yearPath = Path.Combine(ImagesByNamePath, "Year");
  250. if (!Directory.Exists(_yearPath))
  251. {
  252. Directory.CreateDirectory(_yearPath);
  253. }
  254. }
  255. return _yearPath;
  256. }
  257. }
  258. /// <summary>
  259. /// Gets the path to the application's ProgramDataFolder
  260. /// </summary>
  261. private static string GetProgramDataPath()
  262. {
  263. string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
  264. // If it's a relative path, e.g. "..\"
  265. if (!Path.IsPathRooted(programDataPath))
  266. {
  267. string path = Assembly.GetExecutingAssembly().Location;
  268. path = Path.GetDirectoryName(path);
  269. programDataPath = Path.Combine(path, programDataPath);
  270. programDataPath = Path.GetFullPath(programDataPath);
  271. }
  272. if (!Directory.Exists(programDataPath))
  273. {
  274. Directory.CreateDirectory(programDataPath);
  275. }
  276. return programDataPath;
  277. }
  278. }
  279. }