1

Currently writing software which needs to store data (I know, it's that advanced) but I don't know where people are going to want to save it. As a default I would like it to save to C:\ but that doesn't work for use on my server which uses N:\ as the default drive.

I could change it to use N:\ but I would have the same problem if a person the chose to run it on a system with anything else as a drive.

I know you can get the current programs location, but how could I then copy the first part so I could use it.

Example

Program location: X:\Something\Something\program.exe

What I want to be able to take from that: X:\Something\Something and the add the name of the new files to be created (newdatafile.txt) so it becomes X:\Something\Something\newdatafile.txt

I'w working in C#.

Rob
  • 71
  • 8
  • If you leave out the X:\Something\Something\ part and just use the file name, I believe it automatically figures out the X:\Something\Something\ of the program location. For example, I assign the `HelpNamespace` of a `HelpProvider` to only a help.chm file, and it automatically looks in Program Files (for my individual situation). Feel free to correct me if I'm wrong. – Broots Waymb Sep 04 '15 at 16:14
  • Ok, I will try that out :) – Rob Sep 04 '15 at 16:15
  • Can you please clarify whether you are looking for current program location (http://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) OR you are actually interested in where you should be storing user files by default (http://stackoverflow.com/questions/9659127/folder-to-store-data-files-locally-in-wpf-application)? (I'm afraid both versions would be duplicates, but maybe you are looking for something totally different) – Alexei Levenkov Sep 04 '15 at 16:25
  • Bit of a mix of the both. What I want is the current path from the running program, but then how to use that file path to write text files. The end result being that both the .exe and data it creates are in the same place no matter where the users runs the program. – Rob Sep 04 '15 at 16:31

2 Answers2

3

I think this will do what you asked for:

var exeFolder = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\";

Then simply append your new file name to the end.

Ulric
  • 826
  • 5
  • 16
  • 2
    To append the filename I'd recommend `Path.Combine(exeFolder, fileName);` as it will take care of the slashes for you. – Equalsk Sep 04 '15 at 16:19
  • 2
    Note this is exactly what OP is asking for, but saving user's data next to executable is not a good idea for "per-user" installed application and completely non-starer for per-machine installed once (as regular users will not be able to write to such folders). – Alexei Levenkov Sep 04 '15 at 16:24
  • Excellent point. In this case OP should look into environment special folders https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx – Equalsk Sep 04 '15 at 19:22
1

You can use the SaveFileDialog to handle all the heavy lifting.

https://msdn.microsoft.com/en-us/library/sfezx97z(v=vs.110).aspx

Clay Sills
  • 235
  • 1
  • 9