none
Best practices storing Application Data and Resources

    Question

  • I'm developing a windows application and I really found no real good answer on what is the best practices when it comes to storing application specific data.

    1. Where is the best place to store application data which is user specific data which is persistant (but regularly updated/added)?
    2. Where is the best place to store application data which is cross/all user specific data which is persistant (but regularly updated/added)?
    3. Where is the best place to store application specific data which is persistant (but regularly updated/added)?

    I also have some files added in my project which is added as content, I want them to be copied to the folder for user data.

    I know you can use the Environment.GetFolderPath(Environment.SpecialFolder.*) class with the enum og special folders. I'm guessing you should use Environment.SpecialFolder.ApplicationData to store user specific data, and CommonApplicationData for saving for all users and application data? This is data that's going to be updated and such, as settings. Needs to be a place which doesn't require administrator rights to write to.

    Also how do I copy my project items which is content to for example CommonApplicationData and not the output folder?

    Cheers,
    Arne
    Thursday, February 04, 2010 10:00 AM

Answers

  • Correct!!
    for user specific data : <<user>>\AppData\
    and I think CommonApplicationData should be the right place to hold data for all users 
    Paras
    • Marked as answer by ArneHB Thursday, February 04, 2010 11:05 AM
    Thursday, February 04, 2010 10:04 AM
  • See http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx for details.

    Use LocalApplicationData for your user-specific data.
    Use CommonApplicationData for your install-time program settings (you need appropriate rights to write stuff here).
    • Marked as answer by ArneHB Thursday, February 04, 2010 11:05 AM
    Thursday, February 04, 2010 10:19 AM
  • I would go a different route, and choose IsolatedStorage for several reasons.

    1. Secure - nobody can go and mess with the data that is stored there, as it's encrypted format
    2. There is User store for case 1, which is guaranteed to be specific to one windows user account (of course if you have your own user system, then this is not a good solution) and there is also Machine Store for case 2
    3. No need for user to have rights for folders. With Isolated Storage even web applications can write to disk, without needing to give folder access permissions.

    I feel that this part is quite forgotten, but still very useful part of .NET

    Cheers,

    Jani

    • Marked as answer by ArneHB Thursday, February 04, 2010 1:17 PM
    Thursday, February 04, 2010 12:10 PM
  • I followed the lead to Matthew and Paras here. In the install project under File System can you add your own folders like "Common Files Folder" (Program Files\Common) and "User's Application Data Folder" (user/appdata/roaming), or add your own if you want it in ProgramData. You can do this by right clicking the "File System on Target Machine" and "Add Special Folder -> (The folder type)". If you want to make a ProgramData folder then select Custom Folder and rename it to "Common Application Data Folder" and add what Paras said to "Default Location" to "[CommonAppDataFolder][Manufacturer]\[ProductName]", and change the property to "COMMONAPPDATAFOLDER". 

    Right click on the new folder you've made and select "Add->Project Output". Select the correct project in the drop down at the top, then select "Content Files". This should copy all the output files to that folder. Going to test it now.

    Thanks for the help. I'll have a look into Isolated Storage JaniN later, looks intresting.
    • Marked as answer by ArneHB Thursday, February 04, 2010 1:18 PM
    Thursday, February 04, 2010 12:50 PM

