Tôi đang cố gắng hiển thị danh sách tất cả các tệp được tìm thấy trong thư mục đã chọn (và tùy chọn bất kỳ thư mục con nào). Vấn đề tôi gặp phải là khi phương thức GetFiles() đi qua một thư mục mà nó không thể truy cập, nó ném một ngoại lệ và quá trình dừng lại.Bỏ qua các thư mục/tệp khi Directory.GetFiles() bị từ chối truy cập
Làm cách nào để bỏ qua ngoại lệ này (và bỏ qua thư mục/tệp được bảo vệ) và tiếp tục thêm tệp có thể truy cập vào danh sách?
try
{
if (cbSubFolders.Checked == false)
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
foreach (string fileName in files)
ProcessFile(fileName);
}
else
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
foreach (string fileName in files)
ProcessFile(fileName);
}
lblNumberOfFilesDisplay.Enabled = true;
}
catch (UnauthorizedAccessException) { }
finally {}
khác (đánh dấu là trùng lặp) câu hỏi - http://stackoverflow.com/questions/1393178/unauthorizedaccessexception-cannot-resolve-directory-getfiles-failure?noredirect=1 –