Using OpenFileDialog w/ a relative path as initial

2019-07-20 14:00发布

问题:

I'd like to give the appropriate value to Initial directory, so it would open the folder (called "Images") which I created in my project. I must use a relative path, so my program works not depending on the computer I work . But the problem is I don't know how to access this folder...

Does anyone know how to solve this problem?

回答1:

use:

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

Edit: or try this if you prefer... are you in windows forms?

openFileDialog.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @"YourSubDirectoryName");

Edit 2 for WPF:

string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

... from the Microsoft Forums...