Non Recursive Get Files - CSharp File IO

CSharp examples for File IO:Directory

Description

Non Recursive Get Files

Demo Code


using System.Collections.Generic;
using System.Collections;
using System.IO;//  w  w w  . ja  va2 s . co  m
using System;

public class Main{
        private static IList<FileInfo> NonRecursiveGetFiles(ParsedPath rootPath)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(rootPath.VolumeAndDirectory);
        
            return dirInfo.GetFiles(rootPath.FileAndExtension);
        }
        /// <summary>
        /// Returns a list of files given a file search pattern.  Will also search sub-directories.
        /// </summary>
        /// <param name="searchPattern">Search pattern.  Can include a full or partial path and standard wildcards for the file name.</param>
        /// <param name="scope">The scope of the search.</param>
        /// <param name="baseDir">Base directory to use for partially qualified paths</param>
        /// <returns>An array of <c>FileInfo</c> objects for files matching the search pattern. </returns>
        public static IList<FileInfo> GetFiles(ParsedPath fileSpec, SearchScope scope)
        {
            ParsedPath rootPath = fileSpec.MakeFullPath();
        
            if (scope != SearchScope.DirectoryOnly)
            {           
                List<FileInfo> files = new List<FileInfo>();    
            
                if (scope == SearchScope.RecurseParentDirectories)
                    RecursiveGetParentFiles(rootPath, ref files);
                else
                    RecursiveGetSubFiles(rootPath, (scope == SearchScope.RecurseSubDirectoriesBreadthFirst), ref files);
                
                return files.ToArray();
            }
            else
            {
                return NonRecursiveGetFiles(rootPath);
            }
        }
}

Related Tutorials