예제 #1
0
        /// <summary>
        /// DirectoryUtilities.GetFiles is basicaly the same as Directory.GetFiles
        /// however it returns IEnumerator, which means that it lazy.  This is very important
        /// for large directory trees.  A searchPattern can be specified (Windows wildcard conventions)
        /// that can be used to filter the set of archiveFile names returned.
        ///
        /// Suggested Usage
        ///
        ///     foreach(string fileName in DirectoryUtilities.GetFiles("c:\", "*.txt")){
        ///         Console.WriteLine(fileName);
        ///     }
        ///
        /// </summary>
        /// <param variable="directoryPath">The base directory to enumerate</param>
        /// <param variable="searchPattern">A pattern to filter the names (windows filename wildcards * ?)</param>
        /// <param variable="searchOptions">Indicate if the search is recursive or not.  </param>
        /// <returns>The enumerator for all archiveFile names in the directory (recursively). </returns>
        public static IEnumerable <string> GetFiles(string directoryPath, string searchPattern, SearchOption searchOptions)
        {
            string[] fileNames = Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
            Array.Sort <string>(fileNames, StringComparer.OrdinalIgnoreCase);
            foreach (string fileName in fileNames)
            {
                yield return(fileName);
            }

            if (searchOptions == SearchOption.AllDirectories)
            {
                string[] subDirNames = Directory.GetDirectories(directoryPath);
                Array.Sort <string>(subDirNames);
                foreach (string subDir in subDirNames)
                {
                    foreach (string fileName in DirectoryUtilities.GetFiles(subDir, searchPattern, searchOptions))
                    {
                        yield return(fileName);
                    }
                }
            }
        }