BaseApplicationPaths.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Configuration;
  2. using System.IO;
  3. using System.Reflection;
  4. namespace MediaBrowser.Common.Configuration
  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 _logDirectoryPath;
  47. /// <summary>
  48. /// Gets the path to the log directory
  49. /// </summary>
  50. public string LogDirectoryPath
  51. {
  52. get
  53. {
  54. if (_logDirectoryPath == null)
  55. {
  56. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  57. if (!Directory.Exists(_logDirectoryPath))
  58. {
  59. Directory.CreateDirectory(_logDirectoryPath);
  60. }
  61. }
  62. return _logDirectoryPath;
  63. }
  64. }
  65. private string _configurationDirectoryPath;
  66. /// <summary>
  67. /// Gets the path to the application configuration root directory
  68. /// </summary>
  69. public string ConfigurationDirectoryPath
  70. {
  71. get
  72. {
  73. if (_configurationDirectoryPath == null)
  74. {
  75. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  76. if (!Directory.Exists(_configurationDirectoryPath))
  77. {
  78. Directory.CreateDirectory(_configurationDirectoryPath);
  79. }
  80. }
  81. return _configurationDirectoryPath;
  82. }
  83. }
  84. private string _systemConfigurationFilePath;
  85. /// <summary>
  86. /// Gets the path to the system configuration file
  87. /// </summary>
  88. public string SystemConfigurationFilePath
  89. {
  90. get
  91. {
  92. if (_systemConfigurationFilePath == null)
  93. {
  94. _systemConfigurationFilePath = Path.Combine(ConfigurationDirectoryPath, "system.xml");
  95. }
  96. return _systemConfigurationFilePath;
  97. }
  98. }
  99. /// <summary>
  100. /// Gets the path to the application's ProgramDataFolder
  101. /// </summary>
  102. private static string GetProgramDataPath()
  103. {
  104. string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
  105. // If it's a relative path, e.g. "..\"
  106. if (!Path.IsPathRooted(programDataPath))
  107. {
  108. string path = Assembly.GetExecutingAssembly().Location;
  109. path = Path.GetDirectoryName(path);
  110. programDataPath = Path.Combine(path, programDataPath);
  111. programDataPath = Path.GetFullPath(programDataPath);
  112. }
  113. if (!Directory.Exists(programDataPath))
  114. {
  115. Directory.CreateDirectory(programDataPath);
  116. }
  117. return programDataPath;
  118. }
  119. }
  120. }