public static List <string> SearchFile(string path, string searchFileName, SearchFileNameOption opt, string format) { List <string> result = new List <string>(); List <string> allDir = new List <string>(); GetAllDirectories(path, ref allDir); foreach (string dir in allDir) { foreach (string fileName in Directory.GetFiles(dir)) { bool shouldBeAdded = true; FileInfo info = new FileInfo(fileName); if (searchFileName != string.Empty) { switch (opt) { case SearchFileNameOption.MatchCase: shouldBeAdded = Path.GetFileNameWithoutExtension(fileName).Contains(searchFileName); break; case SearchFileNameOption.WholeWord: shouldBeAdded = Path.GetFileNameWithoutExtension(fileName) == searchFileName; break; case SearchFileNameOption.Normal: shouldBeAdded = Path.GetFileNameWithoutExtension(fileName).ToLower().Contains(searchFileName.ToLower()); break; default: break; } } if ((shouldBeAdded) && (format != string.Empty)) { shouldBeAdded = info.Extension.Contains(format); } if (shouldBeAdded) { result.Add(fileName); } } } return(result); }
private void SearchButton_Click(object sender, EventArgs e) { if (PathTextBox.Text == string.Empty) { MessageBox.Show("Please select a Path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); return; } //List<string> list = new List<string>(); //SearchIO.GetAllDirectories("E:\\", ref list); //List<string> allFiles = SearchIO.SearchFile(PathTextBox.Text); //ResultListBox.Items.AddRange(allFiles.ToArray()); ResultListView.Items.Clear(); //List<string> allFiles = SearchIO.SearchFile(PathTextBox.Text , ExtensionTextBox.Text); //ResultListBox.Items.AddRange(allFiles.ToArray()); SearchFileNameOption opt = MatchCaseRadioButton.Checked ? SearchFileNameOption.MatchCase : WholeWordRadioButton.Checked ? SearchFileNameOption.WholeWord : SearchFileNameOption.Normal; List <string> allFiles = new List <string>(); if (string.IsNullOrEmpty(NameTextBox.Text) && string.IsNullOrEmpty(ExtensionTextBox.Text)) { allFiles = SearchIO.SearchFile(PathTextBox.Text); } else { allFiles = SearchIO.SearchFile(PathTextBox.Text, NameTextBox.Text, opt, ExtensionTextBox.Text); } //ResultListBox.Items.AddRange(allFiles.ToArray()); foreach (string fileName in allFiles) { ListViewItem item = new ListViewItem() { Text = Path.GetFileName(fileName), ImageIndex = 0 }; ResultListView.Items.Add(item); toolStripStatusCountlable.Text = ResultListView.Items.Count.ToString(); } }
public static List <string> SearchFile(string path, string searchFileName, SearchFileNameOption opt) { return(SearchFile(path, searchFileName, opt, string.Empty)); }