9

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?

Community
  • 1
  • 1
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

16 Answers16

9

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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Sergey
  • 91
  • 1
  • 1
  • This was the answer form me. – Sandwich Jun 13 '19 at 10:36
  • 1
    Apparently this also happens when setting `FileName` to a full path. I'm not sure why the system can't be smart enough to simply split that up and put each part in the right place. – Nyerguds Aug 04 '22 at 10:27
6

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"

Veit
  • 101
  • 1
  • 5
5

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

AksharRoop
  • 2,263
  • 1
  • 19
  • 29
2

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, "*.*");
mousio
  • 10,079
  • 4
  • 34
  • 43
2

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

    if (!Directory.Exists(InitialDirectory))
        Directory.CreateDirectory(InitialDirectory);
jv_
  • 1,784
  • 1
  • 16
  • 12
1

I got the code to work this way:

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

Marko
  • 20,385
  • 13
  • 48
  • 64
1

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.

Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
0

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.

Petr Žoček
  • 151
  • 1
  • 4
0

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\\";
Greg Barth
  • 173
  • 1
  • 4
  • 12
0

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
user2023861
  • 8,030
  • 9
  • 57
  • 86
0

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 "";
    }
}
barbsan
  • 3,418
  • 11
  • 21
  • 28
Josep
  • 1
  • 9
0

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.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

CommonOpenFileDialog can appear to have this same problem:

Expand any unresolved environment variables, eg. %appdata% to full form:

var dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = Environment.ExpandEnvironmentVariables(path);
0

Using Path.GetFullPath() worked for me dialog.InitialDirectory =Path.GetFullPath (Environment.CurrentDirectory + @"....\Data\Tabs");

which returns G:\Arduino\GUI\Piano\Piano\Data\Tabs\

instead of G:\Arduino\GUI\Piano\Piano\bin\Debug....\Data\Tabs\

Antonio Leite
  • 460
  • 3
  • 7
-1

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;
kformeck
  • 1,703
  • 5
  • 22
  • 43
-2

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.

  • 5
    Hello, please read [Why is it considered inappropriate and unprofessional to type in all capital letters?](https://www.quora.com/Why-is-it-considered-inappropriate-and-unprofessional-to-type-in-all-capital-letters). Thank you. – Eric Aya Dec 01 '21 at 16:48
  • 4
    You can (and should) use the "Edit" button to improve your answer. – Eric Aya Dec 01 '21 at 16:48
  • 1
    You are going to get downvoted and lose rep if you don't fix the all caps as Eric references. You should definitely do that so that you gain rep from answering the question instead of losing it. – Rodger Dec 01 '21 at 17:33