Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Like this -->

C#
private static <fileinfo> GetFiles(string Path)
{
   List<fileinfo> L = new List<fileinfo>();
   DirectoryInfo D = new DirectoryInfo(Path);
   FileInfo F;
   try
   {
      foreach (DirectoryInfo DD in D.GetDirectories())
      {
         foreach(FileInfo FF in DD.GetFiles("*.*"))
         {
            F = new FileInfo(FF.FullName);
            L.Add(F);
         }
         L.AddRange(GetFiles(DD.FullName));
      }
   }
   catch
   {

   }
}
Posted
Updated 29-Dec-14 5:30am
v2
Comments
Afzaal Ahmad Zeeshan 29-Dec-14 11:30am    
That doesn't make any sense. What did you expect this code to do?
BillWoodruff 29-Dec-14 11:40am    
Since you are "eating" the errors: how is it you have come to the conclusion that it is "failing" ?

Quote:
Why failing

if you really need to know that, remove the try catch block, then you can see the exception details or do as below
C#
try
{

  //your code 
}catch(Exception ex)
{
   // Log the ex.ToString() or show it as message 
}
 
Share this answer
 
Because there are files in some folders (and some folders themselves) which are protected from any access by "ordinary" access permission applications - only admins can access them. So when you try to read file information for protected files, it throws an access violation exception. As you would have seen if your try...catch block didn;t just "swallow" errors, but logged or reported them instead.
 
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