Find a file with a certain extension in folder

C#Filepath

C# Problem Overview


Given a folder path (like C:\Random Folder), how can I find a file in it that holds a certain extension, like txt? I assume I'll have to do a search for *.txt in the directory, but I'm not sure how I'm supposed to start this search in the first place.

C# Solutions


Solution 1 - C#

Look at the System.IO.Directory class and the static method GetFiles. It has an overload that accepts a path and a search pattern. Example:

 string[] files = System.IO.Directory.GetFiles(path, "*.txt");

Solution 2 - C#

You could use the Directory class

 Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories)

Solution 3 - C#

It's quite easy, actually. You can use the System.IO.Directory class in conjunction with System.IO.Path. Something like (using LINQ makes it even easier):

var allFilenames = Directory.EnumerateFiles(path).Select(p => Path.GetFileName(p));

// Get all filenames that have a .txt extension, excluding the extension
var candidates = allFilenames.Where(fn => Path.GetExtension(fn) == ".txt")
                             .Select(fn => Path.GetFileNameWithoutExtension(fn));

There are many variations on this technique too, of course. Some of the other answers are simpler if your filter is simpler. This one has the advantage of the delayed enumeration (if that matters) and more flexible filtering at the expense of more code.

Solution 4 - C#

The method below returns only the files with certain extension (eg: file with .txt but not .txt1)

public static IEnumerable<string> GetFilesByExtension(string directoryPath, string extension, SearchOption searchOption)
    {
        return
            Directory.EnumerateFiles(directoryPath, "*" + extension, searchOption)
                .Where(x => string.Equals(Path.GetExtension(x), extension, StringComparison.InvariantCultureIgnoreCase));
    }

Solution 5 - C#

As per my understanding, this can be done in two ways :

  1. You can use Directory Class with Getfiles method and traverse across all files to check our required extension. >Directory.GetFiles("your_folder_path)[i].Contains("*.txt")

  2. You can use Path Class with GetExtension Method which takes file path as a parameter and verifies the extension.To get the file path, just have a looping condition that will fetch a single file and return the filepath that can be used for verification. >Path.GetExtension(your_file_path).Equals(".json")

Note : Both the logic has to be inside a looping condition.

Solution 6 - C#

Use this code for read file with all type of extension file.

string[] sDirectoryInfo = Directory.GetFiles(SourcePath, "*.*");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDominic KView Question on Stackoverflow
Solution 1 - C#Anthony PegramView Answer on Stackoverflow
Solution 2 - C#sgriffinusaView Answer on Stackoverflow
Solution 3 - C#Greg DView Answer on Stackoverflow
Solution 4 - C#hazjackView Answer on Stackoverflow
Solution 5 - C#Pulkit ShahView Answer on Stackoverflow
Solution 6 - C#Vishal PrajapatiView Answer on Stackoverflow