OpenFileDialog Initial Directory not working

G

Greg

I'm a bit confused by this - when I set the InitialDiretory of an
OpenFileDialog to be a subfolder within the same folder as the app, the
file dialog does not initially display the folder - instead it shows
the last used folder (this persists after closing the app).

However, if I use a different path (e.g. "C:\\") then every time the
dialog is shown, it starts at the correct directory every time.

Is there something special about an app path that a file dialog doesn't
like, or is my syntax wrong???

OpenFileDialog objOpenDlg = new OpenFileDialog();
objOpenDlg.InitialDirectory = Application.ExecutablePath + @"\Games\";
if (objOpenDlg.ShowDialog() == DialogResult.OK)
{
//do some stuff here
}

It doesn't seem to make any difference whether the app is a debug or
release mode, and whether it is run from the IDE or not.

Any thoughts???

Greg.
 
T

Truong Hong Thi

Hi Greg,

Use Application.StartupPath instead. Application.ExecutablePath
includes the file name.

objOpenDlg.InitialDirectory = Path.Combine(Application.StartupPath,
"Games");

Regards,
Thi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top