public BuildCacheFormLogic(BuildCacheForm buildCacheForm) { Constrain.NotNull(buildCacheForm); this.buildCacheForm = buildCacheForm; initListener(); initAction(); }
void buildCacheForm_FormClosing(object sender, FormClosingEventArgs e) { Constrain.NotNull(buildCacheAction); if (buildCacheAction.IsWorking) { MessageBox.Show("正在构建缓存,如果要退出,请先取消,或者等待任务完成"); e.Cancel = true; } }
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); } }
public MainFormLogic(MainForm targetForm) { Constrain.NotNull(targetForm); this.targetForm = targetForm; // 订阅整个窗体的 Shown 事件和 searchBT 点击事件 targetForm.Shown += new EventHandler(targetForm_Shown); targetForm.searchBT.Click += new EventHandler(searchBT_Click); // 订阅窗体中 clearCacheLL 的点击事件 targetForm.clearCacheLL.Click += new EventHandler(clearCacheLL_Click); // 将 BuildCacheFormLogic 类的对象附加到缓存生成窗体上 BuildCacheFormLogic buildCacheFormLogic = new BuildCacheFormLogic(targetForm.BuildCacheForm); // 订阅 BuildCacheForm 窗体的 Shown 和 FormClosed 事件 // 用于判断用户是否又重新构建了缓存,以便重新加载 targetForm.BuildCacheForm.Shown += new EventHandler(BuildCacheForm_Shown); targetForm.BuildCacheForm.FormClosed += new FormClosedEventHandler(BuildCacheForm_FormClosed); }
bool searchData(string filename, SearchDataDelegate sdd) { // sdd 不能为 null // 但是 filename 可以随意 if (sdd == null) { throw new ArgumentNullException(); } if (fileEntries == null || fileEntries.Count == 0) { return(false); } // 预先编译好正则表达式 // 用得着用不着得后面判断 Regex regexEngine = null; if (regex) { try { string pattern = filename; regexEngine = new Regex(pattern, regexOptions); } catch (System.Exception ex) { // 出错,很可能是正则表达式有误 // 把异常向上传递 throw new Exception("正则表达式不正确:" + ex.Message); } } // 将文件名转为小写,方便快速匹配 // 注意这一操作必须放在前一句—— // 构造正则表达式引擎之后 if (!string.IsNullOrEmpty(filename)) { filename = filename.ToLower(); } DateTime dt = DateTime.Now; bool success = false; int count = 0; for (int i = 0; i < fileEntries.Count; i++) { FileEntry fileEntry = fileEntries[i]; // 根据用户的要求,采用不同的匹配规则进行匹配 if (same) // 要求精确匹配(但是大小写不敏感) { if (fileEntry.FilenameLC == filename) { ++count; // sdd 返回 false 说明不需要再继续 // 向下寻找了,所以直接跳出 if (sdd(i, fileEntry, GroupEnum.Same) == false) { success = true; break; } } } else { // 要求正则匹配 if (regex) { Constrain.NotNull(regexEngine); Match match = regexEngine.Match(fileEntry.Filename); if (match.Success && match.Index == 0 && match.Length == fileEntry.Filename.Length) { ++count; if (sdd(i, fileEntry, GroupEnum.Regex) == false) { success = true; break; } } } else { // 注意下面三种匹配不是互斥的 // 用户如果多选,则都会进行匹配检查 // 如果匹配,都会添加到最终结果中 // 要求开头匹配 if (startWith) { if (fileEntry.FilenameLC.StartsWith(filename)) { ++count; if (sdd(i, fileEntry, GroupEnum.StartWith) == false) { success = true; break; } } } // 要求中间匹配 if (exist) { if (fileEntry.FilenameLC.IndexOf(filename) > -1) { ++count; if (sdd(i, fileEntry, GroupEnum.Exist) == false) { success = true; break; } } } // 要求结尾匹配 if (endWith) { if (fileEntry.FilenameLC.EndsWith(filename)) { ++count; if (sdd(i, fileEntry, GroupEnum.EndWith) == false) { success = true; break; } } } } } } TimeSpan ts = DateTime.Now - dt; string tip = string.Format("搜索得{0}条记录 共耗时{1}s", count, ts.TotalSeconds); Trace.WriteLine(tip); targetForm.SearchInfo = tip; return(success); }