C# 搜索包含指定字符串的文件
我正在尝试编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想要使用 Windows Search SDK 。
You probably want to use the Windows Search SDK.
您可以在代码中与 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
查看此网站,了解如何使用 Windows Indexing API。它指的是 ASP.NET,但代码是用 C# 编写的。
片段如下:
Check out this website on using the Windows Indexing APIs. It refers to ASP.NET but the code is in C#.
Snippet below: