10

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?

8
  • possible duplicate of Setting the initial directory of an SaveFileDialog? Jan 11, 2012 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, 2012 at 10:16
  • @Cody Gray it's not duplicate. I've already tried that solution and it's not working Jan 11, 2012 at 10:32
  • Would have made sense to mention that in the question. What else have you already tried? Jan 11, 2012 at 10:34
  • With dialog.AutoUpgradeEnabled = true; it's working fine... Weird... Jan 11, 2012 at 10:37

16 Answers 16

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.

2
  • This was the answer form me.
    – Sandwich
    Jun 13, 2019 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 4, 2022 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"

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

0
3

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

    if (!Directory.Exists(InitialDirectory))
        Directory.CreateDirectory(InitialDirectory);
1
  • This makes sense and was also my case. No need for other fixes
    – kuklei
    Oct 31, 2020 at 11:21
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, "*.*");
1

I got the code to work this way:

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

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.

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.

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\\";
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
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 "";
    }
}
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.

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\

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

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.