递归调用会损坏我的数据吗?

发布于 2024-09-12 01:33:04 字数 728 浏览 1 评论 0原文

我调用此函数来加载带有磁盘上目录列表的 TreeView。

private void LoadDirectories(string currentPath, TreeNodeCollection nodes)
{
    DirectoryInfo directoryInfo = new DirectoryInfo(currentPath);
    DirectoryInfo[] directories = directoryInfo.GetDirectories();

    foreach (DirectoryInfo dir in directories)
    {
        if ((dir.Attributes & FileAttributes.System) != FileAttributes.System &&
            (dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
        {
            TreeNode newNode = nodes.Add(dir.Name);
            LoadDirectories(dir.FullName, newNode.Nodes);
        }
    }
}

如果我注释掉递归调用,我将获得树中的所有子目录。如果我不这样做,我就不会。有些目录丢失了。我不确定发生了什么事。

帮助?

斯科特

I'm calling this function to load a TreeView with a list of the directories on the disk.

private void LoadDirectories(string currentPath, TreeNodeCollection nodes)
{
    DirectoryInfo directoryInfo = new DirectoryInfo(currentPath);
    DirectoryInfo[] directories = directoryInfo.GetDirectories();

    foreach (DirectoryInfo dir in directories)
    {
        if ((dir.Attributes & FileAttributes.System) != FileAttributes.System &&
            (dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
        {
            TreeNode newNode = nodes.Add(dir.Name);
            LoadDirectories(dir.FullName, newNode.Nodes);
        }
    }
}

If I comment out the recursive call I get all of the subdirectories in the tree. If I don't I don't. Some directories are missing. I'm not sure what is going on.

Help?

Scott

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2024-09-19 01:33:04

使用这个示例代码它工作得很好。我建议您以此为起点。

static void Main(string[] args)
{
    var n = LoadDirectory(@"E:\Share");
    Console.WriteLine(n.ToString());
    Console.ReadLine();
}

private static Node LoadDirectory(string path)
{
    var n = new Node() { Name = path };
    LoadDirectories(path, n);
    return n;
}

private static void LoadDirectories(string currentPath, Node node)
{
    DirectoryInfo directoryInfo = new DirectoryInfo(currentPath);
    DirectoryInfo[] directories = directoryInfo.GetDirectories();

    foreach (DirectoryInfo dir in directories)
    {
        if ((dir.Attributes & FileAttributes.System) != FileAttributes.System &&
            (dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
        {
            var n = new Node() { Name = dir.Name };
            node.Children.Add(n);
            LoadDirectories(dir.FullName, n);
        }
    }
}

private class Node
{
    public List<Node> Children = new List<Node>();
    public string Name;

    public override string ToString()
    {
        return ToString(1);
    }

    public string ToString(int depth)
    {
        if (Children.Count == 0)
        {
            return Name;
        }
        var sb = new StringBuilder(Name);
        foreach (var n in Children)
        {
            sb.AppendLine();
            sb.Append("".PadLeft(depth, '\t') + n.ToString(depth + 1));
        }
        return sb.ToString();
    }
}

Using this example code it works just fine. I recommend you use this as a starting point.

static void Main(string[] args)
{
    var n = LoadDirectory(@"E:\Share");
    Console.WriteLine(n.ToString());
    Console.ReadLine();
}

private static Node LoadDirectory(string path)
{
    var n = new Node() { Name = path };
    LoadDirectories(path, n);
    return n;
}

private static void LoadDirectories(string currentPath, Node node)
{
    DirectoryInfo directoryInfo = new DirectoryInfo(currentPath);
    DirectoryInfo[] directories = directoryInfo.GetDirectories();

    foreach (DirectoryInfo dir in directories)
    {
        if ((dir.Attributes & FileAttributes.System) != FileAttributes.System &&
            (dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
        {
            var n = new Node() { Name = dir.Name };
            node.Children.Add(n);
            LoadDirectories(dir.FullName, n);
        }
    }
}

private class Node
{
    public List<Node> Children = new List<Node>();
    public string Name;

    public override string ToString()
    {
        return ToString(1);
    }

    public string ToString(int depth)
    {
        if (Children.Count == 0)
        {
            return Name;
        }
        var sb = new StringBuilder(Name);
        foreach (var n in Children)
        {
            sb.AppendLine();
            sb.Append("".PadLeft(depth, '\t') + n.ToString(depth + 1));
        }
        return sb.ToString();
    }
}
甜心小果奶 2024-09-19 01:33:04

事实证明问题根本不在于递归调用。问题在于,如果当前用户无权访问所请求的目录,则 GetDirectories 调用会引发 AccessDenied 异常。

我只是将适当的调用包装在 try/catch 块中,一切都很好。

感谢您对混沌的帮助!

It turns out the problem wasn't with the recursive call at all. The problem was that the GetDirectories call was throwing an AccessDenied exception if the current user didn't have access to the requested directory.

I simply wrapped the appropriate calls in a try/catch block and all is well.

Thanks for your help Chaos!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文