从VB6时代开始,当我通过系统上的目录递归时,我能够以相对适当的方式使用"on next error next".如果我的"foreach"循环遇到Permission Denied或Access Denied错误,我所要做的就是调用"resume next"语句.
然而,在C#中,这不存在,我理解为什么.然而,令人难以理解的是在C#中弄清楚这是如何实现的.
我试图通过我的硬盘上的目录递归并填充TreeView控件.
private void PopulateTree(string dir, TreeNode node)
{
try
{
// get the information of the directory
DirectoryInfo directory = new DirectoryInfo(dir);
// loop through each subdirectory
foreach (DirectoryInfo d in directory.GetDirectories("*", SearchOption.AllDirectories))
{
// create a new node
TreeNode t = new TreeNode(d.Name);
// populate the new node recursively
PopulateTree(d.FullName, t);
node.Nodes.Add(t); // add the node to the "master" node
}
// lastly, loop through each file in the directory, and …Run Code Online (Sandbox Code Playgroud) c# ×1