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 会出错?
如何检查给定目录是否存在父目录?
请注意,我有两个“包含”方法:
- 包括(字符串 s)
- 包括(目录信息目录)
为此,您可以假设 include(string s) 返回 false