Пример #1
0
        public static List <string> GetFiles(FileDirSearchOpt fso)
        {
            if (!Directory.Exists(fso.QueryDirectory))
            {
                return(null);
            }

            Stack <string> directoryStack = new Stack <string>();

            directoryStack.Push(fso.QueryDirectory);

            List <string> fileList       = new List <string>();
            SearchOpt     searchOpt      = fso.SearchOptions;
            Regex         regexInFilter  = new Regex(searchOpt.IncludeFilter);
            Regex         regexOutFilter = string.IsNullOrEmpty(searchOpt.ExcludeFilter) ? null : new Regex(searchOpt.ExcludeFilter);

            while (directoryStack.Count > 0)
            {
                string currentDir = directoryStack.Pop();
                try
                {
                    string[] fileListTmp = Directory.GetFiles(currentDir, "*.*");
                    foreach (string filePath in fileListTmp)
                    {
                        string fileName = Path.GetFileName(filePath);
                        if (regexInFilter.Match(fileName).Success)
                        {
                            if (regexOutFilter == null || !regexOutFilter.IsMatch(fileName))
                            {
                                fileList.Add(filePath);
                            }
                        }
                    }

                    if (!fso.Recursive)
                    {
                        break;
                    }

                    // depth-first: list sub directories
                    foreach (string subDirectory in Directory.GetDirectories(currentDir))
                    {
                        directoryStack.Push(subDirectory);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return(fileList);
        }
Пример #2
0
        private void LoadControllers()
        {
            SearchOpt searchOpt = _replaceOpt.SearchOption;

            if (searchOpt.RegexSettings != null)
            {
                _regexIncludeFilter = new Regex(searchOpt.IncludeFilter, (RegexOptions)searchOpt.RegexSettings);

                if (!string.IsNullOrEmpty(searchOpt.ExcludeFilter))
                {
                    _regexExcludeFilter = new Regex(searchOpt.ExcludeFilter, (RegexOptions)searchOpt.RegexSettings);
                }
            }
        }
Пример #3
0
        private bool IsMatch(string rawText)
        {
            // null text skipped
            if (rawText == null || _replaceOpt.ActionType == ReplaceAction.Unspecified)
            {
                return(false);
            }

            if (_regexIncludeFilter != null)
            {
                return(_regexIncludeFilter.IsMatch(rawText) &&
                       (_regexExcludeFilter == null || !_regexExcludeFilter.IsMatch(rawText)));
            }
            else
            {
                SearchOpt searchOpt = _replaceOpt.SearchOption;
                return(rawText.IndexOf(searchOpt.IncludeFilter) != -1 &&
                       (string.IsNullOrEmpty(searchOpt.ExcludeFilter) || rawText.IndexOf(searchOpt.ExcludeFilter) == -1));
            }
        }