查找目录是否有父目录

发布于 2024-12-02 14:53:24 字数 852 浏览 3 评论 0原文

private void anotherMethod()
{
    DirectoryInfo d = new DirectoryInfo("D\\:");
    string s = included(d);
     ... // do something with s
}

private string included(DirectoryInfo dir)
{
    if (dir != null)
    {
        if (included(dir.FullName))
        {
            return "Full";
        }
        else if (dir.Parent != null) // ERROR
        {
            if (included(dir.Parent.FullName))
            {
                return "Full";
            }
        }
        ...
    }
    ...
}

上面的代码是我正在使用的,但是它不起作用。它抛出一个错误:

未将对象引用设置为对象的实例

dir.FullPath 的实例是 B:\,因此它没有父级,但为什么 dir.Parent != null 会给出错误?

如何检查给定目录是否存在父目录?

请注意,我有两个“包含”方法:

  • included(string s)
  • included(DirectoryInfo dir)

为此目的,您可以假设included(strings)返回false

private void anotherMethod()
{
    DirectoryInfo d = new DirectoryInfo("D\\:");
    string s = included(d);
     ... // do something with s
}

private string included(DirectoryInfo dir)
{
    if (dir != null)
    {
        if (included(dir.FullName))
        {
            return "Full";
        }
        else if (dir.Parent != null) // ERROR
        {
            if (included(dir.Parent.FullName))
            {
                return "Full";
            }
        }
        ...
    }
    ...
}

The above code is what I'm using, it doesn't work however. It throws an error:

object reference not set to an instance of an object

dir.FullPath is B:\ so it has no parent but why does dir.Parent != null give an error?

How can I check to see if a parent directory exists for a given directory?

Notice that I have two "Included" methods:

  • included(string s)
  • included(DirectoryInfo dir)

for the purpose of this you can just assume that included(string s) returns false

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

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

发布评论

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

评论(3

救赎№ 2024-12-09 14:53:24

修复:else if (dir != null && dir.Parent != null)

Fix: else if (dir != null && dir.Parent != null)

冷情 2024-12-09 14:53:24
    public static bool ParentDirectoryExists(string dir)
    {
        DirectoryInfo dirInfo = Directory.GetParent(dir);
        if ((dirInfo != null) && dirInfo.Exists)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static bool ParentDirectoryExists(string dir)
    {
        DirectoryInfo dirInfo = Directory.GetParent(dir);
        if ((dirInfo != null) && dirInfo.Exists)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
心清如水 2024-12-09 14:53:24

您应该能够根据以下内容检查 dir.Parent 是否为 null:

父目录,如果路径为空或文件路径表示根目录(例如“\”、“C:”或 *“\server\share”,则为空引用(在 Visual Basic 中为 Nothing) )。

问题是,就像其他人已经指出的那样,您正在访问空引用(dir)

来源

You should be able to check dir.Parent against null, according to this:

The parent directory, or a null reference (Nothing in Visual Basic) if the path is null or if the file path denotes a root (such as "\", "C:", or * "\server\share").

The problem is, like others pointed out already, you're accessing a method on a null reference (dir)

Source

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