示例#1
0
        public frmMain()
        {
            InitializeComponent();

            #if DEBUG
              cbFolder.Text = @"C:\Users\yo\Documents\FileSearch\FileSearch\bin\Debug";
            #endif

              lstFilePattern.BringToFront();

              //Initialize private variables.
              _results = new List<string>();
              _searchLog = new System.Text.StringBuilder();
              _predefinedPatterns = new List<PredefinedPattern>();
              _searchFolderHistory = new List<SearchFolderHistory>();

              //Load the settings file.
              _settings = XElement.Load("settings.txt");

              //Parse each pattern and place it in the list box.
              foreach (var predefinedPattern in _settings.Element("predefinedPatterns").Elements("predefinedPattern")) {
            if (predefinedPattern.Attribute("Id") == null)
              predefinedPattern.Add(new XAttribute("Id", Guid.NewGuid().ToString()));

            var newPattern = new PredefinedPattern(predefinedPattern);

            _predefinedPatterns.Add(newPattern);
            lstFilePattern.Items.Add(newPattern);
              }

              if (_settings.Element("searchFolderHistory") == null) {
            //Search folder history node is missing from the settings file.
            //Add the node now. No need to save the settings file now.
            _settings.Add(new XElement("searchFolderHistory"));

              } else {
            //Parse each folder and place it in the ComboBox.
            foreach (var searchFolder in _settings.Element("searchFolderHistory").Elements("searchFolder")) {
              var newFolder = new SearchFolderHistory(searchFolder);

              _searchFolderHistory.Add(newFolder);
              cbFolder.Items.Add(newFolder);
            }
              }

              //Present program's version.
              this.Text += " v" + typeof(frmMain).Assembly.GetName().Version.ToString();

            #if DEBUG
              this.Text += " BETA";
            #endif
        }
示例#2
0
 private void cbFolder_SelectionChangeCommitted(object sender, EventArgs e)
 {
     _lastSelectedFolder = (SearchFolderHistory)cbFolder.SelectedItem;
 }
示例#3
0
        private void btnRemoveFolder_Click(object sender, EventArgs e)
        {
            if (_lastSelectedFolder != null) {
            //Remove the last selected folder from the settings and from the ComboBox.
            cbFolder.Items.Remove(_lastSelectedFolder);
            _searchFolderHistory.Remove(_lastSelectedFolder);

            //Find the folder in the settings and remove it.
            var settingSearch = (from p in _settings.Element("searchFolderHistory").Elements()
                             where p.Attribute("Id").Value == _lastSelectedFolder.Id
                             select p);

            //Remove the folder using a loop to ensure complete removal,
            //taking into account possible duplicates.
            foreach (var setting in settingSearch) {
              setting.Remove();
            }

            _settings.Save("settings.txt");
            _lastSelectedFolder = null;
              }
        }
示例#4
0
        private void btnFind_Click(object sender, EventArgs e)
        {
            if (_searchThread == null) {
            bool isValid = true;

            try {
              SetSearchMode(true);

              if (!System.IO.Directory.Exists(cbFolder.Text)) {
            isValid = false;
            MessageBox.Show("Folder '" + cbFolder.Text + "' does not exist.");
              }

              if (txSelectedPattern.Text.Length == 0) {
            isValid = false;
            MessageBox.Show("Enter or select a pattern to search.");
              }

              if (isValid) {
            var pattern = "(";
            var folder = cbFolder.Text;

            _searchLog.Clear();
            lstResults.Items.Clear();
            _results.Clear();

            //Check whether to concatenate selected patterns or use a pattern entered directly in the pattern TextBox.
            if (lstFilePattern.CheckedItems.Count == 0)
              pattern = txSelectedPattern.Text;
            else {
              foreach (PredefinedPattern item in lstFilePattern.CheckedItems) {
                pattern += item.Pattern + "|";
              }

              pattern = pattern.Substring(0, pattern.Length - 1) + ")";
            }

            //Check if the search folder is already in the history.
            var folderSearch = (from p in _searchFolderHistory
                                where p.Folder.ToLower() == folder.ToLower()
                                select p).ToArray();

            if (folderSearch.Length == 0) {
              //Search folder is new to the history.
              //Save the new folder in the settings file.
              var newFolder = new SearchFolderHistory(cbFolder.Text);
              var newFolderXml = new XElement("searchFolder");

              newFolderXml.Add(new XAttribute("Id", newFolder.Id));
              newFolderXml.Add(new XAttribute("Folder", newFolder.Folder));

              _settings.Element("searchFolderHistory").Add(newFolderXml);
              cbFolder.Items.Add(newFolder);
              _searchFolderHistory.Add(newFolder);

              _settings.Save("settings.txt");
            }

            //Prepare a background worker to perform the search in the background to prevent the screen
            //from locking up during search.
            _searchThread = new BackgroundWorker();
            _searchThread.WorkerSupportsCancellation = true;
            _searchThread.WorkerReportsProgress = true;

            _searchThread.DoWork += new DoWorkEventHandler(
              delegate(object backgroundSender, DoWorkEventArgs backgroundE) {
                //Execute the search.
                FindFiles(folder, pattern, chkSearchSubfolders.Checked, backgroundE);
              });

            _searchThread.ProgressChanged += new ProgressChangedEventHandler(
              delegate(object backgroundSender, ProgressChangedEventArgs backgroundE) {
                //Update the Label with folder being processed.
                lbFilesFound.Text = (string)backgroundE.UserState;
              });

            _searchThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
              delegate(object backgroundSender, RunWorkerCompletedEventArgs backgroundE) {
                //Show final search status to the user.
                if (backgroundE.Cancelled) {
                  lbFilesFound.Text = "Search cancelled";
                  _searchLog.AppendLine("Search cancelled");

                } else if (_results.Count == 0) {
                  lbFilesFound.Text = "No files were found.";
                  _searchLog.AppendLine("Search complete. No files were found.");

                } else {
                  lstResults.Items.AddRange(_results.ToArray());
                  lbFilesFound.Text = lstResults.Items.Count + " file(s) were found. Right-click on a line to see additional options.";
                  _searchLog.AppendLine("Search complete. " + lstResults.Items.Count + " file(s) were found.");
                }

                btnFind.Text = "Find";
                SetSearchMode(false);
                _searchThread = null;

                //Show the search log.
                var showSearchLog = new frmSearchLog();
                showSearchLog.RefreshLog(_searchLog.ToString());
                showSearchLog.ShowDialog();
              });

            //Execute the search in the background.
            _searchThread.RunWorkerAsync();

            btnFind.Text = "Cancel";

            base.Cursor = Cursors.Default;
              } else {
            SetSearchMode(false);
            lbFilesFound.Text = "Search cancelled";
              }
            } catch (Exception ex) {
              SetSearchMode(false);

              btnFind.Text = "Find";

              _searchThread = null;

              MessageBox.Show("An error occurred processing patterns.\nError message: " + ex.Message);
            }
              } else {
            //The user clicked the Find button while a search is performed.
            try {
              //Cancel the current search.
              _searchThread.CancelAsync();
            } catch (Exception ex) {
              MessageBox.Show("An error occurred cancelling the search.\nError message: " + ex.Message);
              SetSearchMode(false);
            }
              }
        }