wilsonlarg
wilsonlarg

Reputation: 155

create unique Application Data directory for each instance of an application

I have written a c# application that it is installed as many instances, on different directories defined by the user. e.g.

C:\Program Files(x86)\MyApp1
C:\Program Files(x86)\MyApp2
C:\MyApp1
C:\MyApp2
...

I want to write/read data files in separated directories, one for each of these instances, at Environment.SpecialFolder.LocalApplicationData.

What is the best approach to use in my code in order to reference the corresponded directory from each installed executable. e.g.

C:\Program Files(x86)\MyApp1\app.exe to reference unique app dir C:\Users\xxxx\AppData\Local\MyApp1\

Upvotes: 0

Views: 1169

Answers (3)

Tigran
Tigran

Reputation: 62296

There are several ways to acieve your request. Here are a couple of possible solutions to your problem: 1.

string sPath = System.Environment.GetFolderPath(     Environment.SpecialFolder.CommonApplicationData) 
       + Path.DirectorySeparatorChar + APP_NAME + Path.DirectorySeparatorChar`

where APP_NAME your assembly name. This code will ends up on *...AllUsers\Application Data\APP_NAME* folder.

  1. Use IsoltaedStorage

but you limited in space terms..

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

If you only consume this data from your application and different data should be available per each user, you could have a look at IsolatedStorage, it allows you to abstract from the real location of the data and you can store/retrieve data easily.

the fact that you install the same application in different folders and with different names it's an indication of, at minimum, something I can't understand right now, but in the end you should design it in a way that every instance works isolated like if those were really different applications (like word, excel, notepad...) meaning not hard coded paths ever :)

Upvotes: 1

Deanna
Deanna

Reputation: 24283

You can append a unique value onto the end of the path: C:\Users\xxxx\AppData\Local\MyApp\ghfdsjgb23

If there is no such unique value per installation, you can use a hash of the installation path or similar.

Upvotes: 1

Related Questions