Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I would like to know if you can set the path to the file where the end user saves the created application. I found some options but none of them worked for me as I imagined or maybe I made a mistake somewhere. As a first option, I chose to hard-tune the path to documents, but not every user has the same path. Then I found a possibility :
C#
string path= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
I liked it but it wasn't real, and then I wondered if it would be possible to save a text file directly to the file from which the application is launched. I found two options:
C#
string path;
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
//and
string Path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)


What I have tried:

Does anyone have a solution how to set the path to the document where the application is located?

Something like StartupPath or something like that?

Thank you for any advice.
Posted
Updated 8-Jun-21 22:57pm

1 solution

I'll sneak in before Griff manages to do his thing, you should take a look at Where Should I Store My Data?[^] which gives examples of why storing data in the application directory might not be a good idea. If the application is installed in a protected place, such as Program Files or even the base C:\ directory, you could have problems opening and writing files to those locations.

Instead consider using some of the directories the system has specifically for storing data for either all users or the local user. These would be places indicated by the SpecialFolder.LocalApplicationData, SpecialFolder.ApplicationData and SpecialFolder.CommonApplicationData values.
 
Share this answer
 
Comments
Chris Copeland 9-Jun-21 4:59am    
However, as an aside since I didn't really answer the question, have you tried using System.Reflection.Assembly.GetEntryAssembly().Location;? The entry assembly is the one identified as having run the Main method.
Member 15170612 9-Jun-21 5:05am    
I haven't tried it, thank you for the advice and I'm going for it.
OriginalGriff 9-Jun-21 5:33am    
Don't. As Chris says, it's a bad idea and likely to work fine in dev, but fail in production - which reflects really badly on you and your app as far as users are concerned.

Follow the link Chris gave you - it's not complicated, and it makes your life a whole load easier!
Member 15170612 9-Jun-21 5:35am    
This will take me directly to the .dll file. Is there no other option that would lead me to the .exe?
Chris Copeland 9-Jun-21 5:49am    
Strange that the entry assembly location is giving you the DLL path? In any case, there is also this blog post which outlines another variation. But as both Griff and I have mentioned it's a bad idea to try and store files in the executable directory, you should store them somewhere where permissions aren't going to be an issue. It's very common for applications to use the ProgramData and AppData directories to store files.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900