Пример #1
0
        public static Info search(String path, String extension, string predicate)
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            ParallelOptions options = new ParallelOptions();
            options.CancellationToken = cancellationTokenSource.Token;

            Info info = new Info();
            string[] files = Directory.GetFiles(path, extension, SearchOption.AllDirectories);
            info.totalextensions = files.Length;
            ParallelLoopResult loopResult = Parallel.For(
                0,
                files.Length,
                options,
                (i, loopState) =>
                    {
                        if (File.ReadAllText(files[i]).Contains(predicate))
                        {
                            info.names.Add(files[i]);
                        }
                    }
                );
            if (loopResult.IsCompleted)
            {
                info.total = info.names.Count;
                
            }
            return info;
        }
Пример #2
0
 //extension passed as *.txt
 public static async Task<Info> searchAsync(String path, String extension, string predicate)
 {
     Info info = new Info();
     string [] files = Directory.GetFiles(path, extension, SearchOption.AllDirectories);
     
     foreach (var file in files)
     {
         var data = await searchTextInFile(file , predicate);
         if(data) info.names.Add(file);
     }
     info.totalextensions = files.Length;
     info.total = info.names.Count;
     return info;
 }