Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Created March 24, 2015 14:25
Show Gist options
  • Save SteGriff/e182d6177838d7a6c8cc to your computer and use it in GitHub Desktop.
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
/// <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