0

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?

UPDATE

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

UPDATE 2

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.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    There's nothing "unorthodox" about installing to a drive other than `C:`. Is your question actually "How can I determine where my application was installed?" I have many "drives" available on my system - my personal computer has two partitions (C and D, plus a DVD drive Z), and at my office I have 6 (C, D, E. J, K, and L), not including mappable network space. – Ken White Oct 04 '14 at 04:32
  • I've fixed sample... `.ProgramFiles` is the one you are looking for. – Alexei Levenkov Oct 04 '14 at 05:03

3 Answers3

4

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).

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • You mean I can't allow a user to write a file to a folder beneath my app? That is, if my app "Waltons" is installed in Program Files, and it has a subfolder "Mountains" I can't allow the user to save files there? – B. Clay Shannon-B. Crow Raven Oct 04 '14 at 04:48
  • 1
    @AlexeiLevenkov per your update, there is a 3rd option. Setting the security permissions making the folder writeable to the `Users` user group. – Scott Chamberlain Oct 04 '14 at 04:53
  • 1
    @B.ClayShannon There are ways around it, but you are not supposed to. You should really write the data to `My Documents` – Scott Chamberlain Oct 04 '14 at 04:55
  • 1
    @B.ClayShannon Note: I would not try to allow normal user to write to that locations - even ignoring security restrictions you will run into problems with different users overwriting/locking files... Unless there is really good reason use "my documents", next some other folder like `CommonApplicationData`... – Alexei Levenkov Oct 04 '14 at 04:59
  • How would they overwrite anything - it's a single-user app. – B. Clay Shannon-B. Crow Raven Oct 04 '14 at 21:13
  • 1
    @B.ClayShannon Do you believe that Windows/*nix are single user operating systems where 2 users are never logged in at the same time and running the same application? If you can ensure that (i.e. your application is explicitly designed for some sort of environment where only single user allowed to log in at a time) - indeed there will be no issues with locking (still each user on machine must be very careful to not overwrite others files stored in the shared location). For future questions consider specifying such restriction as it will allow more targeted answers. – Alexei Levenkov Oct 05 '14 at 00:27
2

Don't use hardcoded driver letters. Find the path in runtime. e.g.:

saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
1

You can use the environment variable systemroot to get the drive. Try opening a cmd prompt and entering set to see a list of all the environment variables avaliable and then use System.Envrionment.GetEnvironmentVariable ("systemdrive") and combine that with the rest of your path.