How do I set the Initial Directory on an OpenFileDIalog to the users `Downloads` folder in C#

JMK picture JMK · Mar 18, 2012 · Viewed 21.5k times · Source

Ok so I have an OpenFileDialog and I want to set the initial directory to the users 'Download' folder. This is an internal application and, therefore, I am sure that the user will be using Windows 7.

var ofd = new OpenFileDialog();

//This doesn't work
ofd.InitialDirectory =
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Downloads");

//This doesn't work either
ofd.InitialDirectory = @"%USERPROFILE%\Downloads";

ofd.Filter = "Zip Files|*.zip";

ofd.ShowDialog();

txtFooBar.Text = ofd.FileName;

I have tried the above so far and neither work. No Exception is thrown, it just doesn't set the initial directory to the downloads folder.

Where am I going wrong?

Thanks

Answer

Tim Melton picture Tim Melton · Apr 24, 2015

I was able to use the environment to call directly but I had to add ToString() to the end. It didn't work until I added it.

saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);