Skip to content

Instantly share code, notes, and snippets.

@searope
Created December 18, 2019 20:20
Show Gist options
  • Select an option

  • Save searope/dcdd0b7eac2ebbba7a8883d9dc77d8ec to your computer and use it in GitHub Desktop.

Select an option

Save searope/dcdd0b7eac2ebbba7a8883d9dc77d8ec to your computer and use it in GitHub Desktop.
Finds file or folder in parents folder
public static string FindUpperFileOrFolder(string name, DirectoryInfo dir = null)
{
dir = dir ?? new DirectoryInfo(Directory.GetCurrentDirectory());
var dirInfo = dir.GetDirectories(name).FirstOrDefault();
var fileInfo = dir.GetFiles(name).FirstOrDefault();
if (fileInfo == null && dirInfo == null)
{
return FindUpperFileOrFolder(name, dir.Parent);
}
return fileInfo != null ? fileInfo.FullName : dirInfo.FullName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment