WebContentFolderHelper.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Abp.Reflection.Extensions;
  2. namespace VberAdmin.Web;
  3. /// <summary>
  4. /// This class is used to find root path of the web project in;
  5. /// unit tests (to find views) and entity framework core command line commands (to find conn string).
  6. /// </summary>
  7. public static class WebContentDirectoryFinder
  8. {
  9. /// <summary>
  10. /// 获取 WebMvc/WebHost 根目录
  11. /// </summary>
  12. /// <returns></returns>
  13. /// <exception cref="Exception"></exception>
  14. public static string CalculateContentRootFolder()
  15. {
  16. var coreAssemblyDirectoryPath = Path.GetDirectoryName(typeof(VberAdminCoreModule).GetAssembly().Location);
  17. if (coreAssemblyDirectoryPath == null)
  18. {
  19. throw new Exception("Could not find location of VberAdmin.Core assembly!");
  20. }
  21. var directoryInfo = new DirectoryInfo(coreAssemblyDirectoryPath);
  22. while (!DirectoryContains(directoryInfo.FullName, "VberAdmin_V4.0.1_2022.sln"))
  23. {
  24. directoryInfo = directoryInfo.Parent ?? throw new Exception("Could not find content root folder!");
  25. }
  26. var webMvcFolder = Path.Combine(directoryInfo.FullName, "src", "VberAdmin.Web.Mvc");
  27. if (Directory.Exists(webMvcFolder))
  28. {
  29. return webMvcFolder;
  30. }
  31. var webHostFolder = Path.Combine(directoryInfo.FullName, "src", "VberAdmin.Web.Host");
  32. if (Directory.Exists(webHostFolder))
  33. {
  34. return webHostFolder;
  35. }
  36. throw new Exception("Could not find root folder of the web project!");
  37. }
  38. private static bool DirectoryContains(string directory, string fileName)
  39. {
  40. return Directory.GetFiles(directory).Any(filePath => string.Equals(Path.GetFileName(filePath), fileName));
  41. }
  42. }