`

类似Windows Search的文件搜索系统

 
阅读更多

 

 

Introduction

WinSearchFile is a program that I developed and I usually use it to search files on my PC.

Sometimes the search engine integrated with Explorer doesn't work fine, especially when I try to find text contained into files, so I decided to build my own search program.

There are a lot of search programs available to install on your PC but this one, without indexing your data, is simple and fast enough to help you in your search.

Inside the application

WinSearchFile layout is simple and quite similar to the Explorer integrated search. It is possible to write a pattern search (wildcards admitted) and/or a text to search into file contents (you can also decide for a case sensitive search).

In the "look in" area, you have all the disks of your computer (network connection included). To obtain this list, I use the DiskCollection class developed by dmihailescu in his Get Logical Drives Information article.

Collapse Copy Code
///
 <
SUMMARY
>


///
 Add items for all floppy, 

///
 CD and hard drives.

///
 <
/
SUMMARY
>


private
 void
 LoadDisksComboBox()
{
    disksListBox.Items.Clear();

    //
 Try to use DiskCollection

    //
 retrieving drive information

    DiskCollection diskColl = new
 DiskCollection();
    if
 ( diskColl.Load() )
    {
        foreach
(DiskCollection.LogicalDriveInfo diskinfo in
 diskColl)
        {
            disksListBox.Items.Add(diskinfo.Name.ToString() + 
                                "
: "
+ diskinfo.Description );

        }
    }
    else

    {
        //
 otherwise build a drive list checking 

        //
 if a root directory exists

        for
 (char
 Ch = '
A'
; Ch <= '
Z'
; Ch++)
        {
            string
 Dir = Ch + @"
:\"
;

            if
 (Directory.Exists(Dir))
            {
                disksListBox.Items.Add( Ch+ @"
:"
 );
            }
        }
    }
}

Application shows alerts when you check a not-ready disk.

 

In the WinSearchFile application, I use threads to make the same search simultaneously on different targets; I use a thread for each target drive.

Collapse Copy Code
///
 <
SUMMARY
>


///
 Start search

///
 <
/
SUMMARY
>


///
 <
PARAM
 name="sender"
>
<
/
PARAM
>


///
 <
PARAM
 name="e"
>
<
/
PARAM
>


private
 void
 btnSearch_Click(object
 sender, System.EventArgs e)
{

    //
 empty thread list

    for
 (int
 i = thrdList.ItemCount()-1; i>=0; i--)
    {
        thrdList.RemoveItem(i);
    }

    //
 clear the file founded list

    listFileFounded.Items.Clear();
    ContainingFolder = "
"
;

    //
 get the search pattern

    //
 or use a default

    SearchPattern = txtSearchPattern.Text.Trim();
    if
 (SearchPattern.Length == 0
)
    {
        SearchPattern = "
*.*"
;
    }

    //
 get the text to search for

    SearchForText = txtSearchText.Text.Trim();

    //
 clear the Dirs arraylist

    Dirs.Clear();

    //
 check if each selected drive exists

    foreach
 (int
 Index in
 disksListBox.CheckedIndices)
    {
        //
 chek if drive is ready

        String
 Dir = disksListBox.Items[Index].ToString().Substring(0
,2
);
        Dir += @"
\"
;
        if
 (CheckExists(Dir))
        {
            Dirs.Add(Dir);
        }
    }

    //
 I use 1 thread for each dir to scan

    foreach
 (String
 Dir in
 Dirs)
    {
        Thread oT;
        string
 thrdName = "
Thread"
 + ((int
)(thrdList.ItemCount()+1)).ToString();
        FileSearch fs = new
 FileSearch(Dir, SearchPattern, 
                        SearchForText, CaseSensitive, this
, thrdName);
        oT = new
 Thread(new
 ThreadStart(fs.SearchDir));

        oT.Name = thrdName;

        SearchThread st = new
 SearchThread();
        st.searchdir = Dir;
        st.name = oT.Name;
        st.thrd = oT;
        st.state = SearchThreadState.ready;
        thrdList.AddItem(st);
        oT.Start();
    }
}

Data about searching threads is stored in a list, and during the search process, you can see how many threads are running/ready/cancelled.

 

Threads use the FileSearch class to do their work. To update controls or data structures on main threads, use delegate functions. I defined a delegate function for the AddListBoxItem method:

Collapse Copy Code
///
 <
SUMMARY
>


///
 Delegate for AddListBoxItem

///
 <
/
SUMMARY
>


public
 delegate
 void
 AddListBoxItemDelegate(String
 Text);

///
 <
SUMMARY
>


///
 Add a new item to the file founded list

///
 <
/
SUMMARY
>


///
 <
PARAM
 name="Text"
>
<
/
PARAM
>


public
 void
 AddListBoxItem(String
 Text)
{
    //
 I use Monitor to synchronize access 

    //
 to the file founded list

    Monitor.Enter(listFileFounded);

    listFileFounded.Items.Add(Text);

    Monitor.Exit(listFileFounded);
}

and one to update the thread state:

Collapse Copy Code
///
 <
SUMMARY
>


///
 Delegate for UpdateThreadStatus function

///
 <
/
SUMMARY
>


public
 delegate
 void
 UpdateThreadStatusDelegate(String
 thrdName, 
                                         SearchThreadState sts);

///
 <
SUMMARY
>


///
 Store the new state of a thread

///
 <
/
SUMMARY
>


///
 <
PARAM
 name="thrdName"
>
<
/
PARAM
>


///
 <
PARAM
 name="sts"
>
<
/
PARAM
>


public
 void
 UpdateThreadStatus(String
 thrdName, SearchThreadState sts)
{
    SearchThread st = thrdList.Item(thrdName);
    st.state = sts;
}

On clicking the "Stop search" button, all the running threads are cancelled using the Abort method.

Collapse Copy Code
///
 <
SUMMARY
>


///
 Stop searching

///
 <
/
SUMMARY
>


///
 <
PARAM
 name="sender"
>
<
/
PARAM
>


///
 <
PARAM
 name="e"
>
<
/
PARAM
>


private
 void
 btnStop_Click(object
 sender, System.EventArgs e)
{
    //
 some threads are running

    if
 (InProgress)
    {
        //
 Abort each searching thread in running status

        //
 and change its status to cancelled

        for
 (int
 i= 0
; i < thrdList.ItemCount(); i++)
        {
            if
 (((SearchThread)thrdList.Item(i)).state == 
                               SearchThreadState.running)
            {
                ((SearchThread)thrdList.Item(i)).state = 
                            SearchThreadState.cancelled;
                Thread tt;
                try

                {
                    tt = ((SearchThread)thrdList.Item(i)).thrd;
                    tt.Abort();
                }
                catch

                {
                }
            }
        }

    }
}

On double clicking on a result listbox item, WinSearchFile will open the corresponding containing folder.

 

To quick launch WinSearchFile, you can create a shortcut to it on your desktop and assign to this one a shortcut key.

 

Conclusion

I hope you enjoy this article.

New WinSearchFile version

The new WinSearchFile 2.0, built using Visual Studio 2005 and C# 2.0, contains the following new features:

  • Single Instance Application using code written by Eric Bergman-Terrell .
  • Search inside PDF files.
  • Regular expression searching.
  • Searching using IFilter.
  • Max directory visit depth.
  • File Creation Time or Last ACcess Time or Last Write Time searching
  • directory list to search into.
  • Save results button.

Here is a new screenshot:

 

About IFilter

User can decide to use installed IFilter to extract plaintext from files. To implement this interface I used 2 class developed by Dan Letecky .

The following code shows where I try to use IFilter to get plaintext:

Collapse Copy Code
public
 static
 bool
 FileContainsText(String
 FileName, 
          String
 SearchForText, bool
 CaseSensitive, 
          bool
 UseRegularExpression, bool
 UseIFilter)
{
    bool
 Result = (SearchForText.Length == 0
);

    if
 (!Result)
    {
      //
 try to use IFilter if you have checked

      //
 UseIFilter checkbox

     if
 (Parser.IsParseable(FileName) && UseIFilter)
     {
         string
 content = Parser.Parse(FileName);
         //
 if content length > 0

         //
 means that IFilter works and returns the file content

         //
 otherwise IFilter hadn't read the file content

         //
 i.e. IFilter seems no to be able to extract

         //
 text contained in dll or exe file

         if
 (content.Length > 0
)
         {
             Result = containsPattern(SearchForText, 
                      CaseSensitive, UseRegularExpression, content);
             return
 Result;
         }
      }
      //
 scan files to get plaintext

      //
 with my routines

      if
 (FileName.ToLower().EndsWith("
.pdf"
))
      {
         //
 search text in a pdf file

         Result = SearchInPdf(FileName, SearchForText, 
                  CaseSensitive, UseRegularExpression);
      }
      else

      {
         bool
 Error;
         String
 TextContent = GetFileContent(FileName, out
 Error);

         if
 (!Error)
         {
            Result = containsPattern(SearchForText, 
                     CaseSensitive, UseRegularExpression, TextContent);
         }
      }
    }

    return
 Result;
}

The following screenshot shows the about box where it's possible to get the list of installed IFilter. To get this list I used a class developed by vbAccelerator.com .

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Massimo Beatini


Member

Occupation: Web Developer
Location: Italy Italy

 

转自:http://www.codeproject.com/KB/files/winsearchfile.aspx

下载:http://download.csdn.net/source/1790306

分享到:
评论

相关推荐

    windows文件搜索工具Quick Search.rar

    Quick Search 是来自glarysoft公司的一款完全免费且小巧易于使用的极速windows文件搜索工具(知名的系统优化软件中的大师Glary Utilities Pro也是glarysoft公司的力作),界面极其简洁,因为该电脑文件快速查找工具...

    最新版windows elasticsearch-6.8.9.zip

    Windows版本的Elasticsearch为Windows用户提供了在微软操作系统上搭建和运行搜索集群的便利。最新版的Elasticsearch-6.8.9针对Windows平台进行了优化,确保了在该系统上的稳定性和性能。 **Elasticsearch核心概念:...

    最新版windows elasticsearch-8.6.1-windows-x86-64.zip

    在Windows平台上,Elasticsearch提供了方便的安装包,便于在Windows操作系统上搭建和管理搜索和数据分析环境。此压缩包“elasticsearch-8.6.1-windows-x86_64.zip”是专为64位Windows系统设计的最新版本8.6.1。 **...

    最新版windows elasticsearch-8.1.1-windows-x86_64.zip

    这个压缩包“elasticsearch-8.1.1-windows-x86_64.zip”包含了所有必要的文件,使得用户能够在Windows系统上安装和运行Elasticsearch。 1. **Elasticsearch核心概念**: - **节点(Node)**:Elasticsearch由多个...

    elasticsearch-7.14.2-windows-x86_64.zip

    这个"elasticsearch-7.14.2-windows-x86_64.zip"文件是Elasticsearch的7.14.2版本,专为Windows x86_64(64位)平台设计的安装包。 **Elasticsearch核心概念:** 1. **节点(Node)**:每个运行Elasticsearch的实例...

    elasticsearch-8.5.1-windows-x86-64.zip

    Elasticsearch是一个强大的开源搜索引擎,基于Java开发,其核心功能是全文检索,但同时也支持结构化数据、非结构化数据的检索。它以其高可扩展性、实时性、分布式处理能力以及灵活的数据模型赢得了广大用户的青睐。...

    windows最新版 elasticsearch-7.0.0-windows-x86_64.zip

    总之,"windows最新版 elasticsearch-7.0.0-windows-x86_64.zip"提供了强大的全文搜索和大数据分析功能,适用于Windows平台,通过简单的配置和API调用,可以构建高效、可扩展的数据存储和检索系统。同时,与Kibana、...

    elasticsearch-7.1.0-windows-x86_64.zip

    这个"elasticsearch-7.1.0-windows-x86_64.zip"文件是Elasticsearch的7.1.0版本,专为Windows 32位操作系统编译的安装包。 1. **Elasticsearch核心概念**: - **节点(Node)**:Elasticsearch集群由多个节点组成,...

    文件搜索神器

    在Windows系统中,传统的文件搜索功能依赖于文件的元数据,如文件名、创建日期、修改日期等。这种方式在面对大量文件时,可能会耗费较长的时间。而"文件搜索神器"则采用了更先进的技术,如即时索引和全文检索,使得...

    Everything-1.4.1系统搜索工具,快速找文件

    Ava Find、SearchMyFiles和Windows Search也提供了与 Everything 应用程序类似的功能。 什么是一切应用程序? Everything 是一个易于使用的搜索应用程序,可以帮助您查找存储在 Windows 计算机上的任何文件或...

    最新版windows elasticsearch-8.4.3-windows-x86_64.zip

    在Windows平台上,Elasticsearch提供了方便的安装包,如标题所示的"elasticsearch-8.4.3-windows-x86_64.zip",这是一个专为64位Windows系统设计的版本。 在深入探讨Elasticsearch的知识点之前,让我们先了解一下这...

    elasticsearch 7.10.1(windows)

    索引是存储数据的逻辑空间,类似于传统数据库中的表,但Elasticsearch支持多模态搜索,这意味着一个索引可以存储各种类型的数据。 在7.10.1版本中,Elasticsearch引入了一些重要的更新和改进。其中,性能优化是关键...

    最新版windows elasticsearch-7.14.1-windows-x86_64.zip

    总的来说,Elasticsearch 7.14.1在Windows上的部署和使用涉及到多个方面,包括系统环境的准备、配置文件的调整、安全设置的实施以及与其他工具的集成。正确地配置和使用Elasticsearch能够帮助用户高效管理和检索大量...

    windows下的dd和grep命令工具

    4. **多文件搜索**:`grep "search_string" file1.txt file2.txt`,可以在多个文件中同时进行搜索。 5. **忽略大小写**:`grep -i "search_string" file.txt`, `-i`选项让搜索不区分大小写。 在Windows环境下,...

    最新版windows elasticsearch-7.13.1-windows-x86_64.zip

    总的来说,"windows elasticsearch-7.13.1-windows-x86_64.zip"提供了完整的Windows平台解决方案,涵盖了Elasticsearch的安装、配置、使用和管理等多个方面,是构建高性能、高可用搜索和分析系统的理想选择。

    elasticsearch-7.6.1-windows-x86_64.zip

    这个压缩包“elasticsearch-7.6.1-windows-x86_64.zip”包含了在Windows操作系统上运行Elasticsearch所需的所有组件,特别是针对64位系统进行了优化。 1. **Elasticsearch核心概念**: - **节点(Node)**:Elastic...

    文件夹快速搜索工具ASearch

    **ASearch:高效文件搜索工具** ASearch是由安天公司开发的一款强大且高效的文件夹快速搜索工具。在日常工作中,我们经常会遇到需要快速查找特定文件的情况,而Windows自带的搜索功能可能无法满足这种即时需求,...

    VC 快速搜索指定目录的某个文件

    这个函数用于开始一个文件搜索,它接收两个参数:一个是通配符路径(例如,"*.txt"),另一个是`WIN32_FIND_DATA`结构体的指针,用于存储搜索结果。如果找到匹配的文件,`FindFirstFile`将返回一个搜索句柄,否则...

    最新版windows elasticsearch-6.8.10.zip

    Elasticsearch是一个开源的全文搜索...通过理解并应用这些知识点,你可以在Windows环境中有效地使用Elasticsearch 6.8.10进行数据存储、搜索和分析。在实际应用中,不断学习和优化,以充分发挥Elasticsearch的潜力。

    Win8系统自带搜索功能如何关闭以便使用第三方服务.docx

    例如,如果你经常需要在大量文件中寻找特定内容,可以选择安装支持全文检索的第三方文件搜索工具,如Everything或Listary。这些工具往往提供更快的搜索速度和更精确的匹配结果。此外,如果你希望在浏览器中获得更好...

Global site tag (gtag.js) - Google Analytics