Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I need to find a file within a directory which has lot many folder within it.

I have written a code which does it, since the directory has more than 1000 folders in it, it takes more time to fetch the result.

My code :

C#
protected void Button1_Click(object sender, System.EventArgs e)
   {
       string search = TextBox1.Text;
       string[] files = Directory.GetFiles(@"\\directory1\storage\folder1\", "*.*", SearchOption.AllDirectories);
       int Flag = 0;
       string dir = @"\\directory1\storage\folder1\";
       string[] files1;
       int numFiles;
       files1 = Directory.GetFiles(dir);
       numFiles = files.Length;
       Response.Write("Files searched : " + numFiles + "<br>");
       for (int i = 0; i < numFiles; i++)
       {


           string file = files[i].ToString();

           int file1 = file.IndexOf(search,StringComparison.CurrentCultureIgnoreCase);
           if (file1 != -1)
           {
               Response.Write("<br>" + file);
               //Response.Write("<div style='position:absolute;top:" + file + "px;left:" +  "px'>"+file! + "</div>");
               Flag = 1;

           }




       }
       if (Flag == 0)
       {
           Response.Write("No Matches Found");
       }
   }



Kindly suggest me, possibly post the code.
Posted
Updated 1-Mar-16 5:48am
Comments
Member 13847218 10-Jul-18 13:35pm    
Response error
string search = TextBox1.Text; error
Member 13847218 12-Jul-18 0:39am    
How To Search Values From files(my pc) And Set It Into TextBox In C# encode,decode pls help

The method Directory.GetFiles has one nasty caveat which is not very well know.

Please see this discussion (and my answer, too); you will find exact fixed solution: Directory.Get.Files search pattern problem[^].

—SA
 
Share this answer
 
Comments
Lakamraju Raghuram 31-Jan-12 2:15am    
I am unaware of this. Thanks
+5 or +++5 [ if I can (:- ]
Sergey Alexandrovich Kryukov 31-Jan-12 2:42am    
You are welcome, and thank you. Interesting flaw in Win32 API, isn't it?
All credit goes to Nishant Sivakumar who explained this issue to me.
--SA
I think you can provide search string in Directory.GetFiles itself. like ( Myfile*.txt or MyFile.txt ). Don't know whether this will improve the speed. Just try .:)

jkchan
http://cgmath.blogspot.com
 
Share this answer
 
Comments
Member 8534726 31-Jan-12 1:54am    
My search files are many... like .dll, .exe, .msi, .zip etc
Sergey Alexandrovich Kryukov 31-Jan-12 2:03am    
Not exactly. There is one caveat and a fix you can find from my solution -- please see. I voted 4.
--SA
As per your requirement, i understood you want to search a specific file in the folder including sub folders.

you can you simple like this..
string[] files = Directory.GetFiles("c:\\", "*.exe", SearchOption.AllDirectories);


Hope this may helps you

--RA
 
Share this answer
 
v2
Comments
Rajesh Anuhya 31-Jan-12 2:03am    
Answer edited: to search multiple files.
--RA
Member 10193248 29-Aug-13 16:02pm    
there is a issue about administrator permission when running the code. can u solve it? mail me on pinakichowdhurymca@gmail.com
Sergey Alexandrovich Kryukov 31-Jan-12 2:03am    
Not exactly. There is one caveat and a fix you can find from my solution -- please see. I voted 4.
--SA
Member 8534726 31-Jan-12 2:04am    
How do I pass the value of text box into this path ?

this search should depend on what user gives in the text box
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-12 2:04am    
This solution has one caveat and a fix you can find from my solution -- please see. I voted 4.
--SA
Check out Directory.EnumerateFiles Method (String, String, SearchOption)[^]

"The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient."
 
Share this answer
 

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