All replies

  • Correct!!
    for user specific data : <<user>>\AppData\
    and I think CommonApplicationData should be the right place to hold data for all users 
    Paras
    • Marked as answer by ArneHB Thursday, February 04, 2010 11:05 AM
    Thursday, February 04, 2010 10:04 AM
  • See http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx for details.

    Use LocalApplicationData for your user-specific data.
    Use CommonApplicationData for your install-time program settings (you need appropriate rights to write stuff here).
    • Marked as answer by ArneHB Thursday, February 04, 2010 11:05 AM
    Thursday, February 04, 2010 10:19 AM
  • @ ArneHB : Pardon me... but I replied after reading your entire post.. that is why i mentioned " I think CommonApplicationData should be the right place to hold data for all users "..
    and I thoughtt <<user>>\AppData\ wud refer to Application Data.. My bad


    Paras
    Thursday, February 04, 2010 10:22 AM
  • Do you want to put those files into the CommonApplicationData folder at installaton time? I'm not very familiar with writing installers, but if you open your program solution in Visual Studio, you can add a new installtion project to it via:

    File | Add | New Project... -> Other Project Types -> Setup Project

    After that... I don't really know I'm afraid. But I do know it allows you to select files that you'll want to be installed to various places.
    Thursday, February 04, 2010 10:47 AM
    1. Right-click your setup project in the Solution Explorer and pick "View -> File System".

    2. goto property of Application Folder and change the folder's DefaultLocation property to "[CommonAppDataFolder][Manufacturer]\[ProductName]".

      Hope this helps...Lemme know if this is not what you asked for...


    Paras
    Thursday, February 04, 2010 10:59 AM
  • This will install the program aswell in ProgramData in the installer, not quite what I'm after.

    Let's say we have a XML file which contains different settings to the program. We've made the XML file and added it as content in the project and selected to copy itself to the output folder. Basicly I want that file to be copied to [CommonAppDataFolder]\[ProductName], while the program itself gets installed at [ProgramFilesFolder][Manufacturer]\[ProductName]. The other problem is that the user will have to run the program as administrator if they wish to edit the settings in the program which edits the .XML file in [CommonAppDataFolder]\[ProductName] (C:\ProgramData\), right? The alternative is that I can generate this XML in code and save it in [CommonAppDataFolder], but I'm just wondering if it's posible to make content in a project to install itself at a spesific folder. It's quite easy generating a XML and place it at the right position programaticly, but you can't use the same method if you have a image you want a user to be able to change which would be application specific (like a logo)
    Thursday, February 04, 2010 11:41 AM
  • You're supose to be able to edit the .csproj and add

        <Copy SourceFiles="$(OutputPath)yourfiles" DestinationFolder="$(YourVariable)" ContinueOnError="true" />

    within
      <Target Name="AfterBuild">
      </Target>

    Will try it out now.
    Edit added: http://msdn.microsoft.com/en-us/library/3e54c37h.aspx
    Thursday, February 04, 2010 11:56 AM
  • I would go a different route, and choose IsolatedStorage for several reasons.

    1. Secure - nobody can go and mess with the data that is stored there, as it's encrypted format
    2. There is User store for case 1, which is guaranteed to be specific to one windows user account (of course if you have your own user system, then this is not a good solution) and there is also Machine Store for case 2
    3. No need for user to have rights for folders. With Isolated Storage even web applications can write to disk, without needing to give folder access permissions.

    I feel that this part is quite forgotten, but still very useful part of .NET

    Cheers,

    Jani

    • Marked as answer by ArneHB Thursday, February 04, 2010 1:17 PM
    Thursday, February 04, 2010 12:10 PM
  • I followed the lead to Matthew and Paras here. In the install project under File System can you add your own folders like "Common Files Folder" (Program Files\Common) and "User's Application Data Folder" (user/appdata/roaming), or add your own if you want it in ProgramData. You can do this by right clicking the "File System on Target Machine" and "Add Special Folder -> (The folder type)". If you want to make a ProgramData folder then select Custom Folder and rename it to "Common Application Data Folder" and add what Paras said to "Default Location" to "[CommonAppDataFolder][Manufacturer]\[ProductName]", and change the property to "COMMONAPPDATAFOLDER". 

    Right click on the new folder you've made and select "Add->Project Output". Select the correct project in the drop down at the top, then select "Content Files". This should copy all the output files to that folder. Going to test it now.

    Thanks for the help. I'll have a look into Isolated Storage JaniN later, looks intresting.
    • Marked as answer by ArneHB Thursday, February 04, 2010 1:18 PM
    Thursday, February 04, 2010 12:50 PM