using System; using System.IO; using System.Linq; namespace Business.Core.Web { /// /// This class is used to find root path of the web project in; /// unit tests (to find views) and entity framework core command line commands (to find conn string). /// public static class WebContentDirectoryFinder { public static string CalculateContentRootFolder() { var coreAssemblyDirectoryPath = Path.GetDirectoryName(AppContext.BaseDirectory); if (coreAssemblyDirectoryPath == null) { throw new Exception("Could not find location of Chemical.Ai.Core assembly!"); } DirectoryInfo directoryInfo = new DirectoryInfo(coreAssemblyDirectoryPath); while (!DirectoryContains(directoryInfo.FullName, "Bussiness.sln")) { //发布状态下,应该为当前目录 if (directoryInfo.Parent == null) { return coreAssemblyDirectoryPath; //throw new Exception($"Could not find content root folder!"); } directoryInfo = directoryInfo.Parent; } var webHostFolder = Path.Combine(directoryInfo.FullName, "src", "Bussiness.Host"); if (Directory.Exists(webHostFolder)) { return webHostFolder; } throw new Exception("Could not find root folder of the web project!"); } private static bool DirectoryContains(string directory, string fileName) { return Directory.GetFiles(directory).Any(filePath => string.Equals(Path.GetFileName(filePath), fileName)); } } }