5

dialog is an 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 only can do is giving a definite path, starting with a disk drive:

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

In this case I want the path to be 1 level up 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 hierarchy is like:

ABC
    DEF 
        abcd (where I want to go)
        defg (where .exe is at)

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

davidsbro
  • 2,761
  • 4
  • 23
  • 33
Marson Mao
  • 2,935
  • 6
  • 30
  • 45
  • check this: http://stackoverflow.com/questions/1175242/setting-the-initial-directory-of-an-savefiledialog/1175250#1175250 – Matt Feb 14 '14 at 03:09

3 Answers3

12

I found my own answer!!

string CombinedPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "..\\abcd");
dialog.InitialDirectory = System.IO.Path.GetFullPath(CombinedPath);
Marson Mao
  • 2,935
  • 6
  • 30
  • 45
4

See if the following gets you the path you're looking for:

dialog.InitialDirectory
  = Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()), "abcd");

The call to Path.GetDirectoryName strips off the last portion of the path, after the last directory separator, whether it's a file name or folder name.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • The issue was that adding a relative path like ..\abcd doesn't work. It still won't with the above approach; rather check Marson Mao's answer. – Ruud van Gaal May 15 '18 at 11:27
0

Another way would be

openFileDialog.InitialDirectory = Path.Combine(Application.StartupPath,@"..\YourSubDirectoryName");