7

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
  • 19,462
  • 29
  • 118
  • 205
  • possible duplicate of [Setting the initial directory of an SaveFileDialog?](http://stackoverflow.com/questions/1175242/setting-the-initial-directory-of-an-savefiledialog) – Cody Gray Jan 11 '12 at 10:10
  • note that InitialDirectory is not used if you have a selected FileName(s) ... not the case in your sample code though. – zeFrenchy Jan 11 '12 at 10:16
  • @Cody Gray it's not duplicate. I've already tried that solution and it's not working – Mircea Ispas Jan 11 '12 at 10:32
  • Would have made sense to mention that in the question. What else have you already tried? – Cody Gray Jan 11 '12 at 10:34
  • With dialog.AutoUpgradeEnabled = true; it's working fine... Weird... – Mircea Ispas Jan 11 '12 at 10:37
  • Makes sense. You should probably have that property set anyway to provide your users with a consistent experience. I'm not sure how it got *unset*, considering it's the default. – Cody Gray Jan 11 '12 at 10:41
  • I need to have AutoUpgradeEnabled = false because I need to hide file extensions - http://stackoverflow.com/questions/8801385/c-sharp-dont-display-filter-extenstions-in-openfiledialog/8801665#8801665 – Mircea Ispas Jan 11 '12 at 10:44
  • Also do not use relative paths. Use `InitialDirectory = Path.GetFullPath(directory)` – schoetbi Jan 20 '21 at 09:42

14 Answers14

7

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
  • 21,924
  • 8
  • 55
  • 89
Sergey
  • 71
  • 1
  • 1
4

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
  • 81
  • 4
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

AksharRoop
  • 2,208
  • 1
  • 18
  • 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
  • 9,721
  • 4
  • 33
  • 42
2

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

    if (!Directory.Exists(InitialDirectory))
        Directory.CreateDirectory(InitialDirectory);
jv_
  • 1,456
  • 14
  • 11
1

I got the code to work this way:

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

Marko
  • 19,975
  • 13
  • 46
  • 63
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
  • 7,542
  • 7
  • 52
  • 80
0

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,770
  • 9
  • 45
  • 70
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,328
  • 11
  • 20
  • 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
  • 10,331
  • 15
  • 20
  • 47
-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,603
  • 5
  • 22
  • 42
-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.

  • 4
    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
  • 3
    You can (and should) use the "Edit" button to improve your answer. – Eric Aya Dec 01 '21 at 16:48
  • 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