Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Recursive file search
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 30-Sep-12 5:55am
frubsen107
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 1-Oct-12 2:01am
   
Excluding/Ignoring it should be ok. Providing access to it is not the right solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
frubsen at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2014
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