BaseApplicationPaths.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Configuration;
  2. using System.IO;
  3. using System.Reflection;
  4. namespace MediaBrowser.Common.Kernel
  5. {
  6. /// <summary>
  7. /// Provides a base class to hold common application paths used by both the Ui and Server.
  8. /// This can be subclassed to add application-specific paths.
  9. /// </summary>
  10. public abstract class BaseApplicationPaths
  11. {
  12. private string _programDataPath;
  13. /// <summary>
  14. /// Gets the path to the program data folder
  15. /// </summary>
  16. public string ProgramDataPath
  17. {
  18. get
  19. {
  20. if (_programDataPath == null)
  21. {
  22. _programDataPath = GetProgramDataPath();
  23. }
  24. return _programDataPath;
  25. }
  26. }
  27. private string _pluginsPath;
  28. /// <summary>
  29. /// Gets the path to the plugin directory
  30. /// </summary>
  31. public string PluginsPath
  32. {
  33. get
  34. {
  35. if (_pluginsPath == null)
  36. {
  37. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  38. if (!Directory.Exists(_pluginsPath))
  39. {
  40. Directory.CreateDirectory(_pluginsPath);
  41. }
  42. }
  43. return _pluginsPath;
  44. }
  45. }
  46. private string _pluginConfigurationsPath;
  47. /// <summary>
  48. /// Gets the path to the plugin configurations directory
  49. /// </summary>
  50. public string PluginConfigurationsPath
  51. {
  52. get
  53. {
  54. if (_pluginConfigurationsPath == null)
  55. {
  56. _pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
  57. if (!Directory.Exists(_pluginConfigurationsPath))
  58. {
  59. Directory.CreateDirectory(_pluginConfigurationsPath);
  60. }
  61. }
  62. return _pluginConfigurationsPath;
  63. }
  64. }
  65. private string _logDirectoryPath;
  66. /// <summary>
  67. /// Gets the path to the log directory
  68. /// </summary>
  69. public string LogDirectoryPath
  70. {
  71. get
  72. {
  73. if (_logDirectoryPath == null)
  74. {
  75. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  76. if (!Directory.Exists(_logDirectoryPath))
  77. {
  78. Directory.CreateDirectory(_logDirectoryPath);
  79. }
  80. }
  81. return _logDirectoryPath;
  82. }
  83. }
  84. private string _configurationDirectoryPath;
  85. /// <summary>
  86. /// Gets the path to the application configuration root directory
  87. /// </summary>
  88. public string ConfigurationDirectoryPath
  89. {
  90. get
  91. {
  92. if (_configurationDirectoryPath == null)
  93. {
  94. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  95. if (!Directory.Exists(_configurationDirectoryPath))
  96. {
  97. Directory.CreateDirectory(_configurationDirectoryPath);
  98. }
  99. }
  100. return _configurationDirectoryPath;
  101. }
  102. }
  103. private string _systemConfigurationFilePath;
  104. /// <summary>
  105. /// Gets the path to the system configuration file
  106. /// </summary>
  107. public string SystemConfigurationFilePath
  108. {
  109. get
  110. {
  111. if (_systemConfigurationFilePath == null)
  112. {
  113. _systemConfigurationFilePath = Path.Combine(ConfigurationDirectoryPath, "system.xml");
  114. }
  115. return _systemConfigurationFilePath;
  116. }
  117. }
  118. /// <summary>
  119. /// Gets the path to the application's ProgramDataFolder
  120. /// </summary>
  121. private static string GetProgramDataPath()
  122. {
  123. string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
  124. // If it's a relative path, e.g. "..\"
  125. if (!Path.IsPathRooted(programDataPath))
  126. {
  127. string path = Assembly.GetExecutingAssembly().Location;
  128. path = Path.GetDirectoryName(path);
  129. programDataPath = Path.Combine(path, programDataPath);
  130. programDataPath = Path.GetFullPath(programDataPath);
  131. }
  132. if (!Directory.Exists(programDataPath))
  133. {
  134. Directory.CreateDirectory(programDataPath);
  135. }
  136. return programDataPath;
  137. }
  138. }
  139. }