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

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

parent 9beec34f
...@@ -10,7 +10,7 @@ using System.Runtime.InteropServices; ...@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FTT-Tools")] [assembly: AssemblyProduct("FTT-Tools")]
[assembly: AssemblyCopyright("Copyright © 2024")] [assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]
...@@ -32,5 +32,5 @@ using System.Runtime.InteropServices; ...@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示: //通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.72.*")]
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.btnSearch = new System.Windows.Forms.Button(); this.btnSearch = new System.Windows.Forms.Button();
this.pnlTop = new System.Windows.Forms.Panel(); this.pnlTop = new System.Windows.Forms.Panel();
this.ckbSearchFileName = new System.Windows.Forms.CheckBox();
this.pnlSearchType = new System.Windows.Forms.Panel(); this.pnlSearchType = new System.Windows.Forms.Panel();
this.ckbAll = new System.Windows.Forms.CheckBox(); this.ckbAll = new System.Windows.Forms.CheckBox();
this.ckbCSharp = new System.Windows.Forms.CheckBox(); this.ckbCSharp = new System.Windows.Forms.CheckBox();
...@@ -100,7 +101,7 @@ ...@@ -100,7 +101,7 @@
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.txtSearchText.Location = new System.Drawing.Point(65, 44); this.txtSearchText.Location = new System.Drawing.Point(65, 44);
this.txtSearchText.Name = "txtSearchText"; 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; this.txtSearchText.TabIndex = 4;
// //
// btnSearch // btnSearch
...@@ -112,9 +113,11 @@ ...@@ -112,9 +113,11 @@
this.btnSearch.TabIndex = 7; this.btnSearch.TabIndex = 7;
this.btnSearch.Text = "搜索"; this.btnSearch.Text = "搜索";
this.btnSearch.UseVisualStyleBackColor = true; this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
// //
// pnlTop // pnlTop
// //
this.pnlTop.Controls.Add(this.ckbSearchFileName);
this.pnlTop.Controls.Add(this.pnlSearchType); this.pnlTop.Controls.Add(this.pnlSearchType);
this.pnlTop.Controls.Add(this.btnSearch); this.pnlTop.Controls.Add(this.btnSearch);
this.pnlTop.Controls.Add(this.label1); this.pnlTop.Controls.Add(this.label1);
...@@ -128,6 +131,17 @@ ...@@ -128,6 +131,17 @@
this.pnlTop.Size = new System.Drawing.Size(857, 74); this.pnlTop.Size = new System.Drawing.Size(857, 74);
this.pnlTop.TabIndex = 10; 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 // pnlSearchType
// //
this.pnlSearchType.Controls.Add(this.ckbAll); this.pnlSearchType.Controls.Add(this.ckbAll);
...@@ -282,6 +296,7 @@ ...@@ -282,6 +296,7 @@
private System.Windows.Forms.Panel pnlSearchType; private System.Windows.Forms.Panel pnlSearchType;
private System.Windows.Forms.Panel pnlBottom; private System.Windows.Forms.Panel pnlBottom;
private System.Windows.Forms.CheckBox ckbAll; private System.Windows.Forms.CheckBox ckbAll;
private System.Windows.Forms.CheckBox ckbSearchFileName;
} }
} }
...@@ -28,7 +28,6 @@ namespace WindowsSearch ...@@ -28,7 +28,6 @@ namespace WindowsSearch
public FrmSearch() public FrmSearch()
{ {
InitializeComponent(); InitializeComponent();
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
InitSearchType(); InitSearchType();
InitContextMenu(); InitContextMenu();
...@@ -45,6 +44,7 @@ namespace WindowsSearch ...@@ -45,6 +44,7 @@ namespace WindowsSearch
SetType(ckbSql, "*.sql", true, "sql脚本文件"); 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(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(ckbAll, "*.*", false, "忽而文件类型,搜所有文件");
SetType(ckbSearchFileName, null, false, "选中搜文件名,反之搜文件内容");
SetSearchType(); SetSearchType();
} }
...@@ -117,6 +117,10 @@ namespace WindowsSearch ...@@ -117,6 +117,10 @@ namespace WindowsSearch
/// <param name="e"></param> /// <param name="e"></param>
private void btnSelectPath_Click(object sender, EventArgs e) 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) if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
this.txtFilePath.Text = this.folderBrowserDialog1.SelectedPath; this.txtFilePath.Text = this.folderBrowserDialog1.SelectedPath;
} }
...@@ -145,10 +149,11 @@ namespace WindowsSearch ...@@ -145,10 +149,11 @@ namespace WindowsSearch
kvpSearchTypes = ParseSearchType(searchTypes); kvpSearchTypes = ParseSearchType(searchTypes);
btnSearch.Enabled = false; //搜索开始、禁用按钮 btnSearch.Enabled = false; //搜索开始、禁用按钮
var searchFileName = ckbSearchFileName.Checked;
await Task.Factory.StartNew(async () => await Task.Factory.StartNew(async () =>
{ {
await SearchFiles(strFilePath, kvpSearchTypes, strSearchText); await SearchFiles(strFilePath, kvpSearchTypes, strSearchText, searchFileName);
}); });
btnSearch.Enabled = true;//搜索结束、启用按钮 btnSearch.Enabled = true;//搜索结束、启用按钮
...@@ -183,8 +188,9 @@ namespace WindowsSearch ...@@ -183,8 +188,9 @@ namespace WindowsSearch
/// <param name="fullPath"></param> /// <param name="fullPath"></param>
/// <param name="kvpSearchTypes"></param> /// <param name="kvpSearchTypes"></param>
/// <param name="searchContent"></param> /// <param name="searchContent"></param>
/// <param name="searchFileName"></param>
/// <returns></returns> /// <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>(); List<string> result = new List<string>();
...@@ -198,9 +204,9 @@ namespace WindowsSearch ...@@ -198,9 +204,9 @@ namespace WindowsSearch
SetSearchStatus($"准备开始搜索,共{files.Count()}项"); SetSearchStatus($"准备开始搜索,共{files.Count()}项");
// stopWatch.Start(); // 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(); stopWatch.Stop();
...@@ -220,7 +226,7 @@ namespace WindowsSearch ...@@ -220,7 +226,7 @@ namespace WindowsSearch
/// <param name="kvpSearchRegex"></param> /// <param name="kvpSearchRegex"></param>
/// <param name="searchContent"></param> /// <param name="searchContent"></param>
/// <returns></returns> /// <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>(); List<string> result = new List<string>();
...@@ -231,8 +237,9 @@ namespace WindowsSearch ...@@ -231,8 +237,9 @@ namespace WindowsSearch
{ {
if (type.Value.IsMatch(filePath)) if (type.Value.IsMatch(filePath))
{ {
//文件内容查找,因为文件类型固定,则每个文件只会被读取并查找一次。 //文件内容\文件名查找,因为文件类型固定,则每个文件只会被读取并查找一次。
if (File.ReadAllText(filePath).Contains(searchContent)) if ((searchFileName && IsFileNameMatch(filePath, searchContent))
|| (!searchFileName && IsFileContentMatch(filePath, searchContent)))
{ {
result.Add(filePath); result.Add(filePath);
AddSearchFile(filePath); AddSearchFile(filePath);
...@@ -251,7 +258,7 @@ namespace WindowsSearch ...@@ -251,7 +258,7 @@ namespace WindowsSearch
/// <param name="kvpSearchRegex"></param> /// <param name="kvpSearchRegex"></param>
/// <param name="searchContent"></param> /// <param name="searchContent"></param>
/// <returns></returns> /// <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>(); List<string> result = new List<string>();
...@@ -261,7 +268,8 @@ namespace WindowsSearch ...@@ -261,7 +268,8 @@ namespace WindowsSearch
var tFiles = files.Where(f => type.Value.IsMatch(f)).ToList(); var tFiles = files.Where(f => type.Value.IsMatch(f)).ToList();
foreach (var filePath in tFiles) foreach (var filePath in tFiles)
{ {
if (File.ReadAllText(filePath).Contains(searchContent)) if ((onlyFileName && IsFileNameMatch(filePath, searchContent))
|| (!onlyFileName && IsFileContentMatch(filePath, searchContent)))
{ {
result.Add(filePath); result.Add(filePath);
AddSearchFile(filePath); AddSearchFile(filePath);
...@@ -273,6 +281,44 @@ namespace WindowsSearch ...@@ -273,6 +281,44 @@ namespace WindowsSearch
return result; 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 #endregion
private List<KeyValuePair<string, Regex>> ParseSearchType(List<string> fileTypes) private List<KeyValuePair<string, Regex>> ParseSearchType(List<string> fileTypes)
...@@ -327,7 +373,7 @@ namespace WindowsSearch ...@@ -327,7 +373,7 @@ namespace WindowsSearch
/// <param name="tips">提示词</param> /// <param name="tips">提示词</param>
private void SetType(CheckBox checkBox, string searchTypes, bool check = false, string tips = null) 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; checkBox.Checked = check;
if (tips != null) toolTip.SetToolTip(checkBox, tips); 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