I can set a SaveFileDialog's InitialDirectory property to where my app will usually be installed like so:
saveFileDialog1.InitialDirectory = @"C:\Program Files\Waltons\Mountains";
...but as "there is one in every crowd," some may use a drive letter other than C for their hard drive? How can I set the InitialDirectory to whichever drive letter the user has specified?
I tried Alexei's code (had to change "Concat" to "Combine" and remove a superfluous ")":
saveFileDialog1.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Programs), @"Waltons\Mountains");
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
// TODO: Finish
}
...but it does not open C:\Program Files\Waltons\Mountains
Saeb's suggestion seems to work, as the save file dialog opens in C:\Waltons\Mountains\bin\Debug
...which I hope/reckon would correspond to C:\Waltons on the user's machine (or D:\Waltons or Z:\Waltons or whatever).
I would have to append the "\Maps" I guess for the user - check that it's not running in Visual Studio or something and append that in that event.
Location that is writable by normal user would be better default location for save dialog. I.e. "my documents" via Environment.GetFolderPath
passing one of Environment.SpecialFolder values:
var pathToMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.Personal));
If you need location where your program installed by default like program files
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "MyFolder");
or if you need path relative to program check How can I get the application's path in a .NET console application?.
Note that locations above are not writable by normal users and even admin unless you turn off UAC or explicitly "run as administrator" or change default permissions on these folders (either of approaches to bypass default permission have its drawbacks and one should perform serious security review if allowing regular users to write in programs
/system
folders).