BaseApplicationPaths.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.Configuration;
  8. using System.Reflection;
  9. namespace MediaBrowser.Common.Configuration
  10. {
  11. public abstract class BaseApplicationPaths
  12. {
  13. private string _programDataPath;
  14. /// <summary>
  15. /// Gets the path to the program data folder
  16. /// </summary>
  17. public string ProgramDataPath
  18. {
  19. get
  20. {
  21. if (_programDataPath == null)
  22. {
  23. _programDataPath = GetProgramDataPath();
  24. }
  25. return _programDataPath;
  26. }
  27. }
  28. private string _pluginsPath;
  29. /// <summary>
  30. /// Gets the path to the plugin directory
  31. /// </summary>
  32. public string PluginsPath
  33. {
  34. get
  35. {
  36. if (_pluginsPath == null)
  37. {
  38. _pluginsPath = Path.Combine(ProgramDataPath, "plugins");
  39. if (!Directory.Exists(_pluginsPath))
  40. {
  41. Directory.CreateDirectory(_pluginsPath);
  42. }
  43. }
  44. return _pluginsPath;
  45. }
  46. }
  47. private string _logDirectoryPath;
  48. /// <summary>
  49. /// Gets the path to the log directory
  50. /// </summary>
  51. public string LogDirectoryPath
  52. {
  53. get
  54. {
  55. if (_logDirectoryPath == null)
  56. {
  57. _logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
  58. if (!Directory.Exists(_logDirectoryPath))
  59. {
  60. Directory.CreateDirectory(_logDirectoryPath);
  61. }
  62. }
  63. return _logDirectoryPath;
  64. }
  65. }
  66. private string _configurationDirectoryPath;
  67. /// <summary>
  68. /// Gets the path to the application configuration root directory
  69. /// </summary>
  70. public string ConfigurationDirectoryPath
  71. {
  72. get
  73. {
  74. if (_configurationDirectoryPath == null)
  75. {
  76. _configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
  77. if (!Directory.Exists(_configurationDirectoryPath))
  78. {
  79. Directory.CreateDirectory(_configurationDirectoryPath);
  80. }
  81. }
  82. return _configurationDirectoryPath;
  83. }
  84. }
  85. private string _systemConfigurationFilePath;
  86. /// <summary>
  87. /// Gets the path to the system configuration file
  88. /// </summary>
  89. public string SystemConfigurationFilePath
  90. {
  91. get
  92. {
  93. if (_systemConfigurationFilePath == null)
  94. {
  95. _systemConfigurationFilePath = Path.Combine(ConfigurationDirectoryPath, "system.xml");
  96. }
  97. return _systemConfigurationFilePath;
  98. }
  99. }
  100. /// <summary>
  101. /// Gets the path to the application's ProgramDataFolder
  102. /// </summary>
  103. private static string GetProgramDataPath()
  104. {
  105. string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
  106. // If it's a relative path, e.g. "..\"
  107. if (!Path.IsPathRooted(programDataPath))
  108. {
  109. string path = Assembly.GetExecutingAssembly().Location;
  110. path = Path.GetDirectoryName(path);
  111. programDataPath = Path.Combine(path, programDataPath);
  112. programDataPath = Path.GetFullPath(programDataPath);
  113. }
  114. if (!Directory.Exists(programDataPath))
  115. {
  116. Directory.CreateDirectory(programDataPath);
  117. }
  118. return programDataPath;
  119. }
  120. }
  121. }