コード例 #1
0
 public Category(Children file, string name)
 {
     ListOfFiles = new ObservableCollection<Children>();
     ListOfFiles.Add(file);
     // Assign totalSize
     TotalSize += file.Size;
     Name = name;
 }
コード例 #2
0
 public void AddChildren(Children f, string name)
 {
     ListOfFiles.Add(f);
     TotalSize += f.Size;
     Name = name;
 }
コード例 #3
0
        private List<Category> SortFiles(List<FileInfo> allFiles, object backgroundWorker)
        {
            List<Category> categories = new List<Category>();
            // Anonymous type creates a list of all titles split into substrings
            var splitTitles = _titles.Select(t => new { titleName = t, Parts = SplitByRemovables(t) }).ToList();
            int fileCount = 0;

            foreach (FileInfo file in allFiles)
            {
                fileCount++;
                string[] fileParts = SplitByRemovables(Path.GetFileNameWithoutExtension(file.Name));
                string[] directoryParts = SplitByRemovables(file.Directory.Name);

                // Anonymous type finds the best match for each file
                // Finds the score for each title, orders it by descending based on the score
                // and gets assigned the first title in the ordered list from splitTitles
                var topTitle = splitTitles.Select(t => new { titleObj = t, Score = ScoreTitles(t.Parts, fileParts, directoryParts) })
                    .OrderByDescending(x => x.Score)
                    .First();

                Children child = new Children(file);

                // A score of 0 would indicate no matches
                if(topTitle.Score > 0)
                {
                    // Searches for a Category with the same name as the topTitle
                    // returns null if none is found
                    Category category = categories.FirstOrDefault(c => c.Name == topTitle.titleObj.titleName);

                    // Child assignment as per the returned value from above
                    if (category == null)
                    {
                        // Create a new category and add to the list
                        category = new Category(child, topTitle.titleObj.titleName);
                        categories.Add(category);
                    }
                    else
                    {
                        // Add to the existing category
                        category.AddChildren(child, topTitle.titleObj.titleName);
                    }
                }
                else
                {
                    // Files without a score were not matched with any existing category
                    _notSortedFiles.Add(child);
                }

                // Update Progress
                // Send percentComplete to the backgroundWorker and the current file number
                int progressPercentage = 100 * fileCount / allFiles.Count;
                // Only the ReportProgress method can update the UI
                ((BackgroundWorker)backgroundWorker).ReportProgress(progressPercentage, fileCount);
            }
            return categories;
        }