# Check if a file exists in a directory or parent

I want to check if a file exists in a given directory or in the parent one, so I wrote this code:

if (File.Exists(fileName) ||
File.Exists(Directory.GetParent(Path.GetDirectoryName(fileName)).FullName +
Path.DirectorySeperatorChar +
Path.GetFileName(fileName)))


However, this seems quite odd to me and I wonder if there is a smarter approach to making this check. I'm not necessarily looking for a shorter approach, but a more understandable one.

• A one-line code review ;-) I like it. – t3chb0t Jul 4 '16 at 7:25
• Out of curiosity, what if the file name existed in the current and parent directory. What do you do in that scenario – Siobhan Jul 4 '16 at 8:37
• @TolaniJaiye-Tikolo Throw an excpetion I guess. However I have to admit that I did not think about this scenario as it is irrelevant to me and only adds further complexity to the question that I don´t need (so far). – HimBromBeere Jul 4 '16 at 8:54
• yes I presume but in that scenario if the file name existed in both directories then the current directory file will take precedence because it gets evaluated before the parent. Am trying to get you to think of exceptions anyway. – Siobhan Jul 4 '16 at 8:59
• I generally find that these become much easier to understand if you work with DirectoryInfo and FileInfo rather than using the File and Directory static classes – MikeT Jul 4 '16 at 11:58

It's safer to use the Path.Combine method for joining the directory name and file name:

var fileName = @"c:\temp\foo.txt";

var fileExists =
File.Exists(fileName) ||
File.Exists(
Path.Combine(
Directory.GetParent(Path.GetDirectoryName(fileName)).FullName,
Path.GetFileName(fileName)
)
);


What else you could to is to take it out of the if and use a helper variable and add some identation.

Another approach could be to encapsulate the search paths and the file exists check:

static IEnumerable<string> GetFileSearchPaths(string fileName)
{
yield return fileName;
yield return Path.Combine(
Directory.GetParent(Path.GetDirectoryName(fileName)).FullName,
Path.GetFileName(fileName)
);
}

// or as an extension
static bool FileExists(string fileName)
{
return GetFileSearchPaths(fileName).Any(File.Exists);
}


Usage:

if(FileExists(fileName)) ...


or you can get the actual file name from the search:

static string FindFile(this string fileName)
{
return GetFileSearchPaths(fileName).FirstOrDefault(x => File.Exists(x));
}

var actualFileName = FindFile(fileName);

if (!string.IsNullOrEmpty(actualFileName)) ...

• @HimBromBeere see also the edit about the encapsulation. This might be useful when you are going to add more search paths later ;-] – t3chb0t Jul 4 '16 at 7:45
• Misses this-keyword to be an extension-method. However the method is cool and enables different patterns. – HimBromBeere Jul 4 '16 at 8:56
• @HimBromBeere what do you mean by misses this keyword – Siobhan Jul 4 '16 at 9:03
• @TolaniJaiye-Tikolo Something so that the method is used like fileName.FileExists() (an extension-method). At least the comment //or as an extension makes me think that is what t3chb0t wanted to indicate. – HimBromBeere Jul 4 '16 at 9:05
• @HimBromBeere this is exacly what I meant with the comment ;-) – t3chb0t Jul 4 '16 at 9:17

try to use AbbCode Framework

bool _FileStatus = ac.CheckExistingFile("~/DirectoryPath/FileName.xml");