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\开发者_StackOverflow社区app.exe to reference unique app dir C:\Users\xxxx\AppData\Local\MyApp1\
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.
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 :)
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.
- Use IsoltaedStorage
but you limited in space terms..
精彩评论