//选择搜索路径 private void selectFile_Click(object sender, EventArgs e) { //定义委托 //Action<string> actionDelegate = (x) => { listBox1.Items.Add(x); }; DialogResult formResult = folderBrowserDialog1.ShowDialog(); if (formResult == System.Windows.Forms.DialogResult.OK) { this.txtFilepath.Text = folderBrowserDialog1.SelectedPath; //清空文件列表 //this.listBox1.Items.Clear(); ThreadPool.QueueUserWorkItem(delegate { //选取文件路径 DirectoryInfo dirInfo = new DirectoryInfo(this.txtFilepath.Text); //获取文件夹下所有文件 IOHelperExt.GetAllFiles(dirInfo, ref listFiles); //按文件大小排序 listFiles = listFiles.OrderBy(p => p.Length).ToList(); //foreach (var item in listFiles) //{ // listBox1.BeginInvoke(actionDelegate, item.FullName); //} }); } }
/// <summary> /// 文件项单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listBox_DoubleClick(object sender, EventArgs e) { //Clipboard.SetDataObject(listBox1.SelectedItem); ListBox listbox = (ListBox)sender; string fileSelectItem = listbox.SelectedItem.ToString(); string path = fileSelectItem.Substring(0, fileSelectItem.LastIndexOf('\\')); //打开所在文件夹 IOHelperExt.ExplorePath(fileSelectItem, true); }
/// <summary> /// 内容查找 /// </summary> /// <param name="searchValue"></param> private void ContentCheck(string searchValue, IList <FileInfo> fileList, CountdownEvent handler, Action <string> listbox2Delegate, Action <string> disedDelegate) { CommForAdolph.OfficeHelper officeHelper = new CommForAdolph.OfficeHelper(); foreach (var item in fileList)//listBox1.Items) { if (handler.CurrentCount == 0) { return; } //文件全路径 var itemStr = item.FullName.ToString(); int fileNameStartIndex = itemStr.LastIndexOf('\\'); if (itemStr.Substring(fileNameStartIndex, itemStr.Length - fileNameStartIndex).Contains(searchValue)) { if (!listBox2.Items.Contains(itemStr)) { this.listBox2.BeginInvoke(listbox2Delegate, itemStr); } //listBox2.Items.Add(item); } if (itemStr.Contains("~$")) { continue; } if (item.Extension == ".doc" || item.Extension == ".docx") { if (officeHelper.CheckWordContent(itemStr, searchValue)) { this.listBox2.BeginInvoke(listbox2Delegate, itemStr); } } else if (item.Extension == ".xls" || item.Extension == ".xlsx") { if (officeHelper.CheckExcelContent(itemStr, searchValue)) { this.listBox2.BeginInvoke(listbox2Delegate, itemStr); } } //检查搜索内容 else if (IOHelperExt.CheckContent(searchValue, itemStr) != -1) { this.listBox2.BeginInvoke(listbox2Delegate, itemStr); } if (handler.CurrentCount > 0) { handler.Signal(); this.label1.BeginInvoke(disedDelegate, handler.CurrentCount.ToString()); } } }