Created
March 24, 2015 14:25
-
-
Save SteGriff/e182d6177838d7a6c8cc to your computer and use it in GitHub Desktop.
Given a file path which is missing the file extension, search the directory for a matching file and return its full path, including extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Given a path to a file which is missing the file extension, search the directory | |
/// for a matching file and return its full path (with extension) | |
/// </summary> | |
/// <param name="localPath">Path to a file without file extension</param> | |
/// <returns>The path with the file extension</returns> | |
private static string MatchingFileWithUnknownExtension(string localPath) | |
{ | |
string directory = Path.GetDirectoryName(localPath); | |
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(localPath); | |
string matchingFile = Directory.EnumerateFiles(directory) | |
.Where(f => Path.GetFileNameWithoutExtension(f) == fileNameWithoutExtension) | |
.FirstOrDefault(); | |
return matchingFile; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment