Windows Store Data for Windows

Well, if I want to store data for an application machine, I just use

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) 

And if I want to store data for each user, I use

 Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) 

But what if I want to save data to an application instance? I could use the folder in which the application is stored, but the โ€œprogram filesโ€ folders are not created for this type of data storage, if I understand it correctly ...

Edit: clarify what I had in mind if I install the application 2 times in differnet folders ... not instances that start sorry.

+4
source share
4 answers

I do not know any other application that does this.

If I run two copies of Visual Studio, let's say I expect the settings of the last instance are closed so that they are saved.

If you assign a unique identifier (GUID) to an instance, how do you reuse this value the next time you launch the application? Or do you want to get a new set of values โ€‹โ€‹every time you start the application?

If you want to reuse, you can create numbered subdirectories (1, 2, 3, 4, etc.), and each time you launch the application, write a lock file to this directory. Then check for the lock file and increase the number until you find the unlocked folder.

UPDATE

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

If the user does not have rights to this folder, you will need to create some kind of mapping between the location and the GUID (say), which you then added to the application settings and the user settings path.

+2
source

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

+1
source

Create an instance folder either in the user data folder or in the machine data folder if you need to do this. Your application may delete any unnecessary data upon exit. Although @ChrisF is right about the most common behavior.

0
source

One of the possibilities would be to create a unique identifier at the first start of the program and keep it somewhere specific to the program (possibly a local configuration file).

Then, when you want to access the files for this particular copy of the application, just use

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

or

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

All Articles