Reputation: 20800
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?
Upvotes: 11
Views: 22770
Reputation: 480
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\
Upvotes: 0
Reputation: 1
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);
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: -2
Reputation: 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 "";
}
}
Upvotes: 1
Reputation: 2862
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.
Upvotes: 1
Reputation: 1917
In my case it was not working because the 'InitialDirectory' did not exist.
if (!Directory.Exists(InitialDirectory))
Directory.CreateDirectory(InitialDirectory);
Upvotes: 3
Reputation: 8208
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
Upvotes: 0
Reputation: 173
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\\";
Upvotes: 0
Reputation: 125
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"
Upvotes: 6
Reputation: 171
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.
Upvotes: 0
Reputation: 101
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.
Upvotes: 10
Reputation: 10357
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, "*.*");
Upvotes: 2
Reputation: 1933
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;
Upvotes: -1
Reputation: 21
I got the code to work this way:
dialog.InitialDirectory = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\Videos";
Upvotes: 1
Reputation: 2293
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
Upvotes: 5