Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace FilesSeeker
- {
- public partial class Form1 : Form
- {
- public string _rootFolder = null;
- public List<FindedFile> _files = new List<FindedFile>();
- public int _browsedFolders = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void AnalyzeButton_Click(object sender, EventArgs e)
- {
- DirectoryInfo info = new DirectoryInfo(_rootFolder);
- try
- {
- if (!BrowsFolderWorker.IsBusy)
- {
- CancelBrowsingButton.Visible = true;
- AnalyzeButton.Enabled = false;
- BrowsFolderWorker.RunWorkerAsync(_rootFolder);
- CancelBrowsingButton.Visible = false;
- AnalyzeButton.Enabled = true;
- }
- }
- catch (Exception exception)
- {
- MessageBox.Show("Exception: " + exception.Message);
- }
- }
- private void SelectDirectoryButton_Click(object sender, EventArgs e)
- {
- bool shouldActivateAnalyzeButton = false;
- if (_rootFolder == null)
- shouldActivateAnalyzeButton = true;
- if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- _rootFolder = folderBrowserDialog1.SelectedPath;
- if (shouldActivateAnalyzeButton)
- {
- AnalyzeButton.Enabled = true;
- }
- }
- }
- private void BrowsFolderWorker_DoWork(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker worker = sender as BackgroundWorker;
- DirectoryInfo directoryInfo = e.Argument as DirectoryInfo;
- _browsedFolders = 1;
- List<FindedFile> files = new List<FindedFile>();
- try
- {
- for (int i = 0; i < directoryInfo.GetFiles().Length; i++)
- {
- if (worker.CancellationPending)
- {
- e.Cancel = true;
- return;
- }
- else
- {
- try
- {
- var findedFile = directoryInfo.GetFiles()[i];
- files.Add(new FindedFile(findedFile.FullName));
- }
- catch
- { }
- }
- }
- int directoriesCount = directoryInfo.GetDirectories().Length;
- for (int i = 0; i < directoriesCount; i++)
- {
- if (worker.CancellationPending)
- {
- e.Cancel = true;
- return;
- }
- else
- {
- try
- {
- var findedDirectory = directoryInfo.GetDirectories()[i];
- var filesInFolder = FileBrowser.GetFilesInFolder(findedDirectory);
- files.AddRange(filesInFolder.Files);
- _browsedFolders = _browsedFolders + filesInFolder.BrowsedFolders;
- if (directoriesCount > 0)
- {
- int progress = (i / directoriesCount) * 100;
- worker.ReportProgress(progress);
- }
- }
- catch
- { }
- }
- }
- }
- catch (Exception exception)
- {
- if (exception != null)
- {
- if (!(exception is UnauthorizedAccessException))
- {
- throw exception;
- }
- }
- }
- _files = files;
- }
- private void BrowsFolderWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- if (browsingProgress.Visible == false)
- browsingProgress.Visible = true;
- browsingProgress.Value = e.ProgressPercentage;
- }
- private void BrowsFolderWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- if (e.Cancelled)
- {
- MessageBox.Show("The browsing was cancelled.", "Browsing Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- else if (e.Error != null)
- {
- MessageBox.Show("An error occured: " + Environment.NewLine + e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- else
- {
- if (dataGridView1.DataSource == null)
- dataGridView1.DataSource = _files;
- BrowserCountLabel.Text = "Browsed folders: " + _browsedFolders;
- FileCountLabel.Text = "Finded files: " + _files.Count;
- MessageBox.Show("Browing completed: " + _files.Count + " files finds.", "Browsing Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement