Search code examples
c#directoryrelative-pathopenfiledialog

Using OpenFileDialog w/ a relative path as initialDirectory


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?


Solution

  • 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...