コード例 #1
0
ファイル: Statistics.cs プロジェクト: alexyakunin/FolderStats
        public async Task <FileStatistics> ProcessDir(string path, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var info  = new DirectoryInfo(path);
            var dirs  = info.GetDirectories();
            var files = info.GetFiles();

            if (IncludeExtensions.Count > 0)
            {
                files = files
                        .Where(p => IncludeExtensions.Contains(Path.GetExtension(p.Name)))
                        .ToArray();
            }

            var dirTasks = dirs.Select(p => Task.Run(
                                           () => ProcessDir(Path.Combine(path, p.Name), cancellationToken),
                                           cancellationToken));
            var fileTasks = files.Select(p => Task.Run(
                                             () => ProcessFile(Path.Combine(path, p.Name), cancellationToken),
                                             cancellationToken));
            var allTasks = dirTasks.Concat(fileTasks).ToArray();
            await Task.WhenAll(allTasks).ConfigureAwait(false);

            var statistics = new FileStatistics()
            {
                Count = 0,
                Size  = 0,
                Lines = files.Length
            };

            statistics = allTasks.Aggregate(statistics, (acc, task) => acc.CombineWith(task.Result));

            AddFileStatistics(path, true, statistics);
            return(statistics);
        }
コード例 #2
0
 string[] FilterFiles(string[] files)
 {
     return(files.Where(p =>
     {
         var extension = GetFullFileExtension(Path.GetFileName(p));
         return IncludeExtensions.Contains(extension) && !ExcludeExtensions.Contains(extension);
     }).ToArray());
 }