Wednesday, February 11, 2009

.NET How to merge FileInfo arrays with Linq

using System;

using System.IO;

using System.Linq;

 

[assembly: CLSCompliant(true)]

namespace SuperFastSearch

{

    internal class FileSearch : IFileSearch

    {

        #region IFileSearch Members

 

        public string _Path { get; set; }

        public string _SearchPattern { get; set; }

        public bool _Recursive { get; set; }

 

        public FileInfo[] Search()

        {

            //List lSearchResults = new List();

            FileInfo[] fiResults = null;

            DirectoryInfo[] diResults = null;

 

            SearchOption so = new SearchOption();

 

            if (_Recursive)

            {

                so = SearchOption.AllDirectories;

            }

            else

            {

                so = SearchOption.TopDirectoryOnly;

            }

 

            //check that the class variables have been set up

            if (string.IsNullOrEmpty(_Path) || string.IsNullOrEmpty(_SearchPattern))

            {

                //do not perform the search as the nessescary params have not been supplied

            }

            else

            {

                //perform the search

                DirectoryInfo di = new DirectoryInfo(_Path);

 

                //check if we are searching the 'root' directory

                if (_Path.Length == 3)

                {

                    //_Path is like "C:\" or 'root'

                    //build a list of the root directories to use later when collating file info

                    diResults = di.GetDirectories(_SearchPattern, SearchOption.TopDirectoryOnly);

                   

                    //create a temp array for use in the loop below

                    FileInfo[] tempFi = null;

                   

                    foreach (DirectoryInfo dirInfo in diResults)

                    {

                        //ignore the system volume information directory as we

                        //will get an UnauthorizsedAccessException if we try

                        if (dirInfo.Name != ("System Volume Information"))

                        {

                            tempFi = dirInfo.GetFiles(_SearchPattern, so);

 

                            //check if we have some files before adding them to the array

                            if (tempFi.Length > 0)

                            {

                                if (fiResults != null)

                                {

                                    fiResults = fiResults.Concat(tempFi).ToArray();

                                }

                                else

                                {

                                    fiResults = tempFi;

                                }

                            }

                        }

                    }

 

                }

                else

                {

                    fiResults = di.GetFiles(_SearchPattern, so);

                }

            }

 

            return fiResults;

        }

 

        #endregion

    }

}

 

2 comments:

Asim said...

Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks.order modalert

Anonymous said...

beautythanks for the shared this interesting information with us. I really enjoyed your article and waiting for new..