Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace FilesSeeker
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public string _rootFolder = null;
  16.         public List<FindedFile> _files = new List<FindedFile>();
  17.  
  18.         public int _browsedFolders = 0;
  19.  
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void AnalyzeButton_Click(object sender, EventArgs e)
  27.         {
  28.             DirectoryInfo info = new DirectoryInfo(_rootFolder);
  29.             try
  30.             {
  31.                 if (!BrowsFolderWorker.IsBusy)
  32.                 {
  33.                     CancelBrowsingButton.Visible = true;
  34.                     AnalyzeButton.Enabled = false;
  35.  
  36.                     BrowsFolderWorker.RunWorkerAsync(_rootFolder);
  37.  
  38.                     CancelBrowsingButton.Visible = false;
  39.                     AnalyzeButton.Enabled = true;
  40.                 }
  41.             }
  42.             catch (Exception exception)
  43.             {
  44.                 MessageBox.Show("Exception: " + exception.Message);
  45.             }
  46.                        
  47.         }
  48.  
  49.         private void SelectDirectoryButton_Click(object sender, EventArgs e)
  50.         {
  51.             bool shouldActivateAnalyzeButton = false;
  52.             if (_rootFolder == null)
  53.                 shouldActivateAnalyzeButton = true;
  54.  
  55.             if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  56.             {
  57.                 _rootFolder = folderBrowserDialog1.SelectedPath;
  58.                 if (shouldActivateAnalyzeButton)
  59.                 {
  60.                     AnalyzeButton.Enabled = true;
  61.                 }
  62.             }
  63.         }
  64.  
  65.        
  66.  
  67.         private void BrowsFolderWorker_DoWork(object sender, DoWorkEventArgs e)
  68.         {
  69.             BackgroundWorker worker = sender as BackgroundWorker;
  70.  
  71.             DirectoryInfo directoryInfo = e.Argument as DirectoryInfo;
  72.             _browsedFolders = 1;
  73.             List<FindedFile> files = new List<FindedFile>();
  74.             try
  75.             {
  76.                 for (int i = 0; i < directoryInfo.GetFiles().Length; i++)
  77.                 {
  78.                     if (worker.CancellationPending)
  79.                     {
  80.                         e.Cancel = true;
  81.                         return;
  82.                     }
  83.                     else
  84.                     {
  85.                         try
  86.                         {
  87.                             var findedFile = directoryInfo.GetFiles()[i];
  88.                             files.Add(new FindedFile(findedFile.FullName));
  89.                         }
  90.                         catch
  91.                         { }
  92.                     }
  93.                 }
  94.                 int directoriesCount = directoryInfo.GetDirectories().Length;
  95.                 for (int i = 0; i < directoriesCount; i++)
  96.                 {
  97.                     if (worker.CancellationPending)
  98.                     {
  99.                         e.Cancel = true;
  100.                         return;
  101.                     }
  102.                     else
  103.                     {
  104.                         try
  105.                         {
  106.  
  107.                             var findedDirectory = directoryInfo.GetDirectories()[i];
  108.                             var filesInFolder = FileBrowser.GetFilesInFolder(findedDirectory);
  109.                             files.AddRange(filesInFolder.Files);
  110.                             _browsedFolders = _browsedFolders + filesInFolder.BrowsedFolders;
  111.  
  112.                             if (directoriesCount > 0)
  113.                             {
  114.                                 int progress = (i / directoriesCount) * 100;
  115.                                 worker.ReportProgress(progress);
  116.                             }
  117.                         }
  118.                         catch
  119.                         { }
  120.                     }
  121.                 }
  122.             }
  123.             catch (Exception exception)
  124.             {
  125.                 if (exception != null)
  126.                 {
  127.                     if (!(exception is UnauthorizedAccessException))
  128.                     {
  129.                       throw exception;
  130.                     }
  131.                 }
  132.             }
  133.             _files = files;
  134.  
  135.         }
  136.  
  137.         private void BrowsFolderWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  138.         {
  139.             if (browsingProgress.Visible == false)
  140.                 browsingProgress.Visible = true;
  141.             browsingProgress.Value = e.ProgressPercentage;
  142.         }
  143.  
  144.         private void BrowsFolderWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  145.         {
  146.             if (e.Cancelled)
  147.             {
  148.                 MessageBox.Show("The browsing was cancelled.", "Browsing Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  149.             }
  150.             else if (e.Error != null)
  151.             {
  152.                 MessageBox.Show("An error occured: " + Environment.NewLine + e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  153.             }
  154.             else
  155.             {
  156.                 if (dataGridView1.DataSource == null)
  157.                     dataGridView1.DataSource = _files;
  158.                 BrowserCountLabel.Text = "Browsed folders: " + _browsedFolders;
  159.                 FileCountLabel.Text = "Finded files: " + _files.Count;
  160.  
  161.                 MessageBox.Show("Browing completed: " + _files.Count + " files finds.", "Browsing Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
  162.             }
  163.         }
  164.  
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement