类似Windows Search的文件搜索系统

16 篇文章 0 订阅

 

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows系统中,如果需要快速删除文件夹,可以使用命令行来执行删除操作。引用\[1\]提到,当文件夹中的文件非常多且总容量很大时,使用命令行删除文件夹会比通过鼠标选择文件夹再直接删除更快速。以下是两个常用的命令来快速删除文件夹和文件: 1. 使用rmdir命令:rmdir \[drive:\]path \[/S\] \[/Q\] - \[drive:\]path表示要删除的文件夹的路径。 - /S表示除目录本身外,还将删除指定目录下的所有子目录和文件。 - /Q表示安静模式,删除时不需要经过确认。 例如,要删除D:\temp\目录下的所有文件,可以使用以下命令: rmdir d:\temp\ /S /Q 2. 使用del命令:del \[drive:\]path \[/S\] \[/Q\] - \[drive:\]path表示要删除的文件夹或文件的路径。 - /S表示从所有子目录删除指定文件。 - /Q表示安静模式,删除时不需要经过确认。 例如,要删除D:\temp\目录下的所有文件,可以使用以下命令: del d:\temp\ /S /Q 推荐使用rmdir命令,因为在批量删除大量文件时比del命令更高效快速。如果需要批量删除当前路径下特定后缀名的文件,可以使用类似以下命令: del /a /f /s /q "*.jpg" "*.json" 其中,*为通配符,/a /f是强制删除所有属性的文件,/s表示删除子文件夹中的文件,/q表示无需确认直接删除。 希望以上信息对您有帮助。 #### 引用[.reference_title] - *1* [windows系统,删除文件慢,使用命令行快速删除大文件](https://blog.csdn.net/qq_37819292/article/details/122482520)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] - *2* [Windows使用命令行快速批量删除大量文件](https://blog.csdn.net/i_silence/article/details/117845626)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] - *3* [【✅windows奇怪的知识增加了✅】windows系统下快速删除海量小文件方法](https://blog.csdn.net/zcc1229936385/article/details/114366264)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值