Where can I safely store data files for a ClickOnce deployment?

只愿长相守 提交于 2019-11-27 02:39:30

问题


I have been using ApplicationDeployment.CurrentDeployment.DataDirectory to store content downloaded by the client at runtime which is expected to be there every time the app launches, however now I've found this changes seemingly randomly if the application is updated.

What is the best reliable method for storing user data for the application in click-once deployments?

Currently I've been using the following method

private const string LocalPath = "data";

public string GetStoragePath() {
    string dir;
    if (ApplicationDeployment.IsNetworkDeployed) {
        ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
        dir = Path.Combine(ad.DataDirectory, LocalPath);
    } else {
        dir = LocalPath;
    }
    return CreateDirectory(dir);
}

I originally followed the article Accessing Local and Remote Data in ClickOnce Applications under the heading ClickOnce Data Directory which states this is recommended path.

NOTE: CreateDirectory(string) simply creates a directory if it doesn't already exist.

I have found the root cause of my problem is I'm creating many files and an index file, this index file contains absolute paths, click-once moves the content (or copies) on an upgrade, so the absolute paths no longer exist. I will investigate isolated storage as Damokles suggests to see if this has the same side affect for click-once deployments.


回答1:


Another option is to make a directory for your application in the user's AppData folder and store it there. You can get a path to that with this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

You'll find a lot of applications use that (and it's local equivalent). It also doesn't move around between ClickOnce versions.




回答2:


Check out IsolatedStorage this should help. It even works in partial trust environments.

To keep you data you need to use the application scoped IsolatedStorage

using System.IO;
using System.IO.IsolatedStorage;
...

IsolatedStorageFile appScope = IsolatedStorageFile.GetUserStoreForApplication();    
using(IsolatedStorageFileStream fs = new IsolatedStorageFileStream("data.dat", FileMode.OpenOrCreate, appScope))
{
...

code taken from this post




回答3:


It depends on the data you are saving.

You are currently saving to the Data Directory which is fine. What you need to be aware of is that each version of the application has its own Data Directory. When you update ClickOnce copies all the data from the previous version to the new version when the application is started up. This gives you a hook to migrate any of the data from one version to the next. This is good for in memory databases like Sql Lite or SQL CE.

One thing that I cam across is that when you have a large amount of data (4 gig) if you store it in the Data Directory this data will be copied from the old version to the new version. This will slow down the start up time after an upgrade. If you have a large amount of data or you don't want to worry about migrating data you can either store the data in the users local folder providing you have full trust or you can use isolated storage if you have a partial trust.

Isolated Storage

Local User Application Data



来源:https://stackoverflow.com/questions/6760565/where-can-i-safely-store-data-files-for-a-clickonce-deployment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!