If you need the level then consider the following which given a level gets a parent folder according to the level provided so then combine that path with a file name, use File.Exists etc.
Helper class
using System;
using System.Collections.Generic;
using System.IO;
namespace Utilities.Classes
{
public static class DirectoryHelper
{
public static string UpperFolder(int level, string folderName = "")
{
if (string.IsNullOrWhiteSpace(folderName))
{
folderName = AppDomain.CurrentDomain.BaseDirectory;
}
var folderList = new List<string>();
while (!string.IsNullOrWhiteSpace(folderName))
{
var parentFolder = Directory.GetParent(folderName);
if (parentFolder == null)
{
break;
}
folderName = Directory.GetParent(folderName).FullName;
folderList.Add(folderName);
}
if (folderList.Count > 0 && level > 0)
{
return level - 1 <= folderList.Count - 1 ? folderList[level - 1] : folderName;
}
return folderName;
}
public static string CurrentSolutionFolder() => UpperFolder(4);
}
}
Add this using statement
using static Utilities.Classes.DirectoryHelper;
Simple usage
Debug.WriteLine(CurrentSolutionFolder());
Debug.WriteLine("");
for (int index = 0; index < 5; index++)
{
Debug.WriteLine($"{index}\t{UpperFolder(index)}");
}