Click here to Skip to main content
15,867,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to make a program where you specify a root directory and it will recursively search for all .mp4 files from that folder on.

My problem is that once it finds a folder it does not have access to, it just stops searching and doesn't display all the results.

My code is as follows.

void DirSearch(string sDir)
{

    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {

                     foreach (string f in Directory.GetFiles(d, "*.mp4"))
            {
                lstFilesFound.Items.Add(f);
            }
            DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);

    }
}


Where do I need to put my catch line?

Thanks
Posted

My problem is that once it finds a folder it does not have access to, it just stops searching and doesn't display all the results.
So? The solution is not in the codes. You need to make sure that the folders have the access permission set such that you can browse through it. Until unless, security privileges are properly set, you cannot access them and will have unauthorized exception.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Sep-12 14:39pm    
Sorry, this time I cannot agree, because I know that very often on needs to find some data in "all accessible files". Quite a legitimate problem. Please see my answer.
--SA
frubsen 1-Oct-12 1:13am    
Well the problem is, when I select the Root of a drive, it tries to access the System Volume Information folder which I don't need it to. So either I need to excluide that from the search or have it keep searching when it comes across a folder it can't access.
Sandeep Mewara 1-Oct-12 2:01am    
Excluding/Ignoring it should be ok. Providing access to it is not the right solution.
Unfortunately, this is one of the rare cases where you need to make a fine-grain try-catch block and block the propagation if exception (but not re-throwing). In this approach, you need to put your try-catch in the minimal context containing the line with the operation with each file system element. This way, the search will continue. I'll lean the problem of writing code for your home exercise, this is very simple.

But also, you can try recursive GetFiles, the difference one:
http://msdn.microsoft.com/en-us/library/ms143316.aspx[^];
using System.SearchOption.AllDirectories:
http://msdn.microsoft.com/en-us/library/ms143448.aspx[^].

[EDIT]

If you really want to access all files, and make most of them accessible, you would also need to access elevated privileges in the manifest. The user would need to have admin access and confirm the request.

—SA
 
Share this answer
 
v2
Comments
frubsen 1-Oct-12 1:14am    
thanks, i will take a look at those links

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