理解DirectoryInfo.Exists上的布尔值

bar*_*ron 2 c# boolean directoryinfo fileinfo

var fileOpen = new OpenFileDialog(); var clickedOk = fileOpen.ShowDialog(); if(!((bool)clickedOk))return;

var path = fileOpen.FileName;
var diPath = new DirectoryInfo(path);
var fiPath = new FileInfo(path);

Debug.WriteLine(diPath.Exists);
Run Code Online (Sandbox Code Playgroud)

我只是想知道为什么diPath.Exists在这种情况下是假的?由于用户选择了一个文件,该目录必须存在!?它确实......

我已经使用了一个使用的工作,Directory.Exists(fiPath.DirectoryName)但似乎很奇怪,上面的工作不起作用,并且需要其他var来检查我知道存在的东西,并且应该只能使用diPath,这有点令人恼火.这是怎么回事?

还有一个相关的问题,说我有一个目录C:\ random\spot \这里的directoryinfo为什么没有方法获取该字符串"C:\ random\spot\here"它似乎我只能得到父"点"或姓名"在这里".也许我错过了什么.

谢谢,

Dea*_*ing 6

有一个文件被调用,path但没有名为path的目录.

var diPath = new DirectoryInfo(Path.GetDirectoryName(path));
Run Code Online (Sandbox Code Playgroud)

可能就是你想要的.