遍历某个硬盘目录下的所有某种扩展名的文件

public class FileAccessor
{
    private DirectoryInfo Di;
    private StringBuilder FileStringBuilder;

    public FileAccessor(){}

    /// <summary>
    /// 根据目录名称和扩展名(如.cs)查询该目录(含子目录)下的所有该扩展名的文件
    /// </summary>
    /// <param name="FolderPath">目录的路径</param>
    /// <param name="ExtensionName">扩展名</param>
    /// <returns>所有该扩展名的文件字符串</returns>
    public string GetFiles(string FolderPath, string ExtensionName)
    {
        Di = new DirectoryInfo(FolderPath);
        FileStringBuilder = new StringBuilder();

        //遍历目录下所有文件
         FileInfo[] Files = Di.GetFiles();
        foreach (FileInfo File in Files)
        {
            if (File.Extension == ExtensionName)
            {
                FileStringBuilder.Append(File.FullName);
                FileStringBuilder.Append("\r\n");
            }
        }

        //遍历目录下所有子目录,并递归调用本方法
         DirectoryInfo[] SubDirectories = Di.GetDirectories();
        foreach (DirectoryInfo SubDi in SubDirectories)
        {
            GetFiles(SubDi);
        }
        
        return FileStringBuilder.ToString();
    }
}

你可能感兴趣的:(C++,c,C#)