ApplicationPathHelper.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Configuration;
  3. using System.IO;
  4. namespace MediaBrowser.Server.Startup.Common
  5. {
  6. public static class ApplicationPathHelper
  7. {
  8. /// <summary>
  9. /// Gets the path to the application's ProgramDataFolder
  10. /// </summary>
  11. /// <returns>System.String.</returns>
  12. public static string GetProgramDataPath(string applicationPath)
  13. {
  14. var useDebugPath = false;
  15. #if DEBUG
  16. useDebugPath = true;
  17. #endif
  18. var programDataPath = useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : ConfigurationManager.AppSettings["ReleaseProgramDataPath"];
  19. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  20. // If it's a relative path, e.g. "..\"
  21. if (!Path.IsPathRooted(programDataPath))
  22. {
  23. var path = Path.GetDirectoryName(applicationPath);
  24. if (string.IsNullOrEmpty(path))
  25. {
  26. throw new ApplicationException("Unable to determine running assembly location");
  27. }
  28. programDataPath = Path.Combine(path, programDataPath);
  29. programDataPath = Path.GetFullPath(programDataPath);
  30. }
  31. Directory.CreateDirectory(programDataPath);
  32. return programDataPath;
  33. }
  34. }
  35. }