WebContentFolderHelper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. namespace Business.Core.Web
  5. {
  6. /// <summary>
  7. /// This class is used to find root path of the web project in;
  8. /// unit tests (to find views) and entity framework core command line commands (to find conn string).
  9. /// </summary>
  10. public static class WebContentDirectoryFinder
  11. {
  12. public static string CalculateContentRootFolder()
  13. {
  14. var coreAssemblyDirectoryPath = Path.GetDirectoryName(AppContext.BaseDirectory);
  15. if (coreAssemblyDirectoryPath == null)
  16. {
  17. throw new Exception("Could not find location of Chemical.Ai.Core assembly!");
  18. }
  19. DirectoryInfo directoryInfo = new DirectoryInfo(coreAssemblyDirectoryPath);
  20. while (!DirectoryContains(directoryInfo.FullName, "Bussiness.sln"))
  21. {
  22. //发布状态下,应该为当前目录
  23. if (directoryInfo.Parent == null)
  24. {
  25. return coreAssemblyDirectoryPath;
  26. //throw new Exception($"Could not find content root folder!");
  27. }
  28. directoryInfo = directoryInfo.Parent;
  29. }
  30. var webHostFolder = Path.Combine(directoryInfo.FullName, "src", "Bussiness.Host");
  31. if (Directory.Exists(webHostFolder))
  32. {
  33. return webHostFolder;
  34. }
  35. throw new Exception("Could not find root folder of the web project!");
  36. }
  37. private static bool DirectoryContains(string directory, string fileName)
  38. {
  39. return Directory.GetFiles(directory).Any(filePath => string.Equals(Path.GetFileName(filePath), fileName));
  40. }
  41. }
  42. }