string getFilepath(ListViewItem lvi) { Constrain.NotNull(lvi); try { const int filepathIndex = 1; return(lvi.SubItems[filepathIndex].Text); } catch (System.Exception ex) { Constrain.UnreachableCode("出现 BUG,请调试!错误:" + ex.Message); return(null); } }
// 用户点击“查找”按钮 void searchBT_Click(object sender, EventArgs e) { // 记录下单选框的信息 same = targetForm.sameCB.Checked; startWith = targetForm.startWithCB.Checked; exist = targetForm.existCB.Checked; endWith = targetForm.endWithCB.Checked; regex = targetForm.regexCB.Checked; // 记录下正则表达式选项信息 regexOptions = targetForm.RegexConfigForm.RegexOptions; string filename = targetForm.filenameTB.Text; targetForm.resultLV.Items.Clear(); // 清空上一次的搜索结果 const int maxCount = 1000; // 限制最多向用户显示的搜索结果数目 int count = 0; try { searchData(filename, delegate(int i, FileEntry fileEntry, GroupEnum groupEnum) { ListViewItem lvi = new ListViewItem(new string[] { fileEntry.Filename, fileEntry.FilePath }); ListViewGroup group = null; switch (groupEnum) { case GroupEnum.Same: group = targetForm.resultLV.Groups["Same"]; break; case GroupEnum.StartWith: group = targetForm.resultLV.Groups["StartWith"]; break; case GroupEnum.Exist: group = targetForm.resultLV.Groups["Exist"]; break; case GroupEnum.EndWith: group = targetForm.resultLV.Groups["EndWith"]; break; case GroupEnum.Regex: group = targetForm.resultLV.Groups["Regex"]; break; default: Constrain.UnreachableCode("难道你添加了新的 GroupEnum 却没有修改这里的代码?"); break; } if (group != null) { lvi.Group = group; targetForm.resultLV.Items.Add(lvi); } else { Constrain.UnreachableCode("怎么 group 会为 null?存在 BUG !"); } return(++count < maxCount); }); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } }