ApplicationPathHelper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 ?
  19. ConfigurationManager.AppSettings["DebugProgramDataPath"] :
  20. ConfigurationManager.AppSettings["ReleaseProgramDataPath"];
  21. programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  22. programDataPath = programDataPath
  23. .Replace('/', Path.DirectorySeparatorChar)
  24. .Replace('\\', Path.DirectorySeparatorChar);
  25. // If it's a relative path, e.g. "..\"
  26. if (!Path.IsPathRooted(programDataPath))
  27. {
  28. var path = Path.GetDirectoryName(applicationPath);
  29. if (string.IsNullOrEmpty(path))
  30. {
  31. throw new ApplicationException("Unable to determine running assembly location");
  32. }
  33. programDataPath = Path.Combine(path, programDataPath);
  34. programDataPath = Path.GetFullPath(programDataPath);
  35. }
  36. Directory.CreateDirectory(programDataPath);
  37. return programDataPath;
  38. }
  39. }
  40. }