Reputation: 1969
I'm working on an App, and in the main folder of the app, I have a text file with a list of sites.
I'm having some trouble manipulated the file.
I want to ship the text file with info inside of him with the App, and in the App give the user a place where he can change the content. save it and show the changes.
my qustions are:
Where should I place a file like that?
What is the best way to get it from the appliction?
Why when I place the file in the files folder I can get when I'm runing the app from Visual Studio, but when I relase the App and install it the App cant find the file, and I need to create it (and lossing data)?
Upvotes: 0
Views: 162
Reputation: 39530
The proper way to find the right location is to use Environment.GetFolderPath and then pass in something like SpecialFolder.LocalApplicationData
.
You need to look at the docs for the SpecialFolder enum, though, because there are lots of subtle variations on user data locations.
Your installer tool will probably have facilities to find the same 'special locations', so you can have some hope the file will be where you expect it to be. Of course, this may not help if one user runs the installer and another uses the app, but you might be OK in simple situations.
To try and clarify:
There are two problems here:
Both of these are solved by Windows defining various 'special folders' - you're not supposed to care about exactly where these are, but there are ways to find them when you need them.
To solve problem '1', you need to look at your installer docs, and find out how it deals with special folders - there are almost always macros or some similar mechanism to set the destination of a file to a special folder. To solve problem '2', you need to use Environment.GetFolderPath().
Upvotes: 2
Reputation: 8563
If the file is specific to a single user, you should read / write it to the Application Data directory.
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
If the file is shared among all users, use the Shared Application Data directory:
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
Upvotes: 0