Commit 44ef276f authored by 罗显健's avatar 罗显健

【编号:22639】搜索功能增加仅搜文件名选项

parent 9beec34f
......@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FTT-Tools")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
......@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.72.*")]
......@@ -37,6 +37,7 @@
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.btnSearch = new System.Windows.Forms.Button();
this.pnlTop = new System.Windows.Forms.Panel();
this.ckbSearchFileName = new System.Windows.Forms.CheckBox();
this.pnlSearchType = new System.Windows.Forms.Panel();
this.ckbAll = new System.Windows.Forms.CheckBox();
this.ckbCSharp = new System.Windows.Forms.CheckBox();
......@@ -100,7 +101,7 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.txtSearchText.Location = new System.Drawing.Point(65, 44);
this.txtSearchText.Name = "txtSearchText";
this.txtSearchText.Size = new System.Drawing.Size(396, 21);
this.txtSearchText.Size = new System.Drawing.Size(324, 21);
this.txtSearchText.TabIndex = 4;
//
// btnSearch
......@@ -112,9 +113,11 @@
this.btnSearch.TabIndex = 7;
this.btnSearch.Text = "搜索";
this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// pnlTop
//
this.pnlTop.Controls.Add(this.ckbSearchFileName);
this.pnlTop.Controls.Add(this.pnlSearchType);
this.pnlTop.Controls.Add(this.btnSearch);
this.pnlTop.Controls.Add(this.label1);
......@@ -128,6 +131,17 @@
this.pnlTop.Size = new System.Drawing.Size(857, 74);
this.pnlTop.TabIndex = 10;
//
// ckbSearchFileName
//
this.ckbSearchFileName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ckbSearchFileName.AutoSize = true;
this.ckbSearchFileName.Location = new System.Drawing.Point(398, 46);
this.ckbSearchFileName.Name = "ckbSearchFileName";
this.ckbSearchFileName.Size = new System.Drawing.Size(60, 16);
this.ckbSearchFileName.TabIndex = 12;
this.ckbSearchFileName.Text = "文件名";
this.ckbSearchFileName.UseVisualStyleBackColor = true;
//
// pnlSearchType
//
this.pnlSearchType.Controls.Add(this.ckbAll);
......@@ -282,6 +296,7 @@
private System.Windows.Forms.Panel pnlSearchType;
private System.Windows.Forms.Panel pnlBottom;
private System.Windows.Forms.CheckBox ckbAll;
private System.Windows.Forms.CheckBox ckbSearchFileName;
}
}
......@@ -9,7 +9,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsSearch
{
{
/*
* 1.使用异步搜索,在界面不假死的前提下,尽可能快的找到结果
* 2.搜索过程进度表示、搜索目录总文件数、当前搜了多少文件,动态实时反馈。 Ui操作性能影响很大、甚至非常大。
......@@ -28,7 +28,6 @@ namespace WindowsSearch
public FrmSearch()
{
InitializeComponent();
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
InitSearchType();
InitContextMenu();
......@@ -45,6 +44,7 @@ namespace WindowsSearch
SetType(ckbSql, "*.sql", true, "sql脚本文件");
SetType(ckbCSharp, "*.cs;*.vb;*.resx;*.xsd;*.wsdl;*.htm;*.html;*.aspx;*.ascx;*.asmx;*.svc;*.asax;*.config;*.asp;*.asa;*.cshtml;*.vbhtml;*.razor;*.css;*.xml", false, "常规程序文件类型");
SetType(ckbAll, "*.*", false, "忽而文件类型,搜所有文件");
SetType(ckbSearchFileName, null, false, "选中搜文件名,反之搜文件内容");
SetSearchType();
}
......@@ -117,6 +117,10 @@ namespace WindowsSearch
/// <param name="e"></param>
private void btnSelectPath_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtFilePath.Text) && Directory.Exists(txtFilePath.Text))
{
folderBrowserDialog1.SelectedPath = txtFilePath.Text;
}
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
this.txtFilePath.Text = this.folderBrowserDialog1.SelectedPath;
}
......@@ -145,10 +149,11 @@ namespace WindowsSearch
kvpSearchTypes = ParseSearchType(searchTypes);
btnSearch.Enabled = false; //搜索开始、禁用按钮
var searchFileName = ckbSearchFileName.Checked;
await Task.Factory.StartNew(async () =>
{
await SearchFiles(strFilePath, kvpSearchTypes, strSearchText);
await SearchFiles(strFilePath, kvpSearchTypes, strSearchText, searchFileName);
});
btnSearch.Enabled = true;//搜索结束、启用按钮
......@@ -183,8 +188,9 @@ namespace WindowsSearch
/// <param name="fullPath"></param>
/// <param name="kvpSearchTypes"></param>
/// <param name="searchContent"></param>
/// <param name="searchFileName"></param>
/// <returns></returns>
private Task<List<string>> SearchFiles(string fullPath, List<KeyValuePair<string, Regex>> kvpSearchTypes, string searchContent)
private Task<List<string>> SearchFiles(string fullPath, List<KeyValuePair<string, Regex>> kvpSearchTypes, string searchContent, bool searchFileName = false)
{
List<string> result = new List<string>();
......@@ -198,9 +204,9 @@ namespace WindowsSearch
SetSearchStatus($"准备开始搜索,共{files.Count()}项");
// stopWatch.Start();
// SearchFiles_Type1(files, kvpSearchTypes, searchContent);
// SearchFiles_Type1(files, kvpSearchTypes, searchContent, onlyFileName);
result = SearchFiles_Type2(files, kvpSearchTypes, searchContent);
result = SearchFiles_Type2(files, kvpSearchTypes, searchContent, searchFileName);
stopWatch.Stop();
......@@ -220,7 +226,7 @@ namespace WindowsSearch
/// <param name="kvpSearchRegex"></param>
/// <param name="searchContent"></param>
/// <returns></returns>
private List<string> SearchFiles_Type2(List<string> files, List<KeyValuePair<string, Regex>> kvpSearchRegex, string searchContent)
private List<string> SearchFiles_Type2(List<string> files, List<KeyValuePair<string, Regex>> kvpSearchRegex, string searchContent, bool searchFileName = false)
{
List<string> result = new List<string>();
......@@ -231,8 +237,9 @@ namespace WindowsSearch
{
if (type.Value.IsMatch(filePath))
{
//文件内容查找,因为文件类型固定,则每个文件只会被读取并查找一次。
if (File.ReadAllText(filePath).Contains(searchContent))
//文件内容\文件名查找,因为文件类型固定,则每个文件只会被读取并查找一次。
if ((searchFileName && IsFileNameMatch(filePath, searchContent))
|| (!searchFileName && IsFileContentMatch(filePath, searchContent)))
{
result.Add(filePath);
AddSearchFile(filePath);
......@@ -251,7 +258,7 @@ namespace WindowsSearch
/// <param name="kvpSearchRegex"></param>
/// <param name="searchContent"></param>
/// <returns></returns>
private List<string> SearchFiles_Type1(List<string> files, List<KeyValuePair<string, Regex>> kvpSearchRegex, string searchContent)
private List<string> SearchFiles_Type1(List<string> files, List<KeyValuePair<string, Regex>> kvpSearchRegex, string searchContent, bool onlyFileName = false)
{
List<string> result = new List<string>();
......@@ -261,7 +268,8 @@ namespace WindowsSearch
var tFiles = files.Where(f => type.Value.IsMatch(f)).ToList();
foreach (var filePath in tFiles)
{
if (File.ReadAllText(filePath).Contains(searchContent))
if ((onlyFileName && IsFileNameMatch(filePath, searchContent))
|| (!onlyFileName && IsFileContentMatch(filePath, searchContent)))
{
result.Add(filePath);
AddSearchFile(filePath);
......@@ -273,6 +281,44 @@ namespace WindowsSearch
return result;
}
/// <summary>
/// 文件名是否匹配
/// </summary>
/// <param name="filePath"></param>
/// <param name="searchContent"></param>
/// <returns></returns>
private bool IsFileNameMatch(string filePath, string searchContent)
{
// 只匹配文件名
string fileName = Path.GetFileName(filePath);
return fileName.IndexOf(searchContent, StringComparison.InvariantCultureIgnoreCase) >= 0;
}
/// <summary>
/// 文件内容是否匹配
/// </summary>
/// <param name="filePath"></param>
/// <param name="searchContent"></param>
/// <returns></returns>
private bool IsFileContentMatch(string filePath, string searchContent)
{
try
{
// 读取文件内容并检查是否包含搜索内容
string fileContent = File.ReadAllText(filePath);
return fileContent.IndexOf(searchContent, StringComparison.InvariantCultureIgnoreCase) >= 0;
}
catch (Exception ex)
{
// 处理文件读取异常(如权限问题等)
Console.WriteLine($"Error reading file {filePath}: {ex.Message}");
return false;
}
}
#endregion
private List<KeyValuePair<string, Regex>> ParseSearchType(List<string> fileTypes)
......@@ -327,7 +373,7 @@ namespace WindowsSearch
/// <param name="tips">提示词</param>
private void SetType(CheckBox checkBox, string searchTypes, bool check = false, string tips = null)
{
checkBox.Text = searchTypes;
if (!string.IsNullOrEmpty(searchTypes)) checkBox.Text = searchTypes;
checkBox.Checked = check;
if (tips != null) toolTip.SetToolTip(checkBox, tips);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment