开发者

Understanding a boolean on DirectoryInfo.Exists

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);

I am just wondering why diPath.Exists is false in this case? Since the user has selected a file, the directory must exist!? and it does...

I have used a work around b开发者_Python百科y using Directory.Exists(fiPath.DirectoryName) but it seems strange that the above isn't working, and slightly irritating to need that other var just to check something that I know is there exists, and should just be able to use the diPath. What's the deal?

Also on a related matter, say I have a directoryinfo for a directory C:\random\spot\here why is there no method to obtain that string "C:\random\spot\here" it seems I can only get Parent "spot" or Name "here". Maybe I missed something.

Thanks,


There is a file called path but there is not directory called path.

var diPath = new DirectoryInfo(Path.GetDirectoryName(path));

is probably what you want.


Your including the filename in the "path", and as such the path will be a leaf node (i.e. file) and not a directory (branch node). Windows file/path handling is quite literal about those kind of things.

As mentioned previously DirectoryInfo or Path.GetDirectoryName() is probably what you want to use if working with paths.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