Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- using Label = System.Windows.Controls.Label;
- namespace s4l5 {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window {
- static int idd = 0;
- private string selectedFolder = "";
- private object queue_lock = new object(), dictionary_lock = new object();
- Dictionary<string, long> dict = new Dictionary<string, long>();
- Queue<string> q = new Queue<string>();
- int finish = -1;
- public MainWindow() {
- InitializeComponent();
- DispatcherTimer timer = new DispatcherTimer() {
- Interval = TimeSpan.FromSeconds(1)
- };
- timer.Tick += UpdateList;
- timer.Start();
- }
- string[] sizes = { "Байт", "КБ", "МБ", "ГБ", "ТБ" };
- string getSize(long size) {
- double res = size;
- int i;
- for (i = 0; res / 1024.0 > 1; i++)
- res /= 1024.0;
- return (Math.Round(res * 100)/100.0).ToString() + sizes[i];
- }
- void UpdateList(object sender, EventArgs e) {
- if (finish == -1)
- return;
- listView.Items.Clear();
- foreach (string dir in Directory.GetDirectories(selectedFolder))
- lock (dictionary_lock)
- listView.Items.Add(new Label() { Content = $"{dir}: {(dict.ContainsKey(dir) ? getSize(dict[dir]) : "???")}" });
- }
- void getFilesList(string path) {
- finish = 0;
- Dispatcher.Invoke(() => {
- imgCircle.Visibility = Visibility.Visible;
- });
- Queue<string> queue = new Queue<string>();
- queue.Enqueue(path);
- while (queue.Count > 0) {
- string current = queue.Dequeue();
- foreach (string dir in Directory.GetDirectories(current))
- queue.Enqueue(dir);
- lock (queue_lock)
- foreach (string fl in Directory.GetFiles(current))
- q.Enqueue(fl);
- }
- Dispatcher.Invoke(() => {
- imgCircle.Visibility = Visibility.Collapsed;
- });
- finish = 1;
- }
- void UpdateSize(string path, long size) {
- lock (dictionary_lock) {
- if (!dict.ContainsKey(path))
- dict[path] = size;
- else
- dict[path] += size;
- //System.Windows.MessageBox.Show($"{path}: {size}");
- }
- string[] spl = path.Split('\\');
- if (path == selectedFolder)
- return;
- string[] shifted = new string[spl.Length-1];
- Array.Copy(spl, shifted, spl.Length - 1);
- //System.Windows.MessageBox.Show($"{path} (selected: {selectedFolder})");
- UpdateSize(String.Join("\\", shifted), size);
- }
- void ProcessFiles() {
- while (finish != 1 || q.Count() > 0) {
- if (q.Count() == 0)
- continue;
- string current;
- lock (queue_lock)
- current = q.Dequeue();
- FileInfo fi = new FileInfo(current);
- UpdateSize(current, fi.Length);
- }
- }
- private void MenuItem_Click(object sender, RoutedEventArgs e) {
- }
- private void Scan_Click(object sender, RoutedEventArgs e) {
- using (var dialog = new FolderBrowserDialog {
- Description = "Выберите папку для сканирования",
- ShowNewFolderButton = true
- }) {
- if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
- l1.Content = "Выбрана папка: "+dialog.SelectedPath;
- selectedFolder = dialog.SelectedPath;
- }
- }
- }
- private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
- scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta / 3);
- }
- private void Start_Click(object sender, RoutedEventArgs e) {
- if (selectedFolder != "") {
- Task.Run(() => getFilesList(selectedFolder));
- Task.Run(() => ProcessFiles());
- }
- }
- private void Select_Click(object sender, RoutedEventArgs e) {
- }
- private void Load_Click(object sender, RoutedEventArgs e) {
- }
- private void Save_Click(object sender, RoutedEventArgs e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement