Click here to Skip to main content
12,445,482 members (60,382 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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 :

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 30-Jan-12 19:46pm
Updated 1-Mar-16 5:48am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Comments
Lakamraju Raghuram 31-Jan-12 2:15am
   
I am unaware of this. Thanks
+5 or +++5 [ if I can (:- ]
SAKryukov 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 .Smile | :)

jkchan
http://cgmath.blogspot.com
  Permalink  
Comments
Member 8534726 31-Jan-12 1:54am
   
My search files are many... like .dll, .exe, .msi, .zip etc
SAKryukov 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
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
SAKryukov 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
SAKryukov 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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."
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 1 Apr 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100