// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.IO; using System.Reflection; namespace Microsoft.AspNetCore.Mvc { public static class SolutionPathUtility { private const string SolutionName = "Mvc.sln"; /// /// Gets the full path to the project. /// /// /// The parent directory of the project. /// e.g. samples, test, or test/Websites /// /// The project's assembly. /// The full path to the project. public static string GetProjectPath(string solutionRelativePath, Assembly assembly) { var projectName = assembly.GetName().Name; var applicationBasePath = AppContext.BaseDirectory; var directoryInfo = new DirectoryInfo(applicationBasePath); do { var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName)); if (solutionFileInfo.Exists) { return Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath, projectName)); } directoryInfo = directoryInfo.Parent; } while (directoryInfo.Parent != null); throw new Exception($"Solution root could not be located using application root {applicationBasePath}."); } } }