views:

82

answers:

2

Hi everyone,

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 by 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,

+4  A: 

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.

Dean Harding
+1  A: 

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.

GrayWizardx