ApplicationPaths.cs 9.1 KB

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