OpenFileDialog InitialDirectory

dbilancione

New member
Joined
Jul 17, 2023
Messages
3
Programming Experience
Beginner
Hi everyone,

I am new to C#/VSTO. I used to build Excel application using VBA but I want to move to VSTO. I started converting an application I recently built using VBA.

Using the OpenFileDialog class I wanted to set an InitialDirectory property to "Desktop", which I successfully did, but I wish to restore the last opened path (using the RestoreDirectory property if the user had already navigated to a different path.

Is there a way to test if there is a directory to restore as initial directory else set the "Desktop" as initial directory?

Something like

C#:
 OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Import";

            if fdlg.RestoreDirectory(path) not set
                fdlg.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            fdlg.Filter = "Excel Files (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm|All Files (*.*)|*.*";
            fdlg.FilterIndex = 1;
            fdlg.Multiselect = true;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                foreach (String file in fdlg.FileNames)
                {
                    MessageBox.Show(file);
                }
            }
        }

Many thanks for your suggestions!
 
Is there a way to test if there is a directory to restore as initial directory else set the "Desktop" as initial directory?

The only thing you can do is check what that current directory is set to for the program before opening the open file dialog. But that all the restore directory does anyway. It remembers what the current directory is set to, so that if the flag is true, and current directory is restored.
 
The only thing you can do is check what that current directory is set to for the program before opening the open file dialog. But that all the restore directory does anyway. It remembers what the current directory is set to, so that if the flag is true, and current directory is restored.

Right, that is what I wish to achieve but executing the statement on line #5 (fdlg.InitialDirectory) causes the dialog to open the "Desktop" path all the time.

Ideally, I would like to open it only the first time it runs but when the user has already browsed to a path where the source documents are stored, the dialog should open that path.

How would I check the current directory?
 
What I'm saying is, the "it always starts on the dekstop" is caused by you setting the ID. Don't set the ID, and the default behavior of the OFD a) applies b) is more logical c) is what you're looking for (reopens where the user last was).

In essence your code causes the problem you're now trying to solve
 
Also, in general Microsoft recommends that documents be stored in the documents folder, not the desktop folder. As I recall, the OFD defaults to the documents folder.
 

Latest posts

Back
Top Bottom