From 21f1a51a0707c81e69cdbf97fd61baf68b1c85dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Fri, 22 Apr 2022 18:45:16 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=90=9C=E7=B4=A2=E5=8C=BA=E5=9F=9F?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=202=E3=80=81=E7=B4=A2=E5=BC=95=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=8C=BA=E5=9F=9F=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/AreaEditWindow.xaml | 21 ++++++- TextLocator/AreaEditWindow.xaml.cs | 91 ++++++++++++++++++++++++++++-- TextLocator/AreaInfoItem.xaml | 15 ++--- TextLocator/AreaInfoItem.xaml.cs | 2 + TextLocator/AreaWindow.xaml | 40 +++++++------ TextLocator/Core/AppConst.cs | 4 -- TextLocator/Entity/AreaInfo.cs | 5 ++ TextLocator/MainWindow.xaml.cs | 18 +++--- TextLocator/Util/AreaUtil.cs | 30 +++++++++- TextLocator/Util/FileTypeUtil.cs | 32 +++++++++-- TextLocator/Util/FileUtil.cs | 7 ++- 11 files changed, 210 insertions(+), 55 deletions(-) diff --git a/TextLocator/AreaEditWindow.xaml b/TextLocator/AreaEditWindow.xaml index 97c3a2b..f983051 100644 --- a/TextLocator/AreaEditWindow.xaml +++ b/TextLocator/AreaEditWindow.xaml @@ -5,19 +5,19 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TextLocator" mc:Ignorable="d" - Title="区域编辑" Height="350" Width="600" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow" ResizeMode="CanMinimize" Icon="/Resource/App.ico" Loaded="Window_Loaded" > + Title="区域编辑" Height="465" Width="830" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow" ResizeMode="CanMinimize" Icon="/Resource/App.ico" Loaded="Window_Loaded" > - + - + @@ -29,6 +29,21 @@ + + + + + + + + + + + + + + + diff --git a/TextLocator/AreaEditWindow.xaml.cs b/TextLocator/AreaEditWindow.xaml.cs index bb7df2d..26fa9e4 100644 --- a/TextLocator/AreaEditWindow.xaml.cs +++ b/TextLocator/AreaEditWindow.xaml.cs @@ -1,8 +1,10 @@ -using System; +using log4net; +using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using TextLocator.Entity; +using TextLocator.Enums; using TextLocator.Message; using TextLocator.Util; @@ -13,11 +15,17 @@ namespace TextLocator /// public partial class AreaEditWindow : Window { + private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + /// - /// 索引区文件夹 + /// 搜索区文件夹 /// private List _areaFolders = new List(); /// + /// 搜索区文件类型 + /// + private List _areaFileTypes = new List(); + /// /// 区域信息 /// private AreaInfo _areaInfo; @@ -35,7 +43,8 @@ namespace TextLocator { _areaInfo = new AreaInfo() { - AreaId = "Area" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + AreaId = "Area" + DateTime.Now.ToString("yyyyMMddHHmmssffff"), + AreaFileTypes = FileTypeUtil.GetFileTypesNotAll() }; } } @@ -47,7 +56,21 @@ namespace TextLocator /// private void Window_Loaded(object sender, RoutedEventArgs e) { + // 初始化加载文件类型 + LoadFileType(); + + // 加载区域信息 + LoadAreaInfo(); + } + + /// + /// 加载区域信息 + /// + private void LoadAreaInfo() + { + // 区域名称 this.AreaName.Text = _areaInfo.AreaName; + // 区域文件夹 this.AreaFolders.Items.Clear(); if (_areaInfo.AreaFolders != null) { @@ -62,6 +85,57 @@ namespace TextLocator this.AreaFolders.Items.Add(folder); } } + // 区域文件夹类型 + foreach (UIElement element in this.AreaFileTypes.Children) + { + CheckBox checkbox = element as CheckBox; + if (_areaInfo.AreaFileTypes.Contains((FileType)checkbox.Tag)) + { + checkbox.IsChecked = true; + } + } + } + + /// + /// 初始化加载文件类型 + /// + private void LoadFileType() + { + this.AreaFileTypes.Children.Clear(); + // 遍历文件类型枚举 + foreach (FileType fileType in FileTypeUtil.GetFileTypesNotAll()) + { + // 构造UI元素 + CheckBox checkbox = new CheckBox() + { + Name = "FileType_" + (int)fileType, + Margin = new Thickness(10), + Tag = fileType, + Content = fileType.ToString() + "(" + fileType.GetDescription() + ")" + }; + checkbox.Checked += FileTypeStatusChange; + checkbox.Unchecked += FileTypeStatusChange; + this.AreaFileTypes.Children.Add(checkbox); + } + } + + /// + /// 文件类型选中状态切换 + /// + /// + /// + private void FileTypeStatusChange(object sender, RoutedEventArgs e) + { + List fileTypes = new List(); + foreach(UIElement element in this.AreaFileTypes.Children) + { + CheckBox checkBox = element as CheckBox; + if (checkBox.IsChecked == true) + { + fileTypes.Add((Enums.FileType)System.Enum.Parse(typeof(Enums.FileType), checkBox.Tag.ToString())); + } + } + _areaFileTypes = fileTypes; } /// @@ -144,14 +218,19 @@ namespace TextLocator MessageCore.ShowWarning("区域名称为空"); return; } + if (AreaUtil.GetAreaNameListRuleOut(_areaInfo).Contains(areaName)) + { + MessageCore.ShowWarning("区域名称不能重复"); + return; + } if (this.AreaFolders.Items.Count <= 0) { MessageCore.ShowWarning("至少需要一个文件夹"); return; } - if (AreaUtil.GetAreaNameListRuleOut(_areaInfo).Contains(areaName)) + if (_areaFileTypes.Count <= 0) { - MessageCore.ShowWarning("区域名称不能重复"); + MessageCore.ShowWarning("至少需要一个支持的文件类型"); return; } @@ -159,6 +238,8 @@ namespace TextLocator _areaInfo.AreaName = areaName; // 搜索区文件夹 _areaInfo.AreaFolders = _areaFolders; + // 搜索区文件类型 + _areaInfo.AreaFileTypes = _areaFileTypes; // 保存区域信息 // AreaUtil.SaveAreaInfo(_areaInfo); diff --git a/TextLocator/AreaInfoItem.xaml b/TextLocator/AreaInfoItem.xaml index a35547f..d83e250 100644 --- a/TextLocator/AreaInfoItem.xaml +++ b/TextLocator/AreaInfoItem.xaml @@ -7,21 +7,22 @@ mc:Ignorable="d" d:DesignHeight="50" d:DesignWidth="800"> - - - + + + - + - + + public static readonly Regex REGEX_TAG = new Regex("\\<.[^<>]*\\>"); /// - /// 匹配文件后缀 - /// - public static readonly Regex REGEX_FILE_EXT = new Regex(@"^.+\.(" + FileTypeUtil.GetFileTypeExts("|") + ")$"); - /// /// 匹配排除关键词 /// public static readonly Regex REGEX_EXCLUDE_KEYWORD = new Regex(@"(\$RECYCLE|360REC|SYSTEM|TEMP|SYSTEM VOLUME INFOMATION|\{(.*)\})"); diff --git a/TextLocator/Entity/AreaInfo.cs b/TextLocator/Entity/AreaInfo.cs index 9b73147..66c10be 100644 --- a/TextLocator/Entity/AreaInfo.cs +++ b/TextLocator/Entity/AreaInfo.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using TextLocator.Enums; namespace TextLocator.Entity { @@ -24,5 +25,9 @@ namespace TextLocator.Entity /// 区域文件夹路径 /// public List AreaFolders { get; set; } + /// + /// 区域文件类型 + /// + public List AreaFileTypes { get; set; } } } diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index f7eb192..17f2c0a 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -7,6 +7,7 @@ using System.ComponentModel; using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows; @@ -1068,19 +1069,22 @@ namespace TextLocator // 定义删除文件列表 List deleteFilePaths = new List(); + // 2.2、-------- 获取支持的文件类型后缀 + Regex fileExtRegex = new Regex(@"^.+\.(" + FileTypeUtil.ConvertToFileTypeExts(areaInfo.AreaFileTypes, "|") + ")$"); + // 扫描需要建立索引的文件列表 foreach (string s in areaInfo.AreaFolders) { log.Info("目录:" + s); // 获取文件信息列表 - FileUtil.GetAllFiles(allFilePaths, s); + FileUtil.GetAllFiles(allFilePaths, s, fileExtRegex); } msg = string.Format("搜索区【{0}】,文件扫描完成;文件数:{1},耗时:{2};开始分析需要更新的文件列表...", areaInfo.AreaName, allFilePaths.Count, scanTaskMark.ConsumeTime); log.Info(msg); ShowStatus(msg); - // 2.2、-------- 获取需要删除的文件列表 + // 2.3、-------- 获取需要删除的文件列表 if (AppUtil.ReadSectionList(areaIdIndex) != null) { foreach (string filePath in AppUtil.ReadSectionList(areaIdIndex)) @@ -1094,7 +1098,7 @@ namespace TextLocator } } - // 2.3、-------- 如果是更新操作,判断文件格式是否变化 -> 判断文件更新时间变化找到最终需要更新的文件列表 + // 2.4、-------- 如果是更新操作,判断文件格式是否变化 -> 判断文件更新时间变化找到最终需要更新的文件列表 var analysisTaskMark = TaskTime.StartNew(); // 更新是才需要校验,重建是直接跳过 if (!isRebuild) @@ -1129,7 +1133,7 @@ namespace TextLocator log.Info(msg); ShowStatus(msg); - // 2.4、-------- 验证扫描文件列表是否为空(如果是更新操作,判断文件格式是否变化 -> 判断文件更新时间变化找到最终需要更新的文件列表) + // 2.5、-------- 验证扫描文件列表是否为空(如果是更新操作,判断文件格式是否变化 -> 判断文件更新时间变化找到最终需要更新的文件列表) if (updateFilePaths.Count <= 0 && deleteFilePaths.Count <= 0) { build = false; @@ -1142,10 +1146,10 @@ namespace TextLocator // 后台执行时修改为最小线程单位,反之恢复为系统配置线程数 AppCore.SetThreadPoolSize(!isBackground); - // 2.5、-------- 创建索引方法 + // 2.6、-------- 创建索引方法 int errorCount = IndexCore.CreateIndex(areaInfo.AreaId, updateFilePaths, deleteFilePaths, isRebuild, ShowStatus); - // 2.6、-------- 当前区域完成日志 + // 2.7、-------- 当前区域完成日志 msg = string.Format("搜索区【{0}】,索引{1}完成;{2}数:{3},删除数:{4},错误数:{5},共用时:{6}。", areaInfo.AreaName, tag, tag, updateFilePaths.Count, deleteFilePaths.Count, errorCount, singleTaskMark.ConsumeTime); log.Info(msg); ShowStatus(msg); @@ -1156,7 +1160,7 @@ namespace TextLocator })); - // 2.7、-------- 记录文件总数、更新总数、删除总数、错误总数 + // 2.8、-------- 记录文件总数、更新总数、删除总数、错误总数 fileTotalCount = fileTotalCount + allFilePaths.Count; updateTotalCount = updateTotalCount + updateFilePaths.Count; deleteTotalCount = deleteTotalCount + deleteFilePaths.Count; diff --git a/TextLocator/Util/AreaUtil.cs b/TextLocator/Util/AreaUtil.cs index 12af3e9..06a4167 100644 --- a/TextLocator/Util/AreaUtil.cs +++ b/TextLocator/Util/AreaUtil.cs @@ -23,6 +23,10 @@ namespace TextLocator.Util /// 区域文件夹 /// private static string AreaFolders = "AreaFolders"; + /// + /// 区域文件类型 + /// + private static string AreaFileTypes = "AreaFileTypes"; /// /// 私有构造方法 @@ -48,17 +52,34 @@ namespace TextLocator.Util string areaName = AppUtil.ReadValue(areaId, AreaName, "区域名称"); // 区域文件夹 string folders = AppUtil.ReadValue(areaId, AreaFolders, ""); + // 区域文件类型 + string fileTypeNames = AppUtil.ReadValue(areaId, AreaFileTypes, ""); + + // 区域文件夹解析 List areaFolders = new List(); if (!string.IsNullOrEmpty(folders)) { areaFolders = folders.Split(',').ToList(); } + // 区域文件类型解析 + List areaFileTypes = new List(); + if (!string.IsNullOrEmpty(fileTypeNames)) + { + List tmps = fileTypeNames.Split(',').ToList(); + foreach (string tmp in tmps) + { + areaFileTypes.Add((Enums.FileType)System.Enum.Parse(typeof(Enums.FileType), tmp)); + } + } + + // 构造数据对象 AreaInfo areaInfo = new AreaInfo() { IsEnable = isEnable, AreaId = areaId, AreaName = areaName, - AreaFolders = areaFolders + AreaFolders = areaFolders, + AreaFileTypes = areaFileTypes }; // 全部搜索区 areaInfoList.Add(areaInfo); @@ -72,7 +93,8 @@ namespace TextLocator.Util IsEnable = true, AreaId = "AreaDefault", AreaName = "默认区域", - AreaFolders = new string[] { Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Environment.GetFolderPath(Environment.SpecialFolder.Desktop) }.ToList() + AreaFolders = new string[] { Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Environment.GetFolderPath(Environment.SpecialFolder.Desktop) }.ToList(), + AreaFileTypes = FileTypeUtil.GetFileTypesNotAll() }; areaInfoList.Add(areaInfo); } @@ -121,6 +143,8 @@ namespace TextLocator.Util AppUtil.WriteValue(areaInfo.AreaId, AreaName, areaInfo.AreaName); // 区域文件夹 AppUtil.WriteValue(areaInfo.AreaId, AreaFolders, areaInfo.AreaFolders != null ? string.Join(",", areaInfo.AreaFolders.ToArray()) : null); + // 区域文件类型 + AppUtil.WriteValue(areaInfo.AreaId, AreaFileTypes, areaInfo.AreaFileTypes != null ? string.Join(",", areaInfo.AreaFileTypes.ToArray()) : null); // 区域列表 AppUtil.WriteValue(AreaConfig, areaInfo.AreaId, areaInfo.IsEnable + ""); @@ -136,6 +160,8 @@ namespace TextLocator.Util AppUtil.WriteValue(areaInfo.AreaId, AreaName, null); // 区域文件夹 AppUtil.WriteValue(areaInfo.AreaId, AreaFolders, null); + // 区域文件类型 + AppUtil.WriteValue(areaInfo.AreaId, AreaFileTypes, null); // 区域列表 AppUtil.WriteValue(AreaConfig, areaInfo.AreaId, null); diff --git a/TextLocator/Util/FileTypeUtil.cs b/TextLocator/Util/FileTypeUtil.cs index c9a0d5b..3ba5a95 100644 --- a/TextLocator/Util/FileTypeUtil.cs +++ b/TextLocator/Util/FileTypeUtil.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.IO; +using System.Text; using TextLocator.Enums; namespace TextLocator.Util @@ -55,21 +57,39 @@ namespace TextLocator.Util /// /// 文件类型后缀列表(ext1,ext2...) /// + /// 文件类型列表 /// 分隔符,默认【,】 /// - public static string GetFileTypeExts(string separator = ",") + public static string ConvertToFileTypeExts(List fileTypes, string separator = ",") { - string fileTypeExts = ""; + string exts = ""; // 遍历文件类型,根据后缀查找文件类型 - foreach (FileType ft in Enum.GetValues(typeof(FileType))) + foreach (FileType ft in fileTypes) { - // 获取描述 string description = ft.GetDescription(); if (!string.IsNullOrEmpty(description)) { - fileTypeExts += description + separator; + exts += description + separator; + } + } + return exts.Substring(0, exts.Length - 1).Replace(",", separator); + } + + /// + /// 获取不包含全部的文件类型列表 + /// + /// + public static List GetFileTypesNotAll() + { + List fileTypes = new List(); + foreach(FileType ft in Enum.GetValues(typeof(FileType))) + { + if (ft == FileType.全部) + { + continue; } + fileTypes.Add(ft); } - return fileTypeExts.Substring(0, fileTypeExts.Length - 1).Replace(",", separator); + return fileTypes; } } } diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index 30a3087..e1e0337 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -125,7 +125,8 @@ namespace TextLocator.Util /// /// 文档列表 /// 根目录路径 - public static void GetAllFiles(List filePaths, string rootPath) + /// 文件后缀正则 + public static void GetAllFiles(List filePaths, string rootPath, Regex fileExtRegex) { // 根目录 DirectoryInfo rootDir = new DirectoryInfo(rootPath); @@ -148,7 +149,7 @@ namespace TextLocator.Util continue; } // 递归调用 - GetAllFiles(filePaths, dirPath); + GetAllFiles(filePaths, dirPath, fileExtRegex); } } catch (UnauthorizedAccessException ex) { @@ -164,7 +165,7 @@ namespace TextLocator.Util { // 查找word文件 string[] paths = Directory.GetFiles(rootPath) - .Where(file => AppConst.REGEX_FILE_EXT.IsMatch(file)) + .Where(file => fileExtRegex.IsMatch(file)) .ToArray(); // 遍历每个文档 foreach (string path in paths) -- Gitee