string GetRootMostDirectoryContainingWebConfig(RunningDeployment deployment) { // Optimize for most common case. if (fileSystem.FileExists(Path.Combine(deployment.CurrentDirectory, "Web.config"))) { return(deployment.CurrentDirectory); } // Find all folders under package root and sort them by depth var dirs = fileSystem.EnumerateDirectoriesRecursively(deployment.CurrentDirectory).ToList(); return(dirs.OrderBy(x => x.Count(c => c == '\\')).FirstOrDefault(dir => fileSystem.FileExists(Path.Combine(dir, "Web.config")))); }