Does OpenFileDialog InitialDirectory not accept relative path?

Marson Mao picture Marson Mao · Feb 14, 2014 · Viewed 8.8k times · Source

dialog is a OpenFileDialog class object, and I am using ShowDialog() method.

When I use path containing relative path, like:

dialog.InitialDirectory = "..\\abcd";
dialog.InitialDirectory = Directory.GetCurrentDirectory() + "..\\abcd";

ShowDialog() crashes, what I can only do is giving a definite path, start by disk drive:

dialog.InitialDirectory = "C:\\ABC\\DEF\\abcd";

In this case I want the path to be 1 levels upward of my .exe's current directory, and then downward to directory abcd)
The .exe's current path can be found by Directory.GetCurrentDirectory(), which is perfectly fine, but I cant go on with "..")

The directory hierarchyis like:

ABC
    DEF 
        abcd (where i want)
        defg (where .exe is at)

So, is there any method to use "..\\" with InitialDirectory?
Or I must use definite path with it?
Thanks!

Answer

Marson Mao picture Marson Mao · Feb 14, 2014

I found my own answer!!

string CombinedPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "..\\abcd");
dialog.InitialDirectory = System.IO.Path.GetFullPath(CombinedPath);