Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All

I am developing a website in which I have to provide a feature to serach
files which are at two given paths (say "D:\MyWeb1\" and "D:\MyWeb2").

I searched on net and found below code snippet
C#
void DirSearch(string sDir)
{
    try
    {
       foreach (string d in Directory.GetDirectories(sDir))
       {
        foreach (string f in Directory.GetFiles(d, txtFile.Text))
        {
           lstFilesFound.Items.Add(f);
        }
        DirSearch(d);
       }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}


And on one of the sites I came across code which created two threads to search in two folders and then combine the results for fast searching.
And then few question came up in my mind
1. Is it good to make threads in ASP.net website to search in files?
2. If the number of users making the search increases what will happen
to the performance of my site?
3. SHould I implement it using AJAX?
4. Is there any better way to search in a file for ASP.net website?

Please suggest a approach for searching a file in ASP.net website.

Thanks in advance :)

THE SK
Posted

Trying to answer a follow-up Question on further search optimization:

Further optimization is not trivial. It can be semantic-aware.

For example, you could partially index some of your search results using statistics on most popular requests. When you collect enough statistics you can start response from look-up in your index, which should use the principles of dictionary functionality but implemented based on disk memory. You service could use some of less busy time running a special optimization thread used to update the indexed data. This is just one idea.

I think the advanced optimization is a matter of very hard competition, so I would guess, some best optimization schema are kept in secret. It may need a good deal of research and perhaps experimental work.

—SA
 
Share this answer
 
v2
Comments
THE SK 8-Mar-11 6:39am    
Thanks again for the solution.
It could not had come to my mind.
This will do the trick. But will have to code more :(
My vote of 5 again
Sergey Alexandrovich Kryukov 8-Mar-11 11:41am    
Thank you for accepting my answer and good words.
Good luck, call again.
--SA
Espen Harlinn 8-Mar-11 11:55am    
My 5 - good idea
Sergey Alexandrovich Kryukov 8-Mar-11 12:41pm    
Thank you very much, Espen.
Not really good because it is very preliminary. I'm sure real specialists in search (like Google, Yahoo or Microsoft) have very advances approaches, not sure if anyone outside can use them...
--SA
1) You absolutely need threads, at least one separate thread per search. By if you have many cores or CPUs and not too many clients working in parallel, splitting a single search into sub-directories for more parallelism will improve final response time.

2) Not getting better, apparently. You CPU time is divided into more tasks. See the above item. One big warning: do not make the number of your threads growing proportionally to the number of user sessions. Make number of thread limited and predictable, otherwise too many thread will blow you server :-). Use thread pool if the number of threads is variable. In fact, you can even keep fixed number of threads, too. If some threads are not busy, they may stay at the blocking call to, say, EventWaitHandle and wait for next task.

3) Probably, but it depends. It depends on what you're doing. If the request is not very quick but response takes time (a typical case for search), you may easily go away with just a POST from a normal form.

4) See item (1). Also, I have a big warning about Directory.GetFiles: see Directory.Get.Files search pattern problem[^], the discussion and work-around. Do it thoroughly.

[EDIT] Please see my other Answer.

Good luck.
—SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 7-Mar-11 4:42am    
OP commented:

My vote of 5 :)
Thanks for your reply and I understood your points.
I will be using thread pool.

But still one question in my mind.
Is there any other way to improve search while searching for a string in files in addition to using threads.

Thanks in advance :)

THE SK
Sergey Alexandrovich Kryukov 8-Mar-11 4:33am    
After some thinking I decided to add a separate Answer to your follow-up Question. Please see what I think. If you really need to optimize it well, the problem is not easy.
--SA
Sergey Alexandrovich Kryukov 7-Mar-11 4:43am    
You're welcome.
Will you formally accept my answer?

As to other performance improvements, it needs some thinking.

--SA
Sergey Alexandrovich Kryukov 8-Mar-11 5:29am    
Thank you for accepting my answer...
(Ha! some activists keep down-voting... probably because I keep telling some idiots who are they, I don't know...)
--SA
Espen Harlinn 8-Mar-11 11:53am    
A reasoned reply, my 5

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900