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.
-
4\$\begingroup\$ A one-line code review ;-) I like it. \$\endgroup\$t3chb0t– t3chb0t2016年07月04日 07:25:21 +00:00Commented Jul 4, 2016 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\$Tolani– Tolani2016年07月04日 08:37:37 +00:00Commented Jul 4, 2016 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\$MakePeaceGreatAgain– MakePeaceGreatAgain2016年07月04日 08:54:42 +00:00Commented Jul 4, 2016 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\$Tolani– Tolani2016年07月04日 08:59:07 +00:00Commented Jul 4, 2016 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– MikeT2016年07月04日 11:58:00 +00:00Commented Jul 4, 2016 at 11:58
2 Answers 2
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)) ...
-
\$\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– t3chb0t2016年07月04日 07:45:47 +00:00Commented Jul 4, 2016 at 7:45
-
\$\begingroup\$ Misses
this
-keyword to be an extension-method. However the method is cool and enables different patterns. \$\endgroup\$MakePeaceGreatAgain– MakePeaceGreatAgain2016年07月04日 08:56:17 +00:00Commented Jul 4, 2016 at 8:56 -
\$\begingroup\$ @HimBromBeere what do you mean by misses this keyword \$\endgroup\$Tolani– Tolani2016年07月04日 09:03:00 +00:00Commented Jul 4, 2016 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\$MakePeaceGreatAgain– MakePeaceGreatAgain2016年07月04日 09:05:17 +00:00Commented Jul 4, 2016 at 9:05 -
\$\begingroup\$ @HimBromBeere this is exacly what I meant with the comment ;-) \$\endgroup\$t3chb0t– t3chb0t2016年07月04日 09:17:34 +00:00Commented Jul 4, 2016 at 9:17
try to use AbbCode Framework
bool _FileStatus = ac.CheckExistingFile("~/DirectoryPath/FileName.xml");