'OpenFileDialog InitialDirectory doesn't work

I have this code:

OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = GetDataPath(...);
dialog.AutoUpgradeEnabled = false;
dialog.Filter = GetFilter(...);
if (dialog.ShowDialog(this) == DialogResult.OK)
{...}

I expect, at every run, to have the dialog in same folder - GetDataPath(...) folder, but it remains in the last selected folder.

Is this the correct behavior? Do you know how to fix this? If Windows saves last used path in registry do you know how to find it?

EDIT1:

With:

dialog.AutoUpgradeEnabled = true;

is working as expected...

EDIT2: same problem as here Any known problems with getting SaveFileDialog's InitialDirectory property working in Windows 7?



Solution 1:[1]

Do no include filename to InitialDirectory. Path only.

From msdn: On Windows Vista, if InitialDirectory is set to a full file name instead of just a directory path, the initial directory will default either to the application path, or to the directory from which the user last selected a file.

Solution 2:[2]

to me those answers didn't help (windows 7).

my path looked like this: "C:/xxxx/yyyyy" after switching to backslash it worked fine, my path now looks like this: "C:\xxxxx\yyyyy"

Solution 3:[3]

It may require to set RestoreDirectory

OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = GetDataPath(...);
dialog.RestoreDirectory = true;
dialog.AutoUpgradeEnabled = false;
dialog.Filter = GetFilter(...);
if (dialog.ShowDialog(this) == DialogResult.OK)
{...}

Check this link

Solution 4:[4]

I too have tried different "solutions" found in different places, but none of them seem to work as soon as there is an MRU list entry in the registry :/ But here is my own simple workaround…

Instead of setting the dialog's InitialDirectory property, set the FileName property to your path, but combined with the selected Filter, e.g.:

dialog.FileName = Path.Combine(myPath, "*.*");

Solution 5:[5]

In my case it was not working because the 'InitialDirectory' did not exist.

    if (!Directory.Exists(InitialDirectory))
        Directory.CreateDirectory(InitialDirectory);

Solution 6:[6]

I got the code to work this way:

dialog.InitialDirectory = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\Videos";

Solution 7:[7]

I had the same problem. When I used this code:

string imgPath = AppDomain.CurrentDomain.BaseDirectory + @"Images\";

That does not show the initial directory.

But if I removed the final slash:

string imgPath = AppDomain.CurrentDomain.BaseDirectory + @"Images";

So began show initial directory correctly. Restoring backslash not cause incorrect show, what I don't understand, but it is so.

Solution 8:[8]

I had a problem with this too where it only would show the last directory used. I was using a network path with no drive letter. I needed to add another "\" in front of the server name.

This didn't work:

openFileDialog1.InitialDirectory = "\\\servernam01\\group.data\\EXTERNAL PROJECTS\\VSCHART\\ercotfiles\\";

But this did work:

openFileDialog1.InitialDirectory = "\\\\servernam01\\group.data\\EXTERNAL PROJECTS\\VSCHART\\ercotfiles\\";

Solution 9:[9]

This was happening to me, but the problem was different. I had a typo in the path I was using for the InitialDirectory. When I fixed that, I was fine. If this is happening to you check your output window for this:

A first chance exception of type 'System.IO.FileNotFoundException' 
occurred in System.Windows.Forms.dll

Solution 10:[10]

I've tried the solutions given but without success, but what worked for me, is to remove the trailing "/" from my path.

path = path.TrimEnd(new char[] { '\\' });

Then it works correctly.

Solution 11:[11]

Please, include this function before sending the InitialDirectory.

public static string NormalizePath(string path)
{
    if (path != "")
    {
        return Path.GetFullPath(new Uri(path).LocalPath)
                   .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
                   .ToUpperInvariant();
    }
    else
    {
        return "";
    }
}

Solution 12:[12]

None of the other answers worked for me. See below

Use GetFolderPath for OpenFileDialog object's InitialDirectory.

using (this.openFile)
{
    this.openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
}

this.openFile is just a OpenFileDialog object added to form instead of creating a new object in code.

Solution 13:[13]

I have been having issues with this too. Here is how I fixed it:

Assume bakDir is a string containing the initial directory path you want for your OpenFileDialog.

        OpenFileDialog openFile = new OpenFileDialog();
        if (!Directory.Exists(bakDir))
        {
            Directory.CreateDirectory(bakDir);
        }
        openFile.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"DbBackups";

And when you're done doing your thing with selected file, call this:

        openFile = null;

Solution 14:[14]

I SOLVEDD WORK, IN THE APLICATION CODE PUT THIS¡¡¡¡¡¡¡¡¡¡¡¡¡¡

With oFolderBrowserDialog

   .ShowNewFolderButton = True
   .RootFolder = Environment.SpecialFolder.DesktopDirectory

      If Directory.Exists(RutaReteDescarga) Then

           .SelectedPath = RutaReteDescarga
           .RootFolder = Environment.SpecialFolder.DesktopDirectory
            oFolderBrowserDialog.ShowDialog(Me)
            dRuta = .SelectedPath

    END IF

END WITH

TWO TIMES DesktopDirectory, I WORK PERFECTLY WHEN I OPEN THE BUTTON THE PATH FOR DEFAULT APPEARS HERE IN THE WINDOWS.