0

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\
2
  • notice that C:\Users\xxxx\AppData... is "User specific application data", not "application specific only". do you need all users to read same or different data? Jul 8, 2011 at 12:54
  • Yes I need "User specific application data" but the question could apply for any windows special folder I guess
    – wilsonlarg
    Jul 8, 2011 at 12:57

3 Answers 3

1

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.

2
  • That was my initial thought. I was wondering if there is way to identify a unique hash for the installed instance through registry maybe.
    – wilsonlarg
    Jul 8, 2011 at 13:30
  • Some installers store the paths in the registry, but you still need to knwow where to get that from (and how it differentiates between them). If you don't keep one, your best best is to hash the path.
    – Deanna
    Jul 8, 2011 at 13:43
1

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 :)

1

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..

2
  • but APP_NAME stays the same for each instance. Instances have the same application name and version. They just installed on different paths and have different registry entries added by the installer each time
    – wilsonlarg
    Jul 8, 2011 at 14:04
  • So, at this point, may be you can generate unique path by using parent folder of app (which is for sure unique for every one) + APP_NAME
    – Tigran
    Jul 8, 2011 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.