views:

138

answers:

4

Well if i want to store data for a application machine wide i just use

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

And if i want to store data per user i use

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

But what if i want to save data per application instance? i could use the folder that the application is stored in but the "program files" folders are not made for this type of data storage if i understand it right...

Edit: To clarify what i meant if i install the application 2 times in differnet folders... not instances running sorry.

+1  A: 

Why don't you store the data in the temp directory Path.GetTempPath. You can use an subfolder for your application and another subfolder (maybe a Guid as name would be useful) for your instance. I would implement some manager class that implements IDisposable to allow removing the instance path when the application shuts down.

crauscher
+2  A: 

I don't know of any other application that does that.

If I run two copies of Visual Studio, say, I expect the settings of the last instance closed to be the ones persisted.

If you assign an instance a unique ID (GUID) how do you reuse that value next time you run the application? Or do you want to get a new set of values each time you run the application?

If you want reuse, you could create numbered sub directories (1, 2, 3, 4 etc.) and each time you run the application write a lock file to that directory. Then check for the presence of the lock file and increment the number until you find an unlocked folder.

UPDATE

In light of the comment added - why not get the path of the executable and create a folder under that?

If the user doesn't have rights to that folder you'd have to create some sort of mapping between the location and a GUID (say) which you then appended to the application settings and user settings path.

ChrisF
A: 

Create an instance folder under either the user data folder or the machine data folder if you need to do this. Your application could remove any un-wanted data on exit. Though @ChrisF is right about most usual behaviour.

Stevo3000
A: 

One possibility would be to generate a unique id the first time the program runs and store it somewhere specific to the program (perhaps a local configuration file).

Then when you want to access the files for that specific copy of the application, simply use

System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "\" + uniqueid);

or

System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "\" + uniqueid);
Dan Fuller
This would lead to exakt the same problem where to store the uniqueid? sure a configuration file but a program should never edit its own configuration file if its stored in the same folder as the application.
Petoj
This is an installation specific setting though - the user will never need to change it, so it's ok for the program to set it. Hell, create a separate text file in the installation directory and store it there if you don't want to edit your app.config. It seems like the most simple solution from my point of view.
Dan Fuller
Alternatively, hash or otherwise encode the application installation path and use that as your unique id. This assumes that the user will never move the application and will always install future versions to the same path though.
Dan Fuller