List Directory by extension - CSharp File IO

CSharp examples for File IO:Directory

Description

List Directory by extension

Demo Code


using System.Text.RegularExpressions;
using System.IO;//from  w w  w . j  a  va 2 s .c o  m
using System.Collections.Generic;
using System;

public class Main{
        public static void ListDirectory(DirectoryInfo directoryInfo, string extension, List<string> list)
        {
            Regex regFilePatten = new Regex("[a-f0-9]{40}" + extension);

            foreach (FileInfo fileInfo in directoryInfo.GetFiles(extension.PadLeft(40, '?')))
            {
                if (string.Equals(fileInfo.Name.Substring(0, 1), directoryInfo.Name, StringComparison.OrdinalIgnoreCase)
                    && regFilePatten.IsMatch(fileInfo.Name))
                {
                    string id = fileInfo.Name.Substring(0, 40);
                    list.Add(id);
                }
            }
        }
}

Related Tutorials