ApplicationPaths.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(_configurationPath))
  42. {
  43. Directory.CreateDirectory(_configurationPath);
  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 IBNPath
  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 _pluginsPath;
  180. }
  181. }
  182. /// <summary>
  183. /// Gets the path to the application's ProgramDataFolder
  184. /// </summary>
  185. private static string GetProgramDataPath()
  186. {
  187. string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
  188. // If it's a relative path, e.g. "..\"
  189. if (!Path.IsPathRooted(programDataPath))
  190. {
  191. string path = Assembly.GetExecutingAssembly().Location;
  192. path = Path.GetDirectoryName(path);
  193. programDataPath = Path.Combine(path, programDataPath);
  194. programDataPath = Path.GetFullPath(programDataPath);
  195. }
  196. if (!Directory.Exists(programDataPath))
  197. {
  198. Directory.CreateDirectory(programDataPath);
  199. }
  200. return programDataPath;
  201. }
  202. }
  203. }