Example #1
0
        /// <summary>
        /// Returns a list of all files found in the given folder.
        /// The search is recursive.
        /// </summary>
        /// <param name="rootpath">The folder to look in</param>
        /// <param name="callback">The function to call with the filenames</param>
        /// <param name="folderList">A function to call that lists all folders in the supplied folder</param>
        /// <param name="fileList">A function to call that lists all files in the supplied folder</param>
        /// <param name="attributeReader">A function to call that obtains the attributes for an element, set to null to avoid reading attributes</param>
        /// <returns>A list of the full filenames</returns>
        public static IEnumerable<string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList, ExtractFileAttributes attributeReader)
        {
            Stack<string> lst = new Stack<string>();

            var isFolder = false;
            try
            {
                if (attributeReader == null)
                    isFolder = true;
                else
                    isFolder = (attributeReader(rootpath) & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory;
            }
            catch
            {
            }

            if (isFolder)
            {
                rootpath = AppendDirSeparator(rootpath);
                try
                {

                    System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Directory : attributeReader(rootpath);
                    if (callback(rootpath, rootpath, attr))
                        lst.Push(rootpath);
                }
                catch (System.Threading.ThreadAbortException)
                {
                    throw;
                }
                catch (Exception)
                {
                    callback(rootpath, rootpath, ATTRIBUTE_ERROR | System.IO.FileAttributes.Directory);
                }

                while (lst.Count > 0)
                {
                    string f = AppendDirSeparator(lst.Pop());

                    yield return f;

                    try
                    {
                        foreach(string s in folderList(f))
                        {
                            var sf = AppendDirSeparator(s);
                            System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Directory : attributeReader(sf);
                            if (callback(rootpath, sf, attr))
                                lst.Push(sf);
                        }
                    }
                    catch (System.Threading.ThreadAbortException)
                    {
                        throw;
                    }
                    catch (Exception)
                    {
                        callback(rootpath, f, ATTRIBUTE_ERROR | System.IO.FileAttributes.Directory);
                    }

                    string[] files = null;
                    if (fileList != null)
                        try
                        {
                            files = fileList(f);
                        }
                        catch (System.Threading.ThreadAbortException)
                        {
                            throw;
                        }
                        catch (Exception)
                        {
                            callback(rootpath, f, ATTRIBUTE_ERROR);
                        }

                    if (files != null)
                        foreach(var s in files)
                        {
                            try
                            {
                                System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Normal : attributeReader(s);
                                if (!callback(rootpath, s, attr))
                                    continue;
                            }
                            catch (System.Threading.ThreadAbortException)
                            {
                                throw;
                            }
                            catch (Exception)
                            {
                                callback(rootpath, s, ATTRIBUTE_ERROR);
                                continue;
                            }
                            yield return s;
                        }
                }
            }
            else
            {
                try
                {
                    System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Normal : attributeReader(rootpath);
                    if (!callback(rootpath, rootpath, attr))
                        yield break;
                }
                catch (System.Threading.ThreadAbortException)
                {
                    throw;
                }
                catch (Exception)
                {
                    callback(rootpath, rootpath, ATTRIBUTE_ERROR);
                    yield break;
                }

                yield return rootpath;
            }
        }
Example #2
0
 /// <summary>
 /// Returns a list of all files found in the given folder.
 /// The search is recursive.
 /// </summary>
 /// <param name="rootpath">The folder to look in</param>
 /// <param name="callback">The function to call with the filenames</param>
 /// <param name="folderList">A function to call that lists all folders in the supplied folder</param>
 /// <param name="fileList">A function to call that lists all files in the supplied folder</param>
 /// <returns>A list of the full filenames</returns>
 public static IEnumerable<string> EnumerateFileSystemEntries(string rootpath, EnumerationFilterDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList)
 {
     return EnumerateFileSystemEntries(rootpath, callback, folderList, fileList, null);
 }
Example #3
0
 public BlockedCollector(BlockingQueue<string> queue, string rootpath, FilenameFilter filter, FileSystemInteraction folderList, FileSystemInteraction fileList)
 {
     m_queue = queue;
     System.Threading.Thread t = new System.Threading.Thread(BlockedThreadRunner);
     t.Start(new object[] { rootpath, filter, folderList, fileList});
 }
Example #4
0
 /// <summary>
 /// Returns an IEnumerable with all filesystem entries, recursive
 /// </summary>
 /// <param name="rootpath">The folder to look in</param>
 /// <param name="filter">An optional filter to apply to the filenames</param>
 /// <param name="callback">The function to call with the filenames</param>
 /// <param name="folderList">A function to call that lists all folders in the supplied folder</param>
 /// <param name="fileList">A function to call that lists all files in the supplied folder</param>
 /// <param name="attributeReader">A function to call that obtains the attributes for an element, set to null to skip symlink detection</param>
 /// <returns>The file system entries</returns>
 public static IEnumerable<string> EnumerateFileSystemEntriesBlocked(string rootpath, FilenameFilter filter, FileSystemInteraction folderList, FileSystemInteraction fileList, ExtractFileAttributes attributeReader)
 {
     BlockingQueue<string> queue = new BlockingQueue<string>();
     new BlockedCollector(queue, rootpath, filter, folderList, fileList);
     return new BlockingQueueAsEnumerable<string>(queue);
 }
Example #5
0
        /// <summary>
        /// Returns a list of all files found in the given folder.
        /// The search is recursive.
        /// </summary>
        /// <param name="rootpath">The folder to look in</param>
        /// <param name="callback">The function to call with the filenames</param>
        /// <param name="folderList">A function to call that lists all folders in the supplied folder</param>
        /// <param name="fileList">A function to call that lists all files in the supplied folder</param>
        /// <param name="attributeReader">A function to call that obtains the attributes for an element, set to null to avoid reading attributes</param>
        /// <returns>A list of the full filenames</returns>
        public static void EnumerateFileSystemEntries(string rootpath, EnumerationCallbackDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList, ExtractFileAttributes attributeReader)
        {
            if (!System.IO.Directory.Exists(rootpath))
                return;

            Queue<string> lst = new Queue<string>();
            lst.Enqueue(rootpath);

            while (lst.Count > 0)
            {
                string f = AppendDirSeparator(lst.Dequeue());
                try
                {
                    System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Directory : attributeReader(f);
                    if (!callback(rootpath, f, attr))
                        continue;

                    foreach (string s in folderList(f))
                        lst.Enqueue(s);
                }
                catch (System.Threading.ThreadAbortException)
                {
                    throw;
                }
                catch (Exception)
                {
                    callback(rootpath, f, ATTRIBUTE_ERROR | System.IO.FileAttributes.Directory);
                }

                try
                {
                    if (fileList != null)
                        foreach (string s in fileList(f))
                        {
                            try
                            {
                                System.IO.FileAttributes attr = attributeReader == null ? System.IO.FileAttributes.Normal : attributeReader(s);
                                callback(rootpath, s, attr);
                            }
                            catch (System.Threading.ThreadAbortException)
                            {
                                throw;
                            }
                            catch (Exception)
                            {
                                callback(rootpath, s, ATTRIBUTE_ERROR);
                            }
                        }
                }
                catch (System.Threading.ThreadAbortException)
                {
                    throw;
                }
                catch (Exception)
                {
                    callback(rootpath, f, ATTRIBUTE_ERROR);
                }
            }
        }
Example #6
0
 /// <summary>
 /// Returns a list of all files found in the given folder.
 /// The search is recursive.
 /// </summary>
 /// <param name="rootpath">The folder to look in</param>
 /// <param name="callback">The function to call with the filenames</param>
 /// <param name="folderList">A function to call that lists all folders in the supplied folder</param>
 /// <param name="fileList">A function to call that lists all files in the supplied folder</param>
 /// <returns>A list of the full filenames</returns>
 public static void EnumerateFileSystemEntries(string rootpath, EnumerationCallbackDelegate callback, FileSystemInteraction folderList, FileSystemInteraction fileList)
 {
     EnumerateFileSystemEntries(rootpath, callback, folderList, fileList, null);
 }