ApplicationPaths.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. InitDirectories(); //move this here so we don't have to check for existence on every directory reference
  27. }
  28. return _programDataPath;
  29. }
  30. }
  31. private static void InitDirectories()
  32. {
  33. if (!Directory.Exists(LogDirectoryPath))
  34. {
  35. Directory.CreateDirectory(LogDirectoryPath);
  36. }
  37. if (!Directory.Exists(PluginsPath))
  38. {
  39. Directory.CreateDirectory(PluginsPath);
  40. }
  41. if (!Directory.Exists(RootFolderPath))
  42. {
  43. Directory.CreateDirectory(RootFolderPath);
  44. }
  45. if (!Directory.Exists(ConfigurationPath))
  46. {
  47. Directory.CreateDirectory(ConfigurationPath);
  48. Directory.CreateDirectory(SystemConfigurationPath);
  49. Directory.CreateDirectory(DeviceConfigurationPath);
  50. Directory.CreateDirectory(UserConfigurationPath);
  51. }
  52. }
  53. /// <summary>
  54. /// Gets the path to the plugin directory
  55. /// </summary>
  56. public static string PluginsPath
  57. {
  58. get
  59. {
  60. return Path.Combine(ProgramDataPath, "plugins");
  61. }
  62. }
  63. /// <summary>
  64. /// Gets the path to the application configuration root directory
  65. /// </summary>
  66. public static string ConfigurationPath
  67. {
  68. get
  69. {
  70. return Path.Combine(ProgramDataPath, "config");
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the path to the system configuration directory
  75. /// </summary>
  76. public static string SystemConfigurationPath
  77. {
  78. get
  79. {
  80. return Path.Combine(ConfigurationPath, "system");
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the path to the user configuration directory
  85. /// </summary>
  86. public static string UserConfigurationPath
  87. {
  88. get
  89. {
  90. return Path.Combine(ConfigurationPath, "user");
  91. }
  92. }
  93. /// <summary>
  94. /// Gets the path to the device configuration directory
  95. /// </summary>
  96. public static string DeviceConfigurationPath
  97. {
  98. get
  99. {
  100. return Path.Combine(ConfigurationPath, "device");
  101. }
  102. }
  103. /// <summary>
  104. /// Gets the path to the log directory
  105. /// </summary>
  106. public static string LogDirectoryPath
  107. {
  108. get
  109. {
  110. return Path.Combine(ProgramDataPath, "logs");
  111. }
  112. }
  113. /// <summary>
  114. /// Gets the path to the root media directory
  115. /// </summary>
  116. public static string RootFolderPath
  117. {
  118. get
  119. {
  120. return Path.Combine(ProgramDataPath, "root");
  121. }
  122. }
  123. /// <summary>
  124. /// Gets the path to the application's ProgramDataFolder
  125. /// </summary>
  126. private static string GetProgramDataPath()
  127. {
  128. string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
  129. // If it's a relative path, e.g. "..\"
  130. if (!Path.IsPathRooted(programDataPath))
  131. {
  132. string path = Assembly.GetExecutingAssembly().Location;
  133. path = Path.GetDirectoryName(path);
  134. programDataPath = Path.Combine(path, programDataPath);
  135. programDataPath = Path.GetFullPath(programDataPath);
  136. }
  137. if (!Directory.Exists(programDataPath))
  138. {
  139. Directory.CreateDirectory(programDataPath);
  140. }
  141. return programDataPath;
  142. }
  143. }
  144. }