Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

Multithreaded File/Folder Finder

Rate me:
Please Sign up or sign in to vote.
4.19/5 (18 votes)
26 May 2010CPOL17 min read 110.2K   5.6K   125  
File Find is fast, especially if you have multiple physical drives; version 2.1.0.17.
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace FileFind
{
    /// <summary>
    /// Displays a list of all available disk drives and allows selection of one or more disks.
    /// Disks listed in the configuration ignoreFolders array are marked as not selected
    /// </summary>
    public partial class DiskSelection : Form
    {
        private ArrayList selected;
        private ArrayList ignoreFolders;    //local copy

        private Point location = new Point();
        private bool locationSet = false;
        public Point FormLocation
        {
            get { return location; }
            set
            {
                location = value;
                locationSet = true;
            }
        }

        public DiskSelection(ArrayList select)
        {
            selected = select;
            InitializeComponent();
        }

        /// <summary>
        /// Build a list of disk drives, high lighting the selected drives
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DiskSelection_Load(object sender, EventArgs e)
        {
            if (locationSet)
                this.Location = location;
            ConfigInfo configInfo = new ConfigInfo();
            this.Font = configInfo.UseFont;
            this.Text = configInfo.ProductName + " - " + this.Text;
            ignoreFolders = configInfo.IgnoreFolders;

            DriveInfo[] allDrives = null;
            try { allDrives = DriveInfo.GetDrives(); }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, configInfo.ProductName + " DiskSelection_Load",
                                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                allDrives = null;
            }
            int readyDrives = 0;
            selectListBox.BeginUpdate();
            foreach (DriveInfo drive in allDrives)
            {   //show drives available
                if (drive.IsReady)
                {
                    ++readyDrives;
                    string driveDisplay = drive.Name + drive.VolumeLabel;
                    if (DriveType.Network == drive.DriveType)
                        driveDisplay += " (Network)";
                    int i = selectListBox.Items.Add(driveDisplay);
                    foreach (string dl in selected)
                        if (dl[0] == drive.Name[0])
                        {   //drive was preselected
                            if (FoundInIgnoreDirectory(drive.Name))
                                selectListBox.SetSelected(i, false);
                            else
                                selectListBox.SetSelected(i, true);
                            break;
                        }
                }
            }
            selectListBox.EndUpdate();
            if (selected.Count == readyDrives)
                selectAllDisks.Checked = false; //all drives have been selected
        } //ends private void DiskSelection_Load(...

        /// <summary>
        /// Select or unselect all drives in the display list
        /// Driven by the selectAllDisks check box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectAllDisks_CheckedChanged(object sender, EventArgs e)
        {   //  Entered when the 'Select all disks' check mark is changed
            //  All display items are marked as either selected or not selected
            //depending on their prior setting.
            if (selectAllDisks.Checked)
            {//swap all ready drives to selected status
                int numitems = selectListBox.Items.Count;
                for (int i = 0; i < numitems; ++i)
                    selectListBox.SetSelected(i, true);
                bool[] makeFalse = new bool[numitems];
                for (int i = 0; i < makeFalse.Length; ++i)
                    makeFalse[i] = false;
                for (int i = 0; i < numitems; ++i)
                {//count selections and store the drive character
                    String drivesSelected = selectListBox.SelectedItems[i].ToString();
                    string driveLetter = drivesSelected[0] + @":\";
                    if (FoundInIgnoreDirectory(driveLetter))
                        makeFalse[i] = true;
                    //I originally used "selectListBox.SetSelected(i, false)" instead of the makeFalse array.
                    //It caused the item after the set false to be skipped.
                }
                for (int i = 0; i < makeFalse.Length; ++i)
                    if (makeFalse[i])
                        selectListBox.SetSelected(i, false);
            }
            else
                selectListBox.ClearSelected();
        }

        /// <summary>
        /// determines if a directory has been excluded
        /// </summary>
        /// <param name="directoryInfo">directory under consideration</param>
        /// <returns>true if the directory is to be ignored</returns>
        private bool FoundInIgnoreDirectory(string driveLetter)
        {   //This routine is repeated in several classes.
            //This version does not have hidden file logic.
            if (String.IsNullOrEmpty(driveLetter) || (driveLetter.Length < 3))
                return true;
            string fwdSlash = driveLetter.Replace('\\', '/');
            foreach (string str in ignoreFolders)
            {
                if (':' == str[1])
                {   //fully qualified folder name by drive?
                    if (Char.IsLetter(str[0]))
                    {
                        string folder_str = str.Replace('\\', '/');
                        if (string.Compare(folder_str, fwdSlash, true) == 0)
                            return true;
                    }
                }
                if (string.Compare(str, fwdSlash, true) == 0)
                    return true;
            }
            return false;
        }   //ends private bool FoundInIgnoreDirectory(...

        /// <summary>
        /// Ignore any changes and exit the disk selection form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelBbutton_Click(object sender, EventArgs e)
        { this.Close(); }

        /// return DialogResult.Yes if all drives were selected
        /// return DialogResult.No if some drives were selected
        /// return DialogResult.Abort if no drives were selected
        private void exitButton_Click(object sender, EventArgs e)
        {
            selected = new ArrayList();
            if (-1 != selectListBox.SelectedIndex)
            {//determine which drives have been selected
                for (int i = 0; i < selectListBox.SelectedItems.Count; ++i)
                {//count selections and store the drive character
                    String selectedDrive = selectListBox.SelectedItems[i].ToString();
                    string ds = selectedDrive.Substring(0, selectedDrive.IndexOf("\\") + 1);
                    selected.Add(ds);
                }
                this.DialogResult = DialogResult.No;
            }
            ConfigInfo configInfo = new ConfigInfo();
            if (0 == selected.Count)
            {
                //MessageBox.Show("No drives were selected",
                //        configInfo.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.DialogResult = DialogResult.Abort;
                //return;
            }
            else
            {
                if (selectListBox.Items.Count == selected.Count)
                    this.DialogResult = DialogResult.Yes;
                configInfo.SelectedDisks = selected;    //return selected disks via ConfigInfo class
            }
            this.Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) retired
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions