This question has already been answered:

I do this search in folders

{ treeView1.Nodes.Clear(); try { foreach (string find in Directory.GetFiles(catalog, fileName, SearchOption.AllDirectories)) { fileCount++; label6.Text = "Файлов обработано: " + fileCount; label4.Text = "Обработка: " + find; treeView1.Nodes.Add("" + find); } } catch { treeView1.Nodes.Add("123"); } fileCount = 0; } 

Naturally, in many cases, Exception climbs after which the folder search stops. How to continue the search after the eksepshen, or how to make sure that the search is not conducted in the system directories causing the exception?

Reported as a duplicate by PashaPash member c # 12 May '18 at 14:44 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • The simplest is try / catch inside foreach. - EvgeniyZ
  • Yes, I ran across this info on a foreign forum and tried, but didn’t roll something ...) In this case, an unhandled message comes out: An unhandled exception like "System.UnauthorizedAccessException" in mscorlib.dll Additional information: Access denied along the way "E: \ $ RECYCLE.BIN \ S-1-5-21-1046610997-2961191853-1124677420-1000". - Oleg It doesn’t matter

1 answer 1

  1. try/catch inside foreach .
  2. Do not use the SearchOption.AllDirectories flag, but use SearchOption.TopDirectoryOnly and SearchOption.TopDirectoryOnly recursively.