Reputation: 2251
my code
DirectoryInfo[] dirstring = dir.GetDirectories(SearchTextbox.Text, System.IO.SearchOption.AllDirectories);
FileInfo[] fileinfo = dir.GetFiles(SearchTextbox.Text, System.IO.SearchOption.AllDirectories);
dir=@"D:\";
have a error with expection UnauthorizedAccessException
How can I catch it and continue search in others folder and files.
Upvotes: 4
Views: 2166
Reputation: 941257
You cannot make this work when you start from the root directory of the drive. You are inevitably going to run into a directory for which the user doesn't have list access rights. The "System Volume Information" directory for example, not even an admin account has access to this directory.
You'll have to give up on the SearchOption.AllDirectories option and recurse the directory tree one directory at a time so you can catch access exceptions. See this question for sample code.
Upvotes: 7