ApplicationPathHelper.cs 1.9 KB

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