4
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ A one-line code review ;-) I like it. \$\endgroup\$ – t3chb0t Jul 4 '16 at 7:25
  • \$\begingroup\$ Out of curiosity, what if the file name existed in the current and parent directory. What do you do in that scenario \$\endgroup\$ – Siobhan Jul 4 '16 at 8:37
  • \$\begingroup\$ @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). \$\endgroup\$ – HimBromBeere Jul 4 '16 at 8:54
  • \$\begingroup\$ 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. \$\endgroup\$ – Siobhan Jul 4 '16 at 8:59
  • \$\begingroup\$ 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 \$\endgroup\$ – MikeT Jul 4 '16 at 11:58
6
\$\begingroup\$

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)) ...
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @HimBromBeere see also the edit about the encapsulation. This might be useful when you are going to add more search paths later ;-] \$\endgroup\$ – t3chb0t Jul 4 '16 at 7:45
  • \$\begingroup\$ Misses this-keyword to be an extension-method. However the method is cool and enables different patterns. \$\endgroup\$ – HimBromBeere Jul 4 '16 at 8:56
  • \$\begingroup\$ @HimBromBeere what do you mean by misses this keyword \$\endgroup\$ – Siobhan Jul 4 '16 at 9:03
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – HimBromBeere Jul 4 '16 at 9:05
  • \$\begingroup\$ @HimBromBeere this is exacly what I meant with the comment ;-) \$\endgroup\$ – t3chb0t Jul 4 '16 at 9:17
1
\$\begingroup\$

try to use AbbCode Framework

bool _FileStatus = ac.CheckExistingFile("~/DirectoryPath/FileName.xml");
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.