//SearchEngineEntities db = new SearchEngineEntities();
        /// <summary>
        /// Returns all nested directories including the given one. O(n = all the nested directories)
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public List <string> getNestedDirectories(string directory, List <string> directories)
        {
            //if directory not exist or null return the existing list;

            if (!Directory.Exists(directory.ToLower()) || String.IsNullOrEmpty(directory))
            {
                return(directories);
            }

            directory = directory.ToLower();
            directories.Add(directory); // add current directory

            try {
                string[] tempDirectories = Directory.GetDirectories(directory);
                //parallel operation
                foreach (var dir in tempDirectories)
                {
                    //for each directory

                    directories = getNestedDirectories(dir, directories);
                }
            } catch (Exception ex) {
                ExceptionHandle.Handle(ex);
            }
            return(directories);
        }
Example #2
0
 public string GetMD5FromFile(string FileName)
 {
     using (var md5 = MD5.Create()) {
         try {
             using (var stream = File.OpenRead(FileName)) {
                 return(BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower());
             }
         } catch (Exception ex) {
             ExceptionHandle.Handle(ex);
         }
     }
     return(null);
 }
 /// <summary>
 /// Read file context based on conditions and division method of 4000
 /// </summary>
 /// <param name="path"></param>
 /// <param name="fileSizeInBytes"></param>
 /// <param name="FileContext"></param>
 public string ReadFile(string path, long fileSizeInBytes)
 {
     if (File.Exists(path))
     {
         if (fileSizeInBytes <= FileSizeTypeStructure.MB_1 * 3)
         {
             try {
                 return(File.ReadAllText(path));
             } catch (Exception) {
                 return("");
             }
         }
     }
     else
     {
         ExceptionHandle.Handle(new Exception("File does not exist at the location : " + path));
     }
     return("");
 }
        /// <summary>
        /// Returns all nested directories including the given one with searched results. O(n = all the nested directories)
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public List <string> getNestedDirectories2(
            string directory,
            ref long FileNumberID,
            ref float percentIncrease,
            DateTime startDate,
            DateTime endDate,
            int dateType       = -1,
            bool getContents   = false,
            int FileSizeType   = -1,
            long fileSizeStart = -1,
            long fileSizeEnd   = -1,
            bool lookForDate   = false)
        {
            //if directory not exist or null return the existing list;

            if (!Directory.Exists(directory.ToLower()) || String.IsNullOrEmpty(directory))
            {
                return(directories);
            }

            if (FileSizeType == FileSizeTypeStructure.KB)
            {
                fileSizeStart = fileSizeStart * (int)FileSizeTypeStructure.KB_1;
                fileSizeEnd   = fileSizeEnd * (int)FileSizeTypeStructure.KB_1;
            }
            else if (FileSizeType == FileSizeTypeStructure.MB)
            {
                fileSizeStart = fileSizeStart * (int)FileSizeTypeStructure.MB_1;
                fileSizeEnd   = fileSizeEnd * (int)FileSizeTypeStructure.MB_1;
            }
            else if (FileSizeType == FileSizeTypeStructure.GB)
            {
                fileSizeStart = fileSizeStart * (int)FileSizeTypeStructure.GB_1;
                fileSizeEnd   = fileSizeEnd * (int)FileSizeTypeStructure.GB_1;
            }
            else
            {
                fileSizeStart = -1;
                fileSizeEnd   = -1;
            }


            directory = directory.ToLower();
            directories.Add(directory); // add current directory
            if (FilesList.FolderAlreadyRead == null)
            {
                FilesList.FolderAlreadyRead = new List <string>();
            }

            FilesList.FolderAlreadyRead.Add(directory);

            new Thread(() => {
                if (IsMatchInText(directory, FilesList))
                {
                    Flash.addListItem("(Directory) : " + directory, directory);
                }
            }).Start();


            #region Files Read in a directory

            var files = Directory.GetFiles(directory).ToArray();

            long count = files.Count();

            bool  doNotContinue           = false;
            float progressPercent2        = 0;
            float directoryLoadPercentage = 0;
            if (count == 0)
            {
                count         = 1;
                doNotContinue = true;
            }
            if (directoryLoadPercentage == 0)
            {
                progressPercent2 = count / 100F;
            }
            else
            {
                progressPercent2 = directoryLoadPercentage / count;
            }

            float percentIncrease2 = percentIncrease;
            long  FileID           = FileNumberID;

            if (!doNotContinue)
            {
                //files.AsEnu(file => {
                foreach (var file in files)
                {
                    //new Thread(() => {
                    FileInfo      fileinfo = null;
                    FileStructure filestr  = null;

                    fileinfo = new FileInfo(file);
                    filestr  = new FileStructure()
                    {
                        ID             = ++FileID,
                        ExactLocation  = fileinfo.FullName,
                        FileName       = fileinfo.Name,
                        Folder         = directory,
                        Extension      = fileinfo.Extension,
                        CreatedDate    = fileinfo.CreationTime,
                        ModifiedDate   = fileinfo.LastWriteTime,
                        LastAccessDate = fileinfo.LastAccessTime,
                        SizeBytes      = fileinfo.Length
                    };

                    if (filestr != null)
                    {
                        FilesList.Add(filestr);
                    }
                    if (filestr.FileName == "Technology_2_2.txt")
                    {
                        var found = true;
                    }

                    //new Thread(() => {

                    if (!String.IsNullOrWhiteSpace(fileinfo.Extension) && filestr.Extension.IndexOf('.') > -1)
                    {
                        filestr.Extension = filestr.Extension.Remove(0, 1);
                    }

                    var isExtensionExist = false;
                    if (InludingExtensionList != null)
                    {
                        isExtensionExist = this.InludingExtensionList.Any(n => n == filestr.Extension);
                    }
                    var isDisableExtensionExist = false;
                    if (ExcludingExtensionList != null)
                    {
                        isDisableExtensionExist = this.ExcludingExtensionList.Any(n => n == filestr.Extension);
                    }
                    bool addFile          = false;
                    bool isExtensionRight = false;
                    bool isSizeRight      = false;
                    bool isDateRight      = false;
                    if (isExtensionExist && !isDisableExtensionExist)
                    {
                        isExtensionRight = true;
                    }


                    if (FileSizeType != FileSizeTypeStructure.NoLimit)
                    {
                        if (filestr.SizeBytes >= fileSizeStart && filestr.SizeBytes <= fileSizeEnd)
                        {
                            isSizeRight = true; //only if size meets the condition then add the file.
                        }
                    }
                    else
                    {
                        isSizeRight = true; //no size limit
                    }

                    if (lookForDate)
                    {
                        if (dateType == DateTypeStructure.Modified_Date)
                        {
                            DateTime dt = filestr.ModifiedDate.Date;
                            if (dt != null && (dt >= startDate.Date && dt <= endDate.Date))
                            {
                                isDateRight = true;
                            }
                        }
                        else if (dateType == DateTypeStructure.Created_Date)
                        {
                            DateTime dt = filestr.CreatedDate.Date;
                            if (dt != null && (dt >= startDate.Date && dt <= endDate.Date))
                            {
                                isDateRight = true;
                            }
                        }
                        else if (dateType == DateTypeStructure.Access_Date)
                        {
                            DateTime dt = filestr.LastAccessDate.Date;
                            if (dt != null && (dt >= startDate.Date && dt <= endDate.Date))
                            {
                                isDateRight = true;
                            }
                        }
                    }
                    else
                    {
                        isDateRight = true;
                    }

                    if (isSizeRight && isDateRight && isExtensionRight)
                    {
                        addFile = true;
                    }

                    if (addFile)
                    {
                        filestr.Content = ReadFile(file, filestr.SizeBytes);
                        if (filestr.IsContentEmpty)
                        {
                        }
                    }



                    if (addFile)
                    {
                        //if found by the size
                        new Thread(() => {
                            if (IsMatchInText(filestr.FileName, FilesList))
                            {
                                Flash.addListItem(filestr.FileName, file);
                            }
                            else if (getContents && !filestr.IsContentEmpty && IsMatchInText(filestr.Content, FilesList))
                            {
                                Flash.addListItem(filestr.FileName, file);
                            }
                            else if (getContents && filestr.SizeBytes <= FileSizeTypeStructure.MB_1 * 3)
                            {
                                filestr.Content = this.ReadFile(filestr.ExactLocation, filestr.SizeBytes);
                                if (filestr.IsContentEmpty)
                                {
                                }
                                FilesList.IsSavingNecessary = true;
                            }
                        }).Start();
                    }
                    //}).Start();
                    //percentIncrease2 += progressPercent2;

                    //Flash.setProgress(percentIncrease2);
                    //}).Start();
                } //files
            }
            FileNumberID    = FileID;
            percentIncrease = percentIncrease2;
            //need to learn how to send type of type as list to remove the null
            if (directoryLoadPercentage == 0)
            {
                Flash.setProgress(0);
            }
            #endregion

            try {
                string[] tempDirectories = Directory.GetDirectories(directory);
                //parallel operation
                foreach (var dir in tempDirectories)
                {
                    directories = getNestedDirectories2(
                        dir,
                        ref FileNumberID,
                        ref percentIncrease,
                        startDate,
                        endDate,
                        dateType,
                        getContents,
                        FileSizeType,
                        fileSizeStart,
                        fileSizeEnd,
                        lookForDate);
                }
            } catch (Exception ex) {
                ExceptionHandle.Handle(ex);
            }
            return(directories);
        }