Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top