Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
I want to search all Html(s) File in C:\\ .
I am using this Method :
C#
public List<string> SearchFilesRecursive(string directory, string pattern)
{
    Stack<string> stack = new Stack<string>();
    List<string> result = new List<string>();
    stack.Push(directory);
    List<string> current_dir = new List<string>();
    int extention_length = pattern.Length - 1;
    while (stack.Count > 0)
    {
        string dir = stack.Pop();
        try
        {
            current_dir.AddRange(Directory.GetFiles(dir, pattern));
            for (int i = 0; i < current_dir.Count; i++)
            {
                if (current_dir[i].LastIndexOf(".") != current_dir[i].Length - extention_length)
                {
                    current_dir.RemoveAt(i);
                    i--;
                }
            }
            result.AddRange(current_dir);


            current_dir.Clear();

            // Add all directories at this directory.
            foreach (string dn in Directory.GetDirectories(dir))
            {
                stack.Push(dn);
            }
        }
        catch
        {
            // Couldn't open the directory
        }
    }
    return result;
}

and then using this code for search :
C#
List<string> htmlobj = SearchFilesRecursive(@"C:\", "*.html");
for (int i = 0; i < htmlobj.Count; i++)
    listBox1.Items.Add(htmlobj[i]);

It works good and true but It is not Synchronous , It means when a .html file found program dont add this file Synchronously to listbox !
I know , I must add file to listbox during the method but where ?!

Can anybody explain me , how can I solve it ?! (where do I add it to listbox in this method ?!)

thank u .
Posted
v2
Comments
[no name] 14-Jun-14 6:47am    
Why are you doing this at all when GetFiles does all of this for you?
MohammadSina Karvandi 14-Jun-14 7:10am    
I think it is faster than other ... Am I right ?!
[no name] 14-Jun-14 7:17am    
No I highly doubt that.

Um.
Have you considered:
C#
List<string> files = Directory.GetFiles(@"C:\", "*.html", SearchOption.AllDirectories).ToList();
return files;
 
Share this answer
 
Comments
MohammadSina Karvandi 14-Jun-14 7:13am    
u are right but I could not solve it :
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
because after try and catch I cant continue method and it will start again .
LLLLGGGG 14-Jun-14 7:18am    
Go to your application manifest page and set the permissions for your applicatio to requireadministrator. You'll find the complete syntax commented in the app manifest.

this might help http://msdn.microsoft.com/en-us/library/bb756929.aspx
MohammadSina Karvandi 14-Jun-14 7:21am    
thank u for read. This code is for a company and I have not administrator access
You have to pass a reference to the listbox to your method then add items to the listbox. Remember that your method has to be executed in another thread and that, if you are using wpf, you have to use Dispatcher.Invoke in order to be able to edit the listbox. I.E. you have to use a delegate/lambda expression inside the dispatcher.invoke which calls the listbox.items.add method.
if you are using winform, use a backgroundworker.
if you don't want to pass your listbox inside the method, you can declare an observablecollection<string> and run your method with the async/await pattern (or in another thread, being conscious that your ObservableCollection has to be volatile and, in any case, not inside the method, but as a global variable).
Then, before calling your method, you have to set the itemssource of your listbox to your ObservableCollection. This method works only on wpf, the only method I know with winform is the backgroundworker.

hope it helps.

Jymmy097
 
Share this answer
 
You should define the return type of the SearchFilesRecursive method as IEnumerable<string>.
And use the yield[^] statement for returns (that will need some refactoring from you).

Then you would have to modify your search-code:
C#
foreach (string s in htmlobj) {
   listBox1.Items.Add(s);
}


But then, if your search-method runs on the main thread, there are little chances that you will see any update of the listbox. You have to execute the search on another thread.
BackgroundWorker Class[^] is a good and rather easy way to achieve that.

But, most important, I find your search algorithm too much complex with your list and stack; technically the only thing you need is a list. You should reconsider its design.
Moreover, your algorithm is not strictly recursive, as it never actually calls itself.

Left to quit the recursive way, your requirement could be simplified to:
C#
public IEnumerable<string> SearchFiles(string directory, string pattern) {
   foreach (string s in Directory.GetFiles(dir, pattern, SearchOption.AllDirectories)) {
      yield return s;
   }
}
 
Share this answer
 
v2

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