Пример #1
0
        /// <summary>
        /// Retrieves files from a file directory.
        /// </summary>
        /// <param name="searchInformation">Search information to query the directory with</param>
        /// <param name="callback">Callback method to be invoked after each provided timeout period elapses</param>
        /// <param name="intCallbackTimeoutSecounds">Number of seconds to wait before invoking the callback method</param>
        /// <returns></returns>
        public FileObjectList GetFilesAsync(SearchInformation searchInformation, SearchInformation.SearchCallback callback, int intCallbackTimeoutSecounds = 10)
        {
            try
            {
                // Validation
                FileObjectList fileObjectList = new FileObjectList();

                // Set Initial Search
                DateTime lastDate = DateTime.Now;

                // Create Directories Searched
                DirectoryObjectList directoriesSearched = new DirectoryObjectList();

                // Get File Asynchronously
                this.GetFilesAsync(searchInformation, callback, ref fileObjectList, ref directoriesSearched, ref lastDate, intCallbackTimeoutSecounds);

                return(fileObjectList);
            }
            catch (Exception ex)
            {
                // To Be Implemented: Throw Custom Exception...
                Console.WriteLine(ex.ToString());
                return(null);
            }
        }
Пример #2
0
        private FileObjectList GetFilesAsync(SearchInformation searchInformation, SearchInformation.SearchCallback callback,
                                             ref FileObjectList fileObjectList, ref DirectoryObjectList directoriesSearched, ref DateTime lastDate, int intCallbackTimeoutSecounds = 10)
        {
            try
            {
                #region Get Files

                // Get Directory Files - Top Level Only
                List <string> listFileNames = System.IO.Directory.GetFiles(this.FilePath, searchInformation.SearchPattern, System.IO.SearchOption.TopDirectoryOnly).ToList();

                // Create File List
                FileObjectList listFiles = new FileObjectList(listFileNames);

                // Validation
                if (listFiles.Count > 0)
                {
                    // Filter Files By Search Information
                    List <FileObject> filesToAdd = searchInformation.GetSearchFilesFiltered(listFiles);

                    // Create File List
                    fileObjectList.AddRange(filesToAdd.ToArray());

                    // Distinct List
                    fileObjectList = new FileObjectList(fileObjectList.Distinct().ToList());
                }

                // Add Directory To Searched List
                directoriesSearched.Add(this);

                #endregion

                #region Recurse Sub Directories

                // Loop SubDirectories
                foreach (DirectoryObject directory in this.SubDirectories)
                {
                    // Get Sub Directory Files
                    directory.GetFilesAsync(searchInformation, callback, ref fileObjectList, ref directoriesSearched, ref lastDate, intCallbackTimeoutSecounds);
                }

                #endregion

                #region Invoke Callback

                // Check Time For Callback
                if (fileObjectList != null && DateTime.Now > lastDate.AddSeconds(intCallbackTimeoutSecounds))
                {
                    // Invoke Callback
                    callback.Invoke(fileObjectList, directoriesSearched);
                    lastDate = DateTime.Now;
                }

                #endregion

                return(fileObjectList);
            }
            catch (Exception ex)
            {
                // To Be Implemented: Throw Custom Exception...
                Console.WriteLine(ex.ToString());
                return(null);
            }
        }