Seda
Seda

Reputation: 103

Open File Dialog Initial directory

I have problem with InitialDirectory path i used part of code shown below. OpenDialog always show directory where i open file last time but i couldn't set new relative path.. I tried set absolute path but it didn't work also.

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

public static string OpenDialog()
    {
        // Create OpenDialog 
        var dlg = new Microsoft.Win32.OpenFileDialog();

        // initial directory for OpenFileDialog need fix
        if(Directory.Exists(path))
        {
            dlg.InitialDirectory = path;
        } 

        dlg.RestoreDirectory = true;

Upvotes: 0

Views: 1554

Answers (1)

In your example, 'path' is being set to your .exe, which will cause if (Directory.Exists(path)) to fail, therefore, the dialog will open to the last known good directory, because InitialDirectory will not be set to the value that you want. Try simply hard-coding a known good directory path first. Or you could do something like this to fix it:

path = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;

Upvotes: 1

Related Questions