C# 搜索包含指定字符串的文件

发布于 2024-10-25 09:54:42 字数 399 浏览 6 评论 0原文

我正在尝试编写一个 WinForms 应用程序,它允许搜索包含文本框中写入的字符串的文件(按 WIN+F 你就明白了;)) 在此应用程序中,有一个文件和目录列表,必须搜索该字符串,

这些文件主要是 .doc 和 .xls 我认为,在 doc 中搜索可能更容易,但在 Excel 文件中,单元格可以有不同的编码 我试图通过在 Notepad++ 中打开它们来“读取”这些文件,我发现只有拉丁字符的单元格很容易找到,但是那些带有波兰字符的单元格

在内置搜索的窗口中有一个两字节编码,有没问题,它能够告诉,在一些测试文件中,我的字符串包含波兰语特殊字符,

所以我的问题基本上是,是否有一种方法可以为我的应用程序使用这个 Windows 内置搜索引擎(正如我所写) ,我只需要查找文件名),或者也许您还有其他想法吗,我如何编写简单的多文件搜索?

I am trying to write a WinForms app, that allows searching for a files, that contain a string written in textbox ( press WIN+F and you understand ;) )
in this app, there is a list of files and directories, that must be searched for this string

those files are mostly .doc and .xls
i think, that searching in doc may be easier, but in Excel files, cells can have different encodings
i've tried to "read" those files, by opening them in Notepad++, and i found that cells with only latin characters were easy to find, but those with polish characters, had a two byte encoding

in windows built in search, there was no problem, it was able to tell, that in some test files there is my string that contains polish special characters

so my question basically is, if there is a method to use this windows built-in search engine for my app ( as i wrote, i need to find only filenames ), or maybe you have any other idea, how i can write a simple multi-file search ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

摇划花蜜的午后 2024-11-01 09:54:42

您可能想要使用 Windows Search SDK

You probably want to use the Windows Search SDK.

醉南桥 2024-11-01 09:54:42

您可以在代码中与 Windows 搜索进行交互,使其能够完成搜索多种文件类型的繁重工作。有关详细信息,请参阅此 MSDN 文章:

http:// /msdn.microsoft.com/en-us/library/bb266517%28v=VS.85%29.aspx

You can interact with windows search in your code, allowing it to do the heavy lifting of searching multiple file types. See this MSDN article for more information:

http://msdn.microsoft.com/en-us/library/bb266517%28v=VS.85%29.aspx

花之痕靓丽 2024-11-01 09:54:42

查看此网站,了解如何使用 Windows Indexing API。它指的是 ASP.NET,但代码是用 C# 编写的。

片段如下:

            string  QueryText = "asp alliance"; //The search string
            string CatalogName = "searchcatalog"; //The name of your Index Server catalog
            int NumberOfSearchResults = 0;  
            DataSet SearchResults = new DataSet();  

            //Prevent SQL injection attacks - further security measures are recommended  
            QueryText = QueryText.Replace("'", "''");  

            //Build the search query  
            string SQL = "SELECT doctitle, vpath, Path, Write, Size, Rank ";  
            SQL += "FROM \"" + CatalogName + "\"..SCOPE() ";  
            SQL += "WHERE";  
            SQL += " CONTAINS(Contents, '" + QueryText + "') ";  
            SQL += "AND NOT CONTAINS(Path, '\"_vti_\"') ";  
            SQL += "AND NOT CONTAINS(FileName, '\".ascx\" OR \".config\" OR \".css\"') ";  
            SQL += "ORDER BY Rank DESC";  

            //Connect to Index Server and perform search query  
            try   
            {  
                OleDbConnection IndexServerConnection = new OleDbConnection("Provider=msidxs;");  
                OleDbCommand dbCommand = new OleDbCommand(SQL, IndexServerConnection);  

                OleDbDataAdapter IndexServerDataAdapter = new OleDbDataAdapter();  
                IndexServerDataAdapter.SelectCommand = dbCommand;  

                IndexServerDataAdapter.Fill(SearchResults);  
                NumberOfSearchResults = SearchResults.Tables[0].Rows.Count;  
            }  
            catch (Exception ExceptionObject)  
            {  
                //Query failed so display an error message  
                NumberOfSearchResults = 0;  
                LabelNumberOfResults.Text = "Problem with retrieving search results due to: " + ExceptionObject.Message;  
                DataGridSearchResults.Visible = false;  

            }  

Check out this website on using the Windows Indexing APIs. It refers to ASP.NET but the code is in C#.

Snippet below:

            string  QueryText = "asp alliance"; //The search string
            string CatalogName = "searchcatalog"; //The name of your Index Server catalog
            int NumberOfSearchResults = 0;  
            DataSet SearchResults = new DataSet();  

            //Prevent SQL injection attacks - further security measures are recommended  
            QueryText = QueryText.Replace("'", "''");  

            //Build the search query  
            string SQL = "SELECT doctitle, vpath, Path, Write, Size, Rank ";  
            SQL += "FROM \"" + CatalogName + "\"..SCOPE() ";  
            SQL += "WHERE";  
            SQL += " CONTAINS(Contents, '" + QueryText + "') ";  
            SQL += "AND NOT CONTAINS(Path, '\"_vti_\"') ";  
            SQL += "AND NOT CONTAINS(FileName, '\".ascx\" OR \".config\" OR \".css\"') ";  
            SQL += "ORDER BY Rank DESC";  

            //Connect to Index Server and perform search query  
            try   
            {  
                OleDbConnection IndexServerConnection = new OleDbConnection("Provider=msidxs;");  
                OleDbCommand dbCommand = new OleDbCommand(SQL, IndexServerConnection);  

                OleDbDataAdapter IndexServerDataAdapter = new OleDbDataAdapter();  
                IndexServerDataAdapter.SelectCommand = dbCommand;  

                IndexServerDataAdapter.Fill(SearchResults);  
                NumberOfSearchResults = SearchResults.Tables[0].Rows.Count;  
            }  
            catch (Exception ExceptionObject)  
            {  
                //Query failed so display an error message  
                NumberOfSearchResults = 0;  
                LabelNumberOfResults.Text = "Problem with retrieving search results due to: " + ExceptionObject.Message;  
                DataGridSearchResults.Visible = false;  

            }  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文