Finding files - Directory.GetFiles not sufficient

K

KoenT

Hi,
I need a fast way to enumerate files in a directory hierarchy.
I have tried the Directory.GetFiles routine with search option set to
AllDirectories, because it seemed to be useful for this task, but it has some
limitations:
- whenever a subdirectory is encountered that is not accessible for some
reason, the search is abandoned with an exception, and the rest of the
directory structure is not searched anymore (I would like skip these and
continue the search on all other directores that are accessible)
- while not absoutely essential, it would be nice to have a way to get some
feedback, and GetFiles doesn't provide that (having the possibility to supply
some delegate that is called regularly with progress info for example)

So, I would like to hear about other ways to do a search in a directory
structure given some file pattern with woldcards. Could you please advise? Or
point me to some relevant info?
Thank you.
 
J

Jialiang Ge [MSFT]

Hello KoenT,

I agree with you that, Directory.GetFiles will be much more useful if it
has a callback to report its search progress, and if it has an overload
that specifies whether to ignore UnauthorizedAccessExceptions and just keep
on processing folders to which it does have access. As a long-term
resolution, I have submitted the suggestions to the .NET team. The
designers have confirmed they are very valuable and are tracking them in
their primary planning and scheduling database for consideration in a
future release. As a short-term workaround, we can consider the solution
mentioned by Peter, and below is an example code list I write for your
reference:

public class DirectoryHelper
{
public delegate void DirectoryEnumeratedHandler(string path);

public event DirectoryEnumeratedHandler DirectoryEnumerated;

public string[] GetAllAccessibleFiles(string path, string
searchPattern)
{
List<string> files = new List<string>();
GetAccessibleFilesRecursively(path, searchPattern, files);
return files.ToArray();
}

private void GetAccessibleFilesRecursively(string path, string
searchPattern, List<string> files)
{
try
{
// add the files directly under the path to the result
collection
files.AddRange(Directory.GetFiles(path, searchPattern));

if (DirectoryEnumerated != null)
DirectoryEnumerated(path);

string[] subDirectories = Directory.GetDirectories(path);
foreach (string subDir in subDirectories)
{
GetAccessibleFilesRecursively(subDir, searchPattern,
files);
}
}
catch { }
}
}

The class exposes an event "DirectoryEnumerated" to report the progress,
and it won't stop and throw UnauthorizedAccessExceptions when a sub
directory cannot be accessed.

To use the class:
DirectoryHelper dirHelper = new DirectoryHelper();
string[] files = dirHelper.GetAllAccessibleFiles("d:\\tests\\", "*");

If you have any other concerns or questions, feel free to let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello KoenT,

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of our suggestions? If you need further
assistance, feel free to let me know. I will be more than happy to be of
assistance.

Have a great day!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
K

KoenT

Hi "Jialiang Ge [MSFT]",
thanks for handling this issue and also for passing it on to the team for
future improvements.
I had already followed the advice of Peter and did the recursion myself.
Your solution is more elegant than mine (I didn't go down the delegate route
I had suggested myself).
Thanks for the follow-up.
 
J

Jialiang Ge [MSFT]

I am glad to be helpful to you, KoenT. :)
Thanks for the confirmation.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
J

junk_mail

Dear Sir Jialiang Ge,

Would You mind converting this class into vb.net? I'm newbie in vb.net
and I tried it myself but I faild. Thank You in advance.

Best Regards
sly

Hello KoenT,

I agree with you that, Directory.GetFiles will be much more useful if it
has a callback to report its search progress, and if it has an overload
that specifies whether to ignore UnauthorizedAccessExceptions and just keep
on processing folders to which it does have access. As a long-term
resolution, I have submitted the suggestions to the .NET team. The
designers have confirmed they are very valuable and are tracking them in
their primary planning and scheduling database for consideration in a
future release. As a short-term workaround, we can consider the solution
mentioned by Peter, and below is an example code list I write for your
reference:

public class DirectoryHelper
{
public delegate void DirectoryEnumeratedHandler(string path);

public event DirectoryEnumeratedHandler DirectoryEnumerated;

public string[] GetAllAccessibleFiles(string path, string
searchPattern)
{
List<string> files = new List<string>();
GetAccessibleFilesRecursively(path, searchPattern, files);
return files.ToArray();
}

private void GetAccessibleFilesRecursively(string path, string
searchPattern, List<string> files)
{
try
{
// add the files directly under the path to the result
collection
files.AddRange(Directory.GetFiles(path, searchPattern));

if (DirectoryEnumerated != null)
DirectoryEnumerated(path);

string[] subDirectories = Directory.GetDirectories(path);
foreach (string subDir in subDirectories)
{
GetAccessibleFilesRecursively(subDir, searchPattern,
files);
}
}
catch { }
}
}

The class exposes an event "DirectoryEnumerated" to report the progress,
and it won't stop and throw UnauthorizedAccessExceptions when a sub
directory cannot be accessed.

To use the class:
DirectoryHelper dirHelper = new DirectoryHelper();
string[] files = dirHelper.GetAllAccessibleFiles("d:\\tests\\", "*");

If you have any other concerns or questions, feel free to let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer tohttp://msdn.microsoft.com/subscriptions/managednewsgroups/default.asp...
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) athttp://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top