Exemple #1
0
        private void Scan()
        {
            var name = Thread.CurrentThread.Name;

            Dispatcher.Invoke(() =>
            {
                StatusLB.Content = "";
                InfoLB1.Content  = "";
                InfoLB2.Content  = "";
            });

            DirectoryInfo Dir;

            #region Проверки
            try
            {
                Dir = new DirectoryInfo(Path);
            }
            catch
            {
                ShowError();
                return;
            }
            if (!Dir.Exists)
            {
                ShowError();
                return;
            }
            try
            {
                var Sz = Convert.ToDouble(SizeStr.Replace('.', ','));
                for (int i = 0; i < SizeType; i++)
                {
                    Sz *= 1024;
                }
                Size = Convert.ToInt64(Sz);
            }
            catch
            {
                ShowError();
                return;
            }
            #endregion

            Dispatcher.Invoke(() =>
            {
                Items.Clear();
                StatusLB.Content = "Сканирование каталогов";
            });

            var aaa = DateTime.Now;

            AllFiles = Dir.GetFiles().ToList();
            ScanDirectories(Dir);

            var gfl = AllFiles.AsParallel().GroupBy(f => f.Name).Where(g => g.Count() > 1).OrderBy(g => g.Key).ToList();

            Dispatcher.Invoke(() =>
            {
                Items.Clear();
                StatusLB.Content = "Составление списка";
                InfoLB1.Content  = "";
                InfoLB2.Content  = "";
            });

            for (int i = 0; i < gfl.Count; i++)
            {
                Dispatcher.Invoke(() =>
                {
                    Items.Add(new ListBoxItem {
                        Content = ""
                    });
                    Items.Add(new ListBoxItem {
                        Content = $"-- {gfl[i].Key} --"
                    });
                });

                foreach (var f in gfl[i].OrderBy(f => f.LastWriteTime))
                {
                    double L = f.Length;
                    string T = "b";

                    if (L > 1099511627776)
                    {
                        L = Math.Round(L / 1099511627776, 2); T = "Tb";
                    }
                    if (L > 1073741824)
                    {
                        L = Math.Round(L / 1073741824, 2); T = "Gb";
                    }
                    if (L > 1048576)
                    {
                        L = Math.Round(L / 1048576, 2); T = "Mb";
                    }
                    if (L > 1024)
                    {
                        L = Math.Round(L / 1024, 2); T = "Kb";
                    }

                    Dispatcher.Invoke(() =>
                    {
                        var LBI = new ListBoxItem
                        {
                            Tag     = f,
                            Content = $"{f.LastWriteTime:yy.MM.dd HH:mm} | {L} {T}  ->  {f.FullName.Replace(f.Name, "")}"
                        };
                        LBI.MouseDoubleClick += LBI_MouseDoubleClick;
                        LBI.KeyDown          += LBI_KeyDown;
                        Items.Add(LBI);
                    });
                }
            }

            if (Items.Count > 1)
            {
                Dispatcher.Invoke(() => Items.RemoveAt(0));
            }


            var bbb = DateTime.Now;

            var ab = bbb - aaa;

            Dispatcher.Invoke(() =>
            {
                StatusLB.Content = "Sucessful!!!";
                InfoLB1.Content  = $"Found {gfl.Count()} groups of duplicates";
                InfoLB2.Content  = ab.ToString();
            });
        }