Advertisement
Pearlfromsu

s4l5 50%

Apr 14th, 2023
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Forms;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Windows.Threading;
  18. using Label = System.Windows.Controls.Label;
  19.  
  20. namespace s4l5 {
  21.     /// <summary>
  22.     /// Interaction logic for MainWindow.xaml
  23.     /// </summary>
  24.     public partial class MainWindow : Window {
  25.         static int idd = 0;
  26.         private string selectedFolder = "";
  27.         private object queue_lock = new object(), dictionary_lock = new object();
  28.         Dictionary<string, long> dict = new Dictionary<string, long>();
  29.         Queue<string> q = new Queue<string>();
  30.         int finish = -1;
  31.  
  32.         public MainWindow() {
  33.             InitializeComponent();
  34.             DispatcherTimer timer = new DispatcherTimer() {
  35.                 Interval = TimeSpan.FromSeconds(1)
  36.             };
  37.             timer.Tick += UpdateList;
  38.             timer.Start();
  39.         }
  40.  
  41.         string[] sizes = { "Байт", "КБ", "МБ", "ГБ", "ТБ" };
  42.         string getSize(long size) {
  43.             double res = size;
  44.             int i;
  45.             for (i = 0; res / 1024.0 > 1; i++)
  46.                 res /= 1024.0;
  47.             return (Math.Round(res * 100)/100.0).ToString() + sizes[i];
  48.         }
  49.  
  50.         void UpdateList(object sender, EventArgs e) {
  51.             if (finish == -1)
  52.                 return;
  53.  
  54.             listView.Items.Clear();
  55.             foreach (string dir in Directory.GetDirectories(selectedFolder))
  56.                 lock (dictionary_lock)
  57.                     listView.Items.Add(new Label() { Content = $"{dir}: {(dict.ContainsKey(dir) ? getSize(dict[dir]) : "???")}" });
  58.        
  59.         }
  60.  
  61.         void getFilesList(string path) {
  62.             finish = 0;
  63.  
  64.             Dispatcher.Invoke(() => {
  65.                 imgCircle.Visibility = Visibility.Visible;
  66.             });
  67.             Queue<string> queue = new Queue<string>();
  68.            
  69.             queue.Enqueue(path);
  70.            
  71.             while (queue.Count > 0) {
  72.                 string current = queue.Dequeue();
  73.                 foreach (string dir in Directory.GetDirectories(current))
  74.                     queue.Enqueue(dir);
  75.  
  76.                 lock (queue_lock)
  77.                     foreach (string fl in Directory.GetFiles(current))
  78.                         q.Enqueue(fl);
  79.             }
  80.  
  81.             Dispatcher.Invoke(() => {
  82.                 imgCircle.Visibility = Visibility.Collapsed;
  83.             });
  84.             finish = 1;
  85.         }
  86.  
  87.         void UpdateSize(string path, long size) {
  88.             lock (dictionary_lock) {
  89.                 if (!dict.ContainsKey(path))
  90.                     dict[path] = size;
  91.                 else
  92.                     dict[path] += size;
  93.                 //System.Windows.MessageBox.Show($"{path}: {size}");
  94.  
  95.             }
  96.             string[] spl = path.Split('\\');
  97.             if (path == selectedFolder)
  98.                 return;
  99.  
  100.             string[] shifted = new string[spl.Length-1];
  101.             Array.Copy(spl, shifted, spl.Length - 1);
  102.             //System.Windows.MessageBox.Show($"{path} (selected: {selectedFolder})");
  103.             UpdateSize(String.Join("\\", shifted), size);
  104.         }
  105.  
  106.         void ProcessFiles() {
  107.             while (finish != 1 || q.Count() > 0) {
  108.                 if (q.Count() == 0)
  109.                     continue;
  110.                 string current;
  111.                 lock (queue_lock)
  112.                     current = q.Dequeue();
  113.                 FileInfo fi = new FileInfo(current);
  114.                 UpdateSize(current, fi.Length);
  115.             }
  116.         }
  117.  
  118.         private void MenuItem_Click(object sender, RoutedEventArgs e) {
  119.            
  120.         }
  121.  
  122.         private void Scan_Click(object sender, RoutedEventArgs e) {
  123.             using (var dialog = new FolderBrowserDialog {
  124.                 Description = "Выберите папку для сканирования",
  125.                 ShowNewFolderButton = true
  126.             }) {
  127.                 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
  128.                     l1.Content = "Выбрана папка: "+dialog.SelectedPath;
  129.                     selectedFolder = dialog.SelectedPath;
  130.                 }
  131.             }
  132.         }
  133.         private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
  134.             scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta / 3);
  135.         }
  136.         private void Start_Click(object sender, RoutedEventArgs e) {
  137.             if (selectedFolder != "") {
  138.                 Task.Run(() => getFilesList(selectedFolder));
  139.                 Task.Run(() => ProcessFiles());
  140.             }
  141.         }
  142.         private void Select_Click(object sender, RoutedEventArgs e) {
  143.  
  144.         }
  145.         private void Load_Click(object sender, RoutedEventArgs e) {
  146.            
  147.         }
  148.         private void Save_Click(object sender, RoutedEventArgs e) {
  149.  
  150.         }
  151.  
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement