Find a file with a certain extension in folder

后端 未结 6 1970
清歌不尽
清歌不尽 2020-11-27 15:10

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 sea

6条回答
  •  囚心锁ツ
    2020-11-27 15:49

    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.

提交回复
热议问题