static public FileInfo FindFile(string s, DirectoryInfo SrcDir = null) { if (SrcDir == null) { SrcDir = new DirectoryInfo(Environment.CurrentDirectory); } if (SrcDir.Exists) { var file = SrcDir.GetFiles().Where(f => f.Name == s).FirstOrDefault(); if (file != null) { return(file); } var lsDir = SrcDir.GetDirectories(); foreach (var temp in lsDir) { file = FindFile(s, temp); if (file != null) { return(file); } } } return(null); }
static public DirectoryInfo FindDir(string s, DirectoryInfo SrcDir = null) { if (SrcDir == null) { SrcDir = new DirectoryInfo(Environment.CurrentDirectory); } if (SrcDir.Exists) { var lsDir = SrcDir.GetDirectories(); var dir = lsDir.Where(f => f.Name == s).FirstOrDefault(); if (dir != null) { return(dir); } foreach (var temp in lsDir) { dir = FindDir(s, temp); if (dir != null) { return(dir); } } } return(null); }
/// <summary> /// Attempts to resolve an included file path by checking all the IncludeFolder and SrcDir to get an absolute /// path to the file. /// </summary> string IncludedFileToAbsPath(string path) { foreach (var incPath in ToAbsolutePaths(IncludeFolders)) { var tmp = Path.Combine(incPath, path); if (File.Exists(tmp)) { return(tmp); } } if (!Path.IsPathRooted(SrcDir)) { SrcDir = SrcDir.Replace("~", Environment.GetFolderPath(Environment.SpecialFolder.Personal)); } var newPath = Path.Combine(SrcDir, path); if (!File.Exists(newPath)) { throw new FileNotFoundException($"Could not find file {path} from the Files array. Maybe your {nameof(IncludeFolders)} are not correct?"); } return(newPath); }