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 (jialge@online.microsoft.com, 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:
msdnmg@microsoft.com.
==================================================
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.
Jialiang Ge [MSFT] - 23 Apr 2008 08:26 GMT
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 (jialge@online.microsoft.com, 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:
msdnmg@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
KoenT - 30 Apr 2008 11:00 GMT
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.
Jialiang Ge [MSFT] - 30 Apr 2008 12:23 GMT
I am glad to be helpful to you, KoenT. :-)
Thanks for the confirmation.
Regards,
Jialiang Ge (jialge@online.microsoft.com, 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:
msdnmg@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
junk_mail@wp.pl - 27 May 2008 20:28 GMT
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
On 21 Kwi, 08:10, jia...@online.microsoft.com (Jialiang Ge [MSFT])
wrote:
> Hello KoenT,
>
[quoted text clipped - 82 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.