retrieve the file names within a folder using c#

In C#, you can use the Directory.GetFiles() method to retrieve the file names in a folder. This method takes the folder path as a parameter and returns a string array containing all the file names in the folder.

Here is an example code:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\Path\To\Folder";

        // 获取文件夹下的所有文件名
        string[] fileNames = Directory.GetFiles(folderPath);

        // 打印所有文件名
        foreach (string fileName in fileNames)
        {
            Console.WriteLine(fileName);
        }
    }
}

Keep in mind that the Directory.GetFiles() method also accepts an optional search pattern parameter for filtering specific types of files. For example, you can use *.txt to retrieve all text files in a folder.

string[] fileNames = Directory.GetFiles(folderPath, "*.txt");
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds