I\'d like a SaveFileDialog with the following behavior:
The first time you open it, it goes to \"My Documents\".
Afterwards, it goes to the
savefiledialog.InitialDirectory = Application.StartupPath;
savefiledialog.RestoreDirectory = true;
tested a second ago.
'In terms of saving an output file to a desired directory in vb.net, 'the following was the way I found that worked like a charm:
Dim filcsv As String = fileNamey.Replace(".txt", "_Denied2.csv")
Dim filcsv2 As String = fileNamey.Replace(".txt", "_Approved2.csv")
Dim outputDirectory As String = "C:\Users\jlcmil\Documents\EnableMN\output\"
Dim totalPath As String = System.IO.Path.Combine(outputDirectory, filcsv)
Dim totalPath2 As String = System.IO.Path.Combine(outputDirectory, filcsv2)
Dim outDenied As StreamWriter = New StreamWriter(totalPath)
Dim outApproved As StreamWriter = New StreamWriter(totalPath2)
The suggested workarounds didn't work for me, so after finding How does WPF OpenFileDialog track directory of last opened file? I implemented:
public static void SetInitialDirectory(this FileDialog dlg, string fileExtension, string initialDirectory)
{
// RestoreDirectory doesn't seem to be implemented - https://stackoverflow.com/questions/11144770/how-does-wpf-openfiledialog-track-directory-of-last-opened-file
// so manually only set InitialDirectory if nothing is stored
try
{
var mru = @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\" + fileExtension;
var rk = Registry.CurrentUser.OpenSubKey(mru);
if (rk == null)
{
dlg.InitialDirectory = initialDirectory;
}
}
catch (Exception)
{
// SecurityException, ObjectDisposedException => allow default behaviour
}
}
This will use the provided initialDirectory if the dialog has not been used before for this file extension. Once the dialog has been used, it reverts to the default behaviour of remembering the previous directory.
Just wanted to weigh in on this discussion (a couple of years too late) as I had exact problem too. Even though one is led to believe that this behavior - i.e using a default path the first time and then the previous selected path after that - can be achieved by using openFileDialog's properties and functions, one simply can't (per now)!
One could change the working directory to the desired default path
(e.g. Environment.CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
)
but some framework, like Unity, doesn't like this very much and terminates.
Because of this I ended up with this code:
private bool firstDialogOpened = true;
public void BrowseFiles()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Model files (*.obj)|*.obj|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
if (firstDialogOpened)
{
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
firstDialogOpened = false;
}
if (openFileDialog.ShowDialog() == DialogResult.OK)
filepath.text = openFileDialog.FileName;
}
This gives the desired behavior for me, although I would have loved a more elegant solution.
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, "*.*");
Make sure to check that the directory path exists before setting the Initial directory property. Create the directory if it does not exist. ie
if (!Directory.Exists(FooDirectory))
{
Directory.CreateDirectory(FooDirectory);
}