Search code examples
.netwindowsdata-storage

.NET Windows Store Data


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.


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.